-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathmain.go
133 lines (117 loc) · 4.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"github.com/incogbyte/shosubgo/apishodan"
)
const Author = "inc0gbyt3"
func main() {
domain := flag.String("d", "", "> Domain to find subdomains")
shodanKey := flag.String("s", "", "> Shodan api key")
verbose := flag.Bool("v", false, "> Show all output")
fileName := flag.String("o", "", "> Save domains into a file")
inputFile := flag.String("f", "", "> File containing domains to find subdomains")
jsonFlag := flag.Bool("json", false, "> Save output in JSON format")
flag.Parse()
if *domain == "" && *inputFile == "" {
fmt.Printf("[*] Usage: %s -d target.com -s shodanKey [-f input_file]\n", os.Args[0])
fmt.Printf("[*] Author: %s\n", Author)
os.Exit(1)
}
apiKey := apishodan.New(*shodanKey)
var domains []string
if *domain != "" {
domains = append(domains, *domain)
}
if *inputFile != "" {
fileDomains, err := readDomainsFromFile(*inputFile)
if err != nil {
log.Fatalf("Failed to read domains from file: %v", err)
}
domains = append(domains, fileDomains...)
}
for _, domainSearch := range domains {
subdomain, err := apiKey.GetSubdomain(domainSearch)
if err != nil {
fmt.Printf("Error fetching subdomains for domain %s: %v\n", domainSearch, err)
continue
}
if len(subdomain.SubDomains) == 0 {
fmt.Printf("No subdomains found for domain %s\n", domainSearch)
continue
}
if *verbose {
info, err := apiKey.InfoAccount()
if err != nil {
fmt.Printf("Error fetching account info: %v\n", err)
continue
}
fmt.Printf("[*] Credits: %d\nScan Credits: %d\n\n", info.QueryCredits, info.ScanCredits)
for _, v := range subdomain.Data {
d := v.SubD + subdomain.Domain
fmt.Printf("[*] Domain: %s\nIP/DNS: %s\nLast Scan made by Shodan: %s\n", d, v.Value, v.LastSeen)
}
} else {
if *jsonFlag {
jsonData, err := json.MarshalIndent(subdomain.SubDomains, "", " ")
if err != nil {
log.Fatal("Error marshaling JSON:", err)
}
if *fileName != "" {
f, err := os.OpenFile(*fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
_, err = f.Write(jsonData)
if err != nil {
log.Fatal(err)
}
_, err = f.WriteString("\n")
if err != nil {
log.Fatal(err)
}
f.Close()
fmt.Println("[*] DONE writing JSON to file:", *fileName)
} else {
fmt.Println(string(jsonData))
}
} else {
for _, v := range subdomain.SubDomains {
if *fileName != "" {
f, err := os.OpenFile(*fileName, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
_, err = f.WriteString(v + "\n")
if err != nil {
log.Fatal(err)
}
f.Close()
fmt.Println("[*] DONE writing to file:", *fileName)
}
fmt.Printf("%s.%s\n", v, domainSearch)
}
}
}
}
}
func readDomainsFromFile(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var domains []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
domains = append(domains, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return domains, nil
}