Allow only printing the results to console and only printing CIDRs
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/.idea/
|
||||
@@ -24,7 +24,9 @@ Usage:
|
||||
-t string
|
||||
Domain or IP address (Required)
|
||||
-p string
|
||||
Print results to console
|
||||
Only print results to console
|
||||
-c
|
||||
Only print CIDRs
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
asnip: ASN target organization IP range attack surface mapping
|
||||
by github.com/harleo — MIT License
|
||||
by github.com/harleo — MIT Licence
|
||||
*/
|
||||
|
||||
package main
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"bufio"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -29,18 +29,29 @@ func (s sliceVal) String() string {
|
||||
return str
|
||||
}
|
||||
|
||||
func writeLines(lines []string, path string) error {
|
||||
func writeLines(lines []string, path string) {
|
||||
file, err := os.Create(path)
|
||||
if err != nil {
|
||||
log.Fatalf("[!] Couldn't create file: %s\n", err.Error())
|
||||
}
|
||||
defer file.Close()
|
||||
defer func(file *os.File) {
|
||||
err := file.Close()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to write file %s: %s", file.Name(), err)
|
||||
}
|
||||
}(file)
|
||||
|
||||
w := bufio.NewWriter(file)
|
||||
for _, line := range lines {
|
||||
fmt.Fprintln(w, line)
|
||||
_, err := fmt.Fprintln(w, line)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to write '%s' to file %s: %s", line, file.Name(), err)
|
||||
}
|
||||
}
|
||||
flushErr := w.Flush()
|
||||
if flushErr != nil {
|
||||
log.Fatalf("Couldn't write file %s: %s", file.Name(), flushErr)
|
||||
}
|
||||
return w.Flush()
|
||||
}
|
||||
|
||||
func httpRequest(URI string) string {
|
||||
@@ -49,12 +60,17 @@ func httpRequest(URI string) string {
|
||||
log.Fatalf("[!] Error sending request: %s\n", errGet.Error())
|
||||
}
|
||||
|
||||
responseText, errRead := ioutil.ReadAll(response.Body)
|
||||
responseText, errRead := io.ReadAll(response.Body)
|
||||
if errRead != nil {
|
||||
log.Fatalf("[!] Error reading response: %s\n", errRead.Error())
|
||||
}
|
||||
|
||||
defer response.Body.Close()
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}(response.Body)
|
||||
return string(responseText)
|
||||
}
|
||||
|
||||
@@ -77,7 +93,7 @@ func CIDRToIP(cidr string) []string {
|
||||
if err != nil {
|
||||
log.Fatalf("[!] Failed to convert CIDR to IP: %s\n", err.Error())
|
||||
}
|
||||
|
||||
|
||||
var ips []string
|
||||
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); incrementIP(ip) {
|
||||
ips = append(ips, ip.String())
|
||||
@@ -91,8 +107,8 @@ func CIDRToIP(cidr string) []string {
|
||||
}
|
||||
}
|
||||
|
||||
func getIP(ipdomain string) []net.IP {
|
||||
ip, err := net.LookupIP(ipdomain)
|
||||
func getIP(ipDomain string) []net.IP {
|
||||
ip, err := net.LookupIP(ipDomain)
|
||||
if err != nil {
|
||||
log.Fatalf("[!] Error looking up domain: %s\n", err.Error())
|
||||
}
|
||||
@@ -114,8 +130,9 @@ func checkAPIRateLimit(response string) {
|
||||
|
||||
func main() {
|
||||
var (
|
||||
target = flag.String("t", "", "Domain or IP address (Required)")
|
||||
print = flag.Bool("p", false, "Print results to console")
|
||||
target = flag.String("t", "", "Domain or IP address (Required)")
|
||||
onlyPrint = flag.Bool("p", false, "Only print results to console")
|
||||
onlyCidrs = flag.Bool("c", false, "Only download list of CIDRs")
|
||||
)
|
||||
|
||||
flag.Parse()
|
||||
@@ -135,33 +152,36 @@ func main() {
|
||||
checkAPIRateLimit(apiASResponse)
|
||||
|
||||
var CIDRSv4 []string
|
||||
for _, cidrv4 := range apiASResponseInfo[3:] {
|
||||
if !IsIPv6CIDR(cidrv4) {
|
||||
CIDRSv4 = append(CIDRSv4, cidrv4)
|
||||
for _, cidrV4 := range apiASResponseInfo[3:] {
|
||||
if !IsIPv6CIDR(cidrV4) {
|
||||
CIDRSv4 = append(CIDRSv4, cidrV4)
|
||||
}
|
||||
}
|
||||
sort.Strings(CIDRSv4)
|
||||
|
||||
if *print {
|
||||
if *onlyPrint {
|
||||
fmt.Print(sliceVal(CIDRSv4))
|
||||
}
|
||||
fmt.Printf("[:] Writing %d CIDRs to file...\n", len(CIDRSv4))
|
||||
writeLines(CIDRSv4, "cidrs.txt")
|
||||
|
||||
var ips []string
|
||||
fmt.Println("[:] Converting to IPs...")
|
||||
for _, cidr := range CIDRSv4 {
|
||||
ips = append(ips, CIDRToIP(cidr)...)
|
||||
} else {
|
||||
fmt.Printf("[:] Writing %d CIDRs to file...\n", len(CIDRSv4))
|
||||
writeLines(CIDRSv4, "cidrs.txt")
|
||||
}
|
||||
|
||||
if *print {
|
||||
for _, ipsValue := range ips {
|
||||
fmt.Println(ipsValue)
|
||||
if *onlyCidrs {
|
||||
var ips []string
|
||||
fmt.Println("[:] Converting to IPs...")
|
||||
for _, cidr := range CIDRSv4 {
|
||||
ips = append(ips, CIDRToIP(cidr)...)
|
||||
}
|
||||
|
||||
if *onlyPrint {
|
||||
for _, ipsValue := range ips {
|
||||
fmt.Println(ipsValue)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("[:] Writing %d IPs to file...\n", len(ips))
|
||||
writeLines(ips, "ips.txt")
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("[:] Writing %d IPs to file...\n", len(ips))
|
||||
writeLines(ips, "ips.txt")
|
||||
|
||||
fmt.Println("[!] Done.")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user