forked from tomnomnom/httprobe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
209 lines (168 loc) · 4.24 KB
/
Copy pathmain.go
File metadata and controls
209 lines (168 loc) · 4.24 KB
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
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"strings"
"sync"
"time"
)
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, ",")
}
func usage() {
fmt.Println("Usage: cat tcp-ipp.csv | httprobe")
fmt.Println("")
fmt.Println("Get all HTTP(S) URLs based on input from ipp file. Outputs lines in format <proto>:<host>:<port>.")
fmt.Println("See github.com/tomnomnom/httprobe for original script.")
fmt.Println("")
flag.PrintDefaults()
}
// Confirms HTTP is listening, doesn't care about virtual hosts
// Accepts <ipp.csv> file
func main() {
flag.Usage = usage
// concurrency flag
var concurrency int
flag.IntVar(&concurrency, "c", 20, "set the concurrency level (split equally between HTTPS and HTTP requests)")
// timeout flag
var to int
flag.IntVar(&to, "t", 10000, "timeout (milliseconds)")
// prefer https
var preferHTTPS bool
flag.BoolVar(&preferHTTPS, "prefer-https", false, "only try plain HTTP if HTTPS fails")
// HTTP method to use
var method string
flag.StringVar(&method, "method", "GET", "HTTP method to use")
flag.Parse()
// make an actual time.Duration out of the timeout
timeout := time.Duration(to * 1000000)
var tr = &http.Transport{
MaxIdleConns: 30,
IdleConnTimeout: time.Second,
DisableKeepAlives: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DialContext: (&net.Dialer{
Timeout: timeout,
KeepAlive: time.Second,
}).DialContext,
}
re := func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
client := &http.Client{
Transport: tr,
CheckRedirect: re,
Timeout: timeout,
}
// domain/port pairs are initially sent on the httpsURLs channel.
// If they are listening and the --prefer-https flag is set then
// no HTTP check is performed; otherwise they're put onto the httpURLs
// channel for an HTTP check.
httpsURLs := make(chan string)
httpURLs := make(chan string)
output := make(chan string)
// HTTPS workers
var httpsWG sync.WaitGroup
for i := 0; i < concurrency/2; i++ {
httpsWG.Add(1)
go func() {
for url := range httpsURLs {
// always try HTTPS first
withProto := "https://" + url
if isListening(client, withProto, method) {
output <- withProto
// skip trying HTTP if --prefer-https is set
if preferHTTPS {
continue
}
}
httpURLs <- url
}
httpsWG.Done()
}()
}
// HTTP workers
var httpWG sync.WaitGroup
for i := 0; i < concurrency/2; i++ {
httpWG.Add(1)
go func() {
for url := range httpURLs {
withProto := "http://" + url
if isListening(client, withProto, method) {
output <- withProto
continue
}
}
httpWG.Done()
}()
}
// Close the httpURLs channel when the HTTPS workers are done
go func() {
httpsWG.Wait()
close(httpURLs)
}()
// Output worker
var outputWG sync.WaitGroup
outputWG.Add(1)
go func() {
for o := range output {
fmt.Println(o)
}
outputWG.Done()
}()
// Close the output channel when the HTTP workers are done
go func() {
httpWG.Wait()
close(output)
}()
// accept domains on stdin
sc := bufio.NewScanner(os.Stdin)
for sc.Scan() {
line := strings.Split(strings.ToLower(sc.Text()), ",")
host := line[0]
// TODO add HTTP only option?
for _, port := range line[1:] {
// HTTP only is not supported now
httpsURLs <- fmt.Sprintf("%s:%s", host, port)
}
}
// once we've sent all the URLs off we can close the
// input/httpsURLs channel. The workers will finish what they're
// doing and then call 'Done' on the WaitGroup
close(httpsURLs)
// check there were no errors reading stdin (unlikely)
if err := sc.Err(); err != nil {
fmt.Fprintf(os.Stderr, "failed to read input: %s\n", err)
}
// Wait until the output waitgroup is done
outputWG.Wait()
}
func isListening(client *http.Client, url, method string) bool {
req, err := http.NewRequest(method, url, nil)
if err != nil {
return false
}
req.Header.Add("Connection", "close")
req.Close = true
resp, err := client.Do(req)
if resp != nil {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
if err != nil {
return false
}
return true
}