Skip to content

Commit e4041ed

Browse files
authored
Add files via upload
first version 🚀
0 parents  commit e4041ed

12 files changed

+353
-0
lines changed

go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module ipmap
2+
3+
go 1.19
4+
5+
require github.com/corpix/uarand v0.2.0

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/corpix/uarand v0.2.0 h1:U98xXwud/AVuCpkpgfPF7J5TQgr7R5tqT8VZP5KWbzE=
2+
github.com/corpix/uarand v0.2.0/go.mod h1:/3Z1QIqWkDIhf6XWn/08/uMHoQ8JUoTIKc2iPchBOmM=
3+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
6+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=

main.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"ipmap/modules"
7+
"ipmap/tools"
8+
"strconv"
9+
"strings"
10+
)
11+
12+
var (
13+
domain = flag.String("d", "", "domain parameter")
14+
asn = flag.String("asn", "", "asn parameter")
15+
ip = flag.String("ip", "", "ip parameter")
16+
timeout = flag.Int("t", 0, "timeout parameter")
17+
con = flag.Bool("c", false, "continue parameter")
18+
export = flag.Bool("export", false, "export parameter")
19+
DomainTitle string
20+
)
21+
22+
func main() {
23+
flag.Parse()
24+
if (*asn != "" && *ip != "") || (*asn == "" && *ip == "") {
25+
fmt.Println("======================================================\n" +
26+
" ipmap v1.0 (github.com/sercanarga/ipmap)\n" +
27+
"======================================================\n" +
28+
"PARAMETERS:\n" +
29+
"-asn AS13335\n" +
30+
"-ip 103.21.244.0/22,103.22.200.0/22\n" +
31+
"-d example.com\n" +
32+
"-t 200 (timout default:auto)\n" +
33+
"--c (work until finish scanning)\n" +
34+
"--export (auto export results)\n\n" +
35+
"USAGES:\n" +
36+
"Finding sites by scanning all the IP blocks\nipmap -ip 103.21.244.0/22,103.22.200.0/22\n\n" +
37+
"Finding real IP address of site by scanning given IP addresses\nipmap -ip 103.21.244.0/22,103.22.200.0/22 -d example.com\n\n" +
38+
"Finding sites by scanning all the IP blocks in the ASN\nipmap -asn AS13335\n\n" +
39+
"Finding real IP address of site by scanning all IP blocks in ASN\nipmap -asn AS13335 -d example.com")
40+
return
41+
}
42+
43+
if *timeout == 0 && *domain == "" {
44+
fmt.Println("Timeout parameter( -t ) is not set. By entering the domain, you can have it calculated automatically.")
45+
return
46+
}
47+
48+
if *domain != "" {
49+
getDomain := modules.GetDomainTitle(*domain)
50+
if len(getDomain) == 0 {
51+
fmt.Println("Domain not resolved.")
52+
return
53+
}
54+
DomainTitle = getDomain[0]
55+
56+
if *timeout == 0 {
57+
resolveTime, _ := strconv.Atoi(getDomain[1])
58+
*timeout = ((resolveTime * 15) / 100) + resolveTime
59+
}
60+
}
61+
62+
if *ip != "" {
63+
splitIP := strings.Split(*ip, ",")
64+
tools.FindIP(splitIP, *domain, DomainTitle, *con, *export, *timeout)
65+
return
66+
}
67+
68+
if *asn != "" {
69+
tools.FindASN(*asn, *domain, DomainTitle, *con, *export, *timeout)
70+
return
71+
}
72+
73+
}

modules/calc_ip_address.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package modules
2+
3+
import "net"
4+
5+
func CalcIPAddress(cidr string) ([]string, error) {
6+
ip, ipnet, err := net.ParseCIDR(cidr)
7+
if err != nil {
8+
return nil, err
9+
}
10+
11+
var ips []string
12+
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
13+
ips = append(ips, ip.String())
14+
}
15+
16+
return ips[1 : len(ips)-1], nil
17+
}
18+
19+
func inc(ip net.IP) {
20+
for j := len(ip) - 1; j >= 0; j-- {
21+
ip[j]++
22+
if ip[j] > 0 {
23+
break
24+
}
25+
}
26+
}

