-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
executable file
·289 lines (226 loc) · 6.12 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
package main
import (
"crypto/tls"
"flag"
"fmt"
"github.com/fatih/color"
"github.com/gogf/gf/os/grpool"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"sync"
"time"
)
var(
schemas[2] string=[2] string{"http","https"}
hL string
iL string
ip string
host string
h bool
thread int
addressList []string
hostList []string
threadPool *grpool.Pool
tasklist []HostIP
timeout int
port string
ports =[]int{80,443} //默认端口访问
redirect bool
flog *os.File
outfile string
code string
proxy string
)
func init(){
log.SetOutput(ioutil.Discard)
flag.BoolVar(&h, "h", false, "help")
flag.StringVar(&hL,"hL","","load domain list file path")
flag.StringVar(&iL,"iL","","load domain list file path")
flag.StringVar(&port,"port","","port eg:8080,8888,81 or 1-65535 or port")
flag.StringVar(&ip,"ip","","input ip range ,eg:CIDR or ip or ip,ip,ip")
flag.StringVar(&host,"host","","target domain eg:xxx.com")
flag.IntVar(&thread,"thread",5,"thread default 5")
flag.IntVar(&timeout,"timeout",3,"timeout default 3")
flag.BoolVar(&redirect, "redirect", false, "follow 30x redirect")
flag.StringVar(&outfile,"output","hostinfo.txt","output result to file")
flag.StringVar(&code,"code","200","http status code filter options eg:200,201,500 or 200")
flag.StringVar(&proxy,"proxy","","use a proxy to connect to the target URL")
}
type HostIP struct {
Schema string
Address string
Host string
Port int
}
func HostVerify(hostip HostIP) {
var target string
var titleinf string=""
tr := &http.Transport{
Dial: (&net.Dialer{
//解决连接过多出现err:too many open file.
// https://colobu.com/2016/07/01/the-complete-guide-to-golang-net-http-timeouts/
// http://craigwickesser.com/2015/01/golang-http-to-many-open-files/
Timeout: time.Duration(timeout) * time.Second,
Deadline: time.Now().Add(time.Duration(timeout) * time.Second),
KeepAlive: time.Duration(timeout) * time.Second,
}).Dial,
TLSHandshakeTimeout:time.Duration(timeout)* time.Second,
//忽略证书校验
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
//设置代理
if proxy!=""{
tr.Proxy=getProxy(proxy)
}
client:=&http.Client{
Timeout:time.Duration(timeout)*time.Second,
Transport:tr,
}
//关闭redirect
if !redirect {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
if hostip.Port==443{
log.Printf("%s,%d\n",hostip.Schema,port)
target=fmt.Sprintf("https://%s",hostip.Address)
}else if hostip.Port==80{
log.Printf("%s,%d",hostip.Schema,port)
target=fmt.Sprintf("http://%s",hostip.Address)
}else{
target=fmt.Sprintf("%s://%s:%d",hostip.Schema,hostip.Address,hostip.Port)
}
log.Printf("请求URL:%s\n",target)
req,err:=http.NewRequest(http.MethodGet,target,nil)
if err!=nil{
log.Printf("error:%s\n",err)
return
}
//set host
req.Host=hostip.Host
req.Header.Set("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36")
req.Header.Set("Connection","close") // 解决 too many open files
resp,err:=client.Do(req)
if err!=nil{
log.Printf("error:%s\n",err)
return
}
defer resp.Body.Close()
banner:=resp.Header.Get("Server")
//contentlength := resp.Header.Get("Content-Length")
x_power_by:=resp.Header.Get("X-Powered-By")
contenttype:=""
pair := strings.SplitN(resp.Header.Get("Content-Type"), ";", 2)
if len(pair) == 2 {
contenttype= pair[0]
}
body,err:=ioutil.ReadAll(resp.Body)
if err!=nil{
log.Printf("error:%s\n",err)
return
}
re:=regexp.MustCompile("<title>(.*)<\\/title>")
title:=re.FindSubmatch(body)
if len(title)>0{
titleinf=string(title[len(title)-1])
if titleinf ==""{
titleinf="空标题"
log.Printf("%s\n",title)
}
}
urlout:=fmt.Sprintf("%s://%s:%d-%s",hostip.Schema,hostip.Host,hostip.Port,hostip.Address)
//out:=fmt.Sprintf("%-70s%-20s%-63s%-9s%-10s%-5s%-5s\n",urlout,status,titleinf,banner,x_power_by,contentlength,contenttype)
out:=fmt.Sprintf("%-5d %-15d %-60s %-35s %-15s %-50s %s\n",resp.StatusCode, len(body) ,banner, x_power_by, contenttype,urlout, titleinf)
var d *color.Color
codeStr:=strconv.Itoa(resp.StatusCode)
if strings.Contains(code,codeStr){
if strings.HasPrefix(codeStr,"2"){
d=color.New(color.FgHiGreen,color.Bold)
}else if strings.HasPrefix(codeStr,"3"){
d=color.New(color.FgCyan,color.Bold)
}else{
d=color.New(color.FgHiRed,color.Bold)
}
d.Printf(out)
flog.WriteString(out)
}
}
func getProxy(proxy string)func(*http.Request) (*url.URL, error) {
proxyurl,err:=url.Parse(proxy)
if err!=nil{
fmt.Println("proxy url parse fail:%s",proxyurl)
os.Exit(0)
}
return http.ProxyURL(proxyurl)
}
//所有协议 ip 和 host组合成任务列表
func MakeTask(hostList []string , addressList []string){
for _,host :=range hostList {
for _,addr:=range addressList{
for _,schema:=range schemas{
for _,port :=range ports{
tasklist=append(tasklist, HostIP{schema,addr,host,port})
}
}
}
}
}
//多线程扫描
func HostScan(){
wg:=sync.WaitGroup{}
for _,task :=range tasklist{
wg.Add(1)
v:=task
threadPool.Add(func() {
HostVerify(v)
wg.Done()
})
}
wg.Wait()
}
func main() {
flag.Parse()
if h{
flag.Usage()
}
flog, _ = os.OpenFile(outfile, os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_TRUNC,0755)//打印输出到文件
//加载host列表
if hL!=""{
hostList=SliceStringUnique(append(hostList,ListReader(hL,"host")...))
}
//加载adderss列表
if iL!=""{
addressList=SliceStringUnique(append(addressList,ListReader(iL,"ip")...))
}
//加载用户输入的IP段或者IP
if ip!=""{
addressList=SliceStringUnique(append(addressList,IPParse(ip)...))
}
if port!=""{
portNumbers,err:=PortParse(port)
if err!=nil{
fmt.Println(err)
return
}
ports=SliceIntUnique(append(ports,portNumbers...))
}
//加载用户输入的host
if host!=""{
hostList=SliceStringUnique(append(hostList,host))
}
log.Printf("%v\n",addressList)
log.Printf("%v\n",hostList)
log.Printf("%v\n",ports)
MakeTask(hostList,addressList) //加载任务列表
//设置线程数
threadPool=grpool.New(thread)
HostScan();
}