-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.go
358 lines (318 loc) · 10.9 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package main
import (
"bufio"
"crypto/sha1"
"crypto/tls"
"flag"
"fmt"
"net"
"os"
"regexp"
"sort"
"strings"
"sync"
"time"
"github.com/3th1nk/cidr"
"github.com/json-iterator/go"
"github.com/panjf2000/ants/v2"
"github.com/valyala/fasthttp"
)
var (
client *fasthttp.Client
similarDetector *Similar
timeout time.Duration
urlRegex = regexp.MustCompile(`^(http|ws)s?://`)
portMedium = []string{"8000", "8080", "8443"}
portLarge = []string{"81", "591", "2082", "2087", "2095", "2096", "3000", "8000", "8001", "8008", "8080", "8083", "8443", "8834", "8888"}
portXlarge = []string{"81", "300", "591", "593", "832", "981", "1010", "1311", "2082", "2087", "2095", "2096", "2480", "3000", "3128", "3333", "4243", "4567", "4711", "4712", "4993", "5000", "5104", "5108", "5800", "6543", "7000", "7396", "7474", "8000", "8001", "8008", "8014", "8042", "8069", "8080", "8081", "8088", "8090", "8091", "8118", "8123", "8172", "8222", "8243", "8280", "8281", "8333", "8443", "8500", "8834", "8880", "8888", "8983", "9000", "9043", "9060", "9080", "9090", "9091", "9200", "9443", "9800", "9981", "12443", "16080", "18091", "18092", "20720", "28017"}
)
type probeArgs []string
func (p *probeArgs) Set(val string) error {
*p = append(*p, val)
return nil
}
func (p probeArgs) String() string {
return strings.Join(p, ",")
}
type verboseStruct struct {
Site string `json:"site"`
StatusCode int `json:"status_code"`
Server string `json:"server"`
ContentType string `json:"content_type"`
Location string `json:"location"`
}
type Task struct {
Scheme string
Url string
}
func (t *Task) String() string {
return fmt.Sprintf("%s://%s", t.Scheme, t.Url)
}
func init() {
client = &fasthttp.Client{
NoDefaultUserAgentHeader: true,
Dial: func(addr string) (net.Conn, error) {
return fasthttp.DialDualStackTimeout(addr, time.Minute*3) // net/http's default is 3 minutes
},
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
Renegotiation: tls.RenegotiateOnceAsClient, // For "local error: tls: no renegotiation"
},
// This also limits the maximum header size.
ReadBufferSize: 48 * 1024,
WriteBufferSize: 48 * 1024,
MaxIdleConnDuration: time.Second,
}
}
func main() {
// Threads
var concurrency int
flag.IntVar(&concurrency, "c", 50, "Concurrency")
// probe flags, get from httprobe
var probes probeArgs
flag.Var(&probes, "p", "add additional probe (proto:port)")
// skip default probes flag, get from httprobe
var skipDefault bool
flag.BoolVar(&skipDefault, "s", false, "skip the default probes (http:80 and https:443)")
// Time out flag
var to int
flag.IntVar(&to, "t", 9, "Timeout (seconds)")
// Input file flag
var inputFile string
flag.StringVar(&inputFile, "i", "-", "Input file (default is stdin)")
var sameLinePorts bool
flag.BoolVar(&sameLinePorts, "l", false, "Use ports in the same line (google.com,2087,2086)")
var cidrInput bool
flag.BoolVar(&cidrInput, "cidr", false, "Generate IP addresses from CIDR")
// Get an idea from httprobe written by @tomnomnom
var preferHTTPS bool
flag.BoolVar(&preferHTTPS, "prefer-https", false, "oOnly try plain HTTP if HTTPS fails")
var detectSimilarSites bool
flag.BoolVar(&detectSimilarSites, "detect-similar", false, "Detect similar sites (careful when using this, this just using headers and cookies to generation hash)")
var verbose bool
flag.BoolVar(&verbose, "v", false, "Turn on verbose")
var debug bool
flag.BoolVar(&debug, "d", false, "Turn on debug")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "AUTHOR: @thebl4ckturtle - github.com/theblackturtle\n\nUsage of fprobe:\n")
flag.PrintDefaults()
}
flag.Parse()
timeout = time.Duration(to) * time.Second
if detectSimilarSites {
similarDetector = NewSimilar()
}
var wg sync.WaitGroup
var workingPool *ants.PoolWithFunc
workingPool, err := ants.NewPoolWithFunc(concurrency, func(i interface{}) {
defer wg.Done()
t := i.(Task)
success, v, err := isWorking(t.String(), verbose)
if success {
if !verbose {
fmt.Println(t.String())
} else {
if vj, err := jsoniter.MarshalToString(v); err == nil {
fmt.Println(vj)
}
}
if (preferHTTPS && t.Scheme == "https") || t.Scheme == "http" {
return
}
wg.Add(1)
workingPool.Invoke(Task{
Scheme: "http",
Url: t.Url,
})
} else {
if debug {
fmt.Fprintf(os.Stderr, "[DEBUG] %s: %s\n", t.String(), err)
}
}
}, ants.WithPreAlloc(true))
if err != nil {
fmt.Println("Failed to create working pool")
os.Exit(1)
}
defer workingPool.Release()
var sc *bufio.Scanner
if inputFile == "" {
fmt.Fprintln(os.Stderr, "Please check your input again")
os.Exit(1)
}
if inputFile == "-" {
sc = bufio.NewScanner(os.Stdin)
} else {
f, err := os.Open(inputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Error when open input file: %s\n", err)
os.Exit(1)
}
defer f.Close()
sc = bufio.NewScanner(f)
}
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
if err := sc.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error reading input file: %s\n", err)
break
}
if line == "" {
continue
}
if cidrInput {
c, err := cidr.ParseCIDR(line)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse input as CIDR: %s\n", err)
continue
}
if err := c.ForEachIP(func(ip string) error {
wg.Add(1)
_ = workingPool.Invoke(Task{
Scheme: "https",
Url: ip,
})
return nil
}); err != nil {
fmt.Fprintf(os.Stderr, "Failed to parse input as CIDR: %s\n", err)
}
continue
}
if sameLinePorts {
lineArgs := strings.Split(line, ",")
if len(lineArgs) < 2 {
continue
}
d, ports := lineArgs[0], lineArgs[1:]
for _, port := range ports {
if port := strings.TrimSpace(port); port != "" {
wg.Add(1)
_ = workingPool.Invoke(Task{
Scheme: "https",
Url: fmt.Sprintf("%s:%s", d, port),
})
}
}
continue
}
if urlRegex.MatchString(line) {
wg.Add(1)
if strings.HasPrefix(line, "http://") {
_ = workingPool.Invoke(Task{
Scheme: "http",
Url: strings.TrimPrefix(line, "http://"),
})
} else {
_ = workingPool.Invoke(Task{
Scheme: "https",
Url: strings.TrimPrefix(line, "http://"),
})
}
continue
}
if !skipDefault {
wg.Add(1)
_ = workingPool.Invoke(Task{
Scheme: "https",
Url: line,
})
}
for _, p := range probes {
switch p {
case "medium":
for _, port := range portMedium {
wg.Add(1)
_ = workingPool.Invoke(Task{
Scheme: "https",
Url: fmt.Sprintf("%s:%s", line, port),
})
}
case "large":
for _, port := range portLarge {
wg.Add(1)
_ = workingPool.Invoke(Task{
Scheme: "https",
Url: fmt.Sprintf("%s:%s", line, port),
})
}
case "xlarge":
for _, port := range portXlarge {
wg.Add(1)
_ = workingPool.Invoke(Task{
Scheme: "https",
Url: fmt.Sprintf("%s:%s", line, port),
})
}
default:
pair := strings.SplitN(p, ":", 2)
if len(pair) != 2 {
continue
}
wg.Add(1)
if pair[0] == "http" {
_ = workingPool.Invoke(Task{
Scheme: "http",
Url: fmt.Sprintf("%s:%s", line, pair[1]),
})
} else {
_ = workingPool.Invoke(Task{
Scheme: "https",
Url: fmt.Sprintf("%s:%s", line, pair[1]),
})
}
}
}
}
wg.Wait()
}
func isWorking(url string, verbose bool) (bool, *verboseStruct, error) {
var v *verboseStruct
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.SetRequestURI(url)
req.SetConnectionClose()
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
resp.SkipBody = true
err := client.DoTimeout(req, resp, timeout)
if err != nil {
return false, v, err
}
if similarDetector != nil {
var headers []string
resp.Header.VisitAll(func(key, _ []byte) {
headers = append(headers, string(key))
})
sort.Strings(headers)
var cookies []string
resp.Header.VisitAllCookie(func(key, _ []byte) {
cookies = append(cookies, string(key))
})
sort.Strings(cookies)
hash := getHash(headers, cookies)
// Return if this site's hash exists
if !similarDetector.Add(hash) {
return false, v, fmt.Errorf("similar another site")
}
}
if verbose {
server := resp.Header.Peek(fasthttp.HeaderServer)
contentType := resp.Header.Peek(fasthttp.HeaderContentType)
location := resp.Header.Peek(fasthttp.HeaderLocation)
v = &verboseStruct{
Site: url,
StatusCode: resp.StatusCode(),
Server: string(server),
ContentType: string(contentType),
Location: string(location),
}
}
return true, v, nil
}
func getHash(headers, cookies []string) string {
headerHash := strings.Join(headers, ",")
cookieHash := strings.Join(cookies, ",")
hash := fmt.Sprintf("%s,%s", headerHash, cookieHash)
checksum := sha1.Sum([]byte(hash))
return fmt.Sprintf("%x", checksum)
}