modules/find_ip_blocks.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package modules
2+
3+
func FindIPBlocks(asn string) string {
4+
output := RequestFunc("https://www.radb.net/query?advanced_query=1&keywords="+asn+"&-T+option=&ip_option=&-i=1&-i+option=origin", "www.radb.net", 5000)
5+
if len(output) > 0 {
6+
return output[2]
7+
}
8+
9+
return ""
10+
}

modules/get_domain_title.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package modules
2+
3+
import (
4+
"regexp"
5+
)
6+
7+
func GetDomainTitle(url string) []string {
8+
getTitle := RequestFunc("http://"+url, url, 5000)
9+
re := regexp.MustCompile(`.*?<title>(.*?)</title>.*`)
10+
11+
if len(getTitle) > 0 {
12+
match := re.FindStringSubmatch(getTitle[2])
13+
return []string{match[1], getTitle[3]}
14+
}
15+
16+
return []string{}
17+
}

modules/get_site.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package modules
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
)
7+
8+
func GetSite(ip string, domain string, timeout int) []string {
9+
requestSite := RequestFunc("http://"+ip, domain, timeout)
10+
11+
if len(requestSite) > 0 {
12+
re := regexp.MustCompile(`.*?<title>(.*?)</title>.*`)
13+
title := re.FindStringSubmatch(requestSite[2])
14+
if len(title) > 0 {
15+
explodeHttpCode := strings.Split(requestSite[0], " ")
16+
return []string{explodeHttpCode[0], requestSite[1], title[1]}
17+
}
18+
}
19+
20+
return []string{}
21+
}

modules/request.go

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package modules
2+
3+
import (
4+
"context"
5+
"github.com/corpix/uarand"
6+
"net/http"
7+
"net/http/httputil"
8+
"strconv"
9+
"time"
10+
)
11+
12+
func RequestFunc(ip string, url string, timeout int) []string {
13+
n := time.Now()
14+
15+
req, err := http.NewRequest("GET", ip, nil)
16+
if err != nil {
17+
return []string{}
18+
}
19+
20+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Millisecond)
21+
defer cancel()
22+
req = req.WithContext(ctx)
23+
24+
req.Host = url
25+
26+
req.Header.Set("User-Agent", uarand.GetRandom())
27+
resp, err := http.DefaultClient.Do(req)
28+
if err != nil {
29+
return []string{}
30+
}
31+
32+
last, err := httputil.DumpResponse(resp, true)
33+
if err != nil {
34+
return []string{}
35+
}
36+
37+
return []string{resp.Status, ip, string(last), strconv.FormatInt(time.Since(n).Milliseconds(), 10)}
38+
}

modules/resolve_site.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package modules
2+
3+
import "fmt"
4+
5+
func ResolveSite(IPAddress []string, Websites [][]string, DomainTitle string, IPBlocks []string, domain string, con bool, export bool, timeout int) {
6+
for _, ip := range IPAddress {
7+
site := GetSite(ip, domain, timeout)
8+
if len(site) > 0 {
9+
fmt.Print("+")
10+
Websites = append(Websites, site)
11+
12+
if DomainTitle != "" && site[2] == DomainTitle && con == false {
13+
PrintResult("Search Domain by ASN", DomainTitle, timeout, IPBlocks, Websites, export)
14+
return
15+
}
16+
} else {
17+
fmt.Print("-")
18+
}
19+
}
20+
21+
PrintResult("Search All ASN/IP", DomainTitle, timeout, IPBlocks, Websites, export)
22+
}

modules/result_print.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package modules
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strconv"
7+
"strings"
8+
"time"
9+
)
10+
11+
func exportFile(result string) {
12+
fileName := "ipmap_" + strconv.FormatInt(time.Now().Local().Unix(), 10) + "_export.txt"
13+
f, err := os.Create(fileName)
14+
if err != nil {
15+
fmt.Println("Export file creation error")
16+
return
17+
}
18+
defer f.Close()
19+
20+
_, err = f.WriteString(result)
21+
if err != nil {
22+
fmt.Println("Export file write error")
23+
return
24+
}
25+
26+
fmt.Println("Successfully exported: " + fileName)
27+
return
28+
29+
}
30+
31+
func PrintResult(method string, title string, timeout int, ipblocks []string, founded [][]string, export bool) {
32+
fmt.Println("\n")
33+
34+
resultString := "==================== RESULT ===================="
35+
resultString += "\nMethod: " + method
36+
37+
if title != "" {
38+
resultString += "\nSearch Site: " + title
39+
}
40+
41+
resultString += "\nTimeout: " + strconv.Itoa(timeout) + "ms"
42+
resultString += "\nIP Blocks: " + strings.Join(ipblocks, ",")
43+
44+
resultString += "\nFounded Websites:\n"
45+
if len(founded) > 0 {
46+
for _, site := range founded {
47+
resultString += strings.Join(site, ", ") + "\n"
48+
}
49+
}
50+
resultString += "================================================"
51+
fmt.Println(resultString)
52+
53+
if export == true {
54+
exportFile(resultString)
55+
return
56+
}
57+
58+
fmt.Print("\nDo you want to export result to file? (Y/n): ")
59+
var ex string
60+
_, err := fmt.Scanln(&ex)
61+
if err != nil {
62+
return
63+
}
64+
65+
if ex == "y" || ex == "Y" || ex == "" {
66+
exportFile(resultString)
67+
} else {
68+
fmt.Println("Export canceled")
69+
}
70+
}

tools/find_asn.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package tools
2+
3+
import (
4+
"fmt"
5+
"ipmap/modules"
6+
"regexp"
7+
"strconv"
8+
"time"
9+
)
10+
11+
var (
12+
IPBlocks []string
13+
IPAddress []string
14+
Websites [][]string
15+
)
16+
17+
func FindASN(asn string, domain string, domainTitle string, con bool, export bool, timeout int) {
18+
re := regexp.MustCompile(`(?m)route:\s+([0-9\.\/]+)$`)
19+
for _, match := range re.FindAllStringSubmatch(modules.FindIPBlocks(asn), -1) {
20+
IPBlocks = append(IPBlocks, match[1])
21+
}
22+
23+
for _, block := range IPBlocks {
24+
ips, err := modules.CalcIPAddress(block)
25+
if err != nil {
26+
return
27+
}
28+
29+
IPAddress = append(IPAddress, ips...)
30+
}
31+
32+
fmt.Println("ASN: " + asn +
33+
"\nIP Block: " + strconv.Itoa(len(IPBlocks)) +
34+
"\nIP Address: " + strconv.Itoa(len(IPAddress)) +
35+
"\nStart Time: " + time.Now().Local().String() +
36+
"\nEnd Time: " + time.Now().Add((time.Millisecond*time.Duration(timeout))*time.Duration(len(IPAddress))).Local().String())
37+
38+
modules.ResolveSite(IPAddress, Websites, domainTitle, IPBlocks, domain, con, export, timeout)
39+
}

tools/find_ip.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package tools
2+
3+
import (
4+
"fmt"
5+
"ipmap/modules"
6+
"strconv"
7+
"time"
8+
)
9+
10+
func FindIP(IPBlocks []string, domain string, domainTitle string, con bool, export bool, timeout int) {
11+
for _, block := range IPBlocks {
12+
ips, err := modules.CalcIPAddress(block)
13+
if err != nil {
14+
return
15+
}
16+
17+
IPAddress = append(IPAddress, ips...)
18+
}
19+
20+
fmt.Println("IP Block: " + strconv.Itoa(len(IPBlocks)) +
21+
"\nIP Address: " + strconv.Itoa(len(IPAddress)) +
22+
"\nStart Time: " + time.Now().Local().String() +
23+
"\nEnd Time: " + time.Now().Add((time.Millisecond*time.Duration(timeout))*time.Duration(len(IPAddress))).Local().String())
24+
25+
modules.ResolveSite(IPAddress, Websites, domainTitle, IPBlocks, domain, con, export, timeout)
26+
}

0 commit comments

Comments
 (0)