From 8ca4d2c89aef3d74627fa9ca8a0d56ff18a780a5 Mon Sep 17 00:00:00 2001 From: shadow1ng Date: Thu, 3 Dec 2020 15:55:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96ip=E6=AE=B5=E5=A4=84=E7=90=86?= =?UTF-8?q?=E6=A8=A1=E5=9D=97,=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81192.168.?= =?UTF-8?q?1.1-192.168.255.255?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + common/ParseIP.go | 244 +++++++++++++++++++++++++--------------------- 2 files changed, 136 insertions(+), 109 deletions(-) diff --git a/README.md b/README.md index 1447096..b83d35a 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ 因为用习惯了f-scrack,习惯一条命令跑完所有模块,省去一个个模块单独调用的时间,当然我附加了-m 指定模块的功能。 ## 最近更新 +[+] 2020/12/03 优化ip段处理模块,新增支持192.168.1.1-192.168.255.255 [+] 2020/11/17 增加-ping 参数,作用是存活探测模块用ping代替icmp发包。 [+] 2020/11/17 增加WebScan模块,新增shiro简单识别。https访问时,跳过证书认证。将服务模块和web模块的超时分开,增加-wt 参数(WebTimeout)。 [+] 2020/11/16 对icmp模块进行优化,增加-it 参数(IcmpThreads),默认11000,适合扫B段 diff --git a/common/ParseIP.go b/common/ParseIP.go index 691c044..3554cfa 100644 --- a/common/ParseIP.go +++ b/common/ParseIP.go @@ -11,162 +11,190 @@ import ( "strings" ) -var ParseIPErr =errors.New("host parsing error\n" + - "format: \n"+ +var ParseIPErr = errors.New(" host parsing error\n" + + "format: \n" + "192.168.1.1\n" + - "192.168.1.1/8\n"+ - "192.168.1.1/16\n"+ - "192.168.1.1/24\n"+ + "192.168.1.1/8\n" + + "192.168.1.1/16\n" + + "192.168.1.1/24\n" + "192.168.1.1,192.168.1.2\n" + + "192.168.1.1-192.168.255.255\n" + "192.168.1.1-255") -func ParseIP(ip string,filename string)(hosts []string,err error){ +func ParseIP(ip string, filename string) (hosts []string, err error) { - if ip != ""{ - hosts,err = ParseIPs(ip) + if ip != "" { + hosts, err = ParseIPs(ip) } - if filename != ""{ + if filename != "" { var filehost []string - filehost,_ = Readipfile(filename) - hosts = append(hosts,filehost...) + filehost, _ = Readipfile(filename) + hosts = append(hosts, filehost...) } hosts = RemoveDuplicate(hosts) - return hosts,err + return hosts, err } -func ParseIPs(ip string)(hosts []string,err error){ - if strings.Contains(ip,","){ - IPList:=strings.Split(ip,",") +func ParseIPs(ip string) (hosts []string, err error) { + if strings.Contains(ip, ",") { + IPList := strings.Split(ip, ",") var ips []string - for _,ip:=range IPList{ - ips,err = ParseIPone(ip) - CheckErr(ip,err) - hosts = append(hosts,ips...) + for _, ip := range IPList { + ips, err = ParseIPone(ip) + CheckErr(ip, err) + hosts = append(hosts, ips...) } - return hosts,err - }else { - hosts,err = ParseIPone(ip) - CheckErr(ip,err) - return hosts,err + return hosts, err + } else { + hosts, err = ParseIPone(ip) + CheckErr(ip, err) + return hosts, err } } -func ParseIPone(ip string)([]string,error){ - reg:=regexp.MustCompile(`[a-zA-Z]+`) +func ParseIPone(ip string) ([]string, error) { + reg := regexp.MustCompile(`[a-zA-Z]+`) switch { - case strings.Contains(ip[len(ip)-3:len(ip)],"/24"): + case strings.Contains(ip[len(ip)-3:len(ip)], "/24"): return ParseIPA(ip) - case strings.Contains(ip[len(ip)-3:len(ip)],"/16"): + case strings.Contains(ip[len(ip)-3:len(ip)], "/16"): return ParseIPD(ip) - case strings.Contains(ip[len(ip)-2:len(ip)],"/8"): + case strings.Contains(ip[len(ip)-2:len(ip)], "/8"): return ParseIPE(ip) - case strings.Count(ip,"-")==1: + case strings.Count(ip, "-") == 1: return ParseIPC(ip) case reg.MatchString(ip): _, err := net.LookupHost(ip) if err != nil { - return nil,err + return nil, err } - return []string{ip},nil + return []string{ip}, nil default: - testIP:=net.ParseIP(ip) - if testIP==nil{ - return nil,ParseIPErr + testIP := net.ParseIP(ip) + if testIP == nil { + return nil, ParseIPErr } - return []string{ip},nil + return []string{ip}, nil } } -//Parsing CIDR IP -func ParseIPA(ip string)([]string,error){ - realIP:=ip[:len(ip)-3] - testIP:=net.ParseIP(realIP) - if testIP==nil{ - return nil,ParseIPErr +//Parsing CIDR IP +func ParseIPA(ip string) ([]string, error) { + realIP := ip[:len(ip)-3] + testIP := net.ParseIP(realIP) + + if testIP == nil { + return nil, ParseIPErr } - IPrange:=strings.Join(strings.Split(realIP,".")[0:3],".") + IPrange := strings.Join(strings.Split(realIP, ".")[0:3], ".") var AllIP []string - for i:=0;i<=255;i++{ - AllIP=append(AllIP,IPrange+"."+strconv.Itoa(i)) + for i := 0; i <= 255; i++ { + AllIP = append(AllIP, IPrange+"."+strconv.Itoa(i)) } - return AllIP,nil + return AllIP, nil } //Resolving multiple IPS, for example: 192.168.111.1,192.168.111.2 -func ParseIPB(ip string)([]string,error){ - IPList:=strings.Split(ip,",") - for _,i:=range IPList{ - testIP:=net.ParseIP(i) - if testIP==nil{ - return nil,ParseIPErr +func ParseIPB(ip string) ([]string, error) { + IPList := strings.Split(ip, ",") + for _, i := range IPList { + testIP := net.ParseIP(i) + if testIP == nil { + return nil, ParseIPErr } } - return IPList,nil + return IPList, nil } -//Resolving a range of IP,for example: 192.168.111.1-255 -func ParseIPC(ip string)([]string,error){ - IPRange:=strings.Split(ip,"-") - testIP:=net.ParseIP(IPRange[0]) - Range,err:=strconv.Atoi(IPRange[1]) - if testIP==nil || Range>255 || err!=nil{ - return nil,ParseIPErr - } - SplitIP:=strings.Split(IPRange[0],".") - ip1,err1:=strconv.Atoi(SplitIP[3]) - ip2,err2:=strconv.Atoi(IPRange[1]) - PrefixIP:=strings.Join(SplitIP[0:3],".") +//Resolving a range of IP,for example: 192.168.111.1-255,192.168.111.1-192.168.112.255 +func ParseIPC(ip string) ([]string, error) { + IPRange := strings.Split(ip, "-") + testIP := net.ParseIP(IPRange[0]) var AllIP []string - if ip1>ip2 || err1!=nil || err2!=nil{ - return nil,ParseIPErr - } - for i:=ip1;i<=ip2;i++{ - AllIP=append(AllIP,PrefixIP+"."+strconv.Itoa(i)) - } - return AllIP,nil - -} - -func ParseIPD(ip string)([]string,error){ - realIP:=ip[:len(ip)-3] - testIP:=net.ParseIP(realIP) - - if testIP==nil{ - return nil,ParseIPErr - } - IPrange:=strings.Join(strings.Split(realIP,".")[0:2],".") - var AllIP []string - for a:=0;a<=255;a++{ - for b:=0;b<=255;b++{ - AllIP=append(AllIP,IPrange+"."+strconv.Itoa(a)+"."+strconv.Itoa(b)) + if len(IPRange[1]) < 4 { + Range, err := strconv.Atoi(IPRange[1]) + if testIP == nil || Range > 255 || err != nil { + return nil, ParseIPErr + } + SplitIP := strings.Split(IPRange[0], ".") + ip1, err1 := strconv.Atoi(SplitIP[3]) + ip2, err2 := strconv.Atoi(IPRange[1]) + PrefixIP := strings.Join(SplitIP[0:3], ".") + if ip1 > ip2 || err1 != nil || err2 != nil { + return nil, ParseIPErr + } + for i := ip1; i <= ip2; i++ { + AllIP = append(AllIP, PrefixIP+"."+strconv.Itoa(i)) + } + } else { + SplitIP1 := strings.Split(IPRange[0], ".") + SplitIP2 := strings.Split(IPRange[1], ".") + fmt.Println(SplitIP1, SplitIP2, len(SplitIP1), len(SplitIP2)) + if len(SplitIP1) != 4 || len(SplitIP2) != 4 { + return nil, ParseIPErr + } + start, end := [4]int{}, [4]int{} + for i := 0; i < 4; i++ { + ip1, err1 := strconv.Atoi(SplitIP1[i]) + ip2, err2 := strconv.Atoi(SplitIP2[i]) + if ip1 > ip2 || err1 != nil || err2 != nil { + return nil, ParseIPErr + } + start[i], end[i] = ip1, ip2 + } + startNum := (start[0]<<24 | start[1]<<16 | start[2]<<8 | start[3]) + endNum := (end[0]<<24 | end[1]<<16 | end[2]<<8 | end[3]) + fmt.Println(startNum, endNum) + for num := startNum; num < endNum; num++ { + ip := (strconv.Itoa((num>>24)&0xff) + "." + strconv.Itoa((num>>16)&0xff) + "." + strconv.Itoa((num>>8)&0xff) + "." + strconv.Itoa((num)&0xff)) + AllIP = append(AllIP, ip) } } - return AllIP,nil + + return AllIP, nil + } -func ParseIPE(ip string)([]string,error){ - realIP:=ip[:len(ip)-2] - testIP:=net.ParseIP(realIP) +func ParseIPD(ip string) ([]string, error) { + realIP := ip[:len(ip)-3] + testIP := net.ParseIP(realIP) - if testIP==nil{ - return nil,ParseIPErr + if testIP == nil { + return nil, ParseIPErr } - IPrange:=strings.Join(strings.Split(realIP,".")[0:1],".") + IPrange := strings.Join(strings.Split(realIP, ".")[0:2], ".") var AllIP []string - for a:=0;a<=255;a++{ - for b:=0;b<=255;b++{ - AllIP=append(AllIP,IPrange+"."+strconv.Itoa(a)+"."+strconv.Itoa(b)+"."+strconv.Itoa(1)) - AllIP=append(AllIP,IPrange+"."+strconv.Itoa(a)+"."+strconv.Itoa(b)+"."+strconv.Itoa(254)) + for a := 0; a <= 255; a++ { + for b := 0; b <= 255; b++ { + AllIP = append(AllIP, IPrange+"."+strconv.Itoa(a)+"."+strconv.Itoa(b)) } } - return AllIP,nil + return AllIP, nil } -func Readipfile(filename string)([]string,error){ +func ParseIPE(ip string) ([]string, error) { + realIP := ip[:len(ip)-2] + testIP := net.ParseIP(realIP) + + if testIP == nil { + return nil, ParseIPErr + } + IPrange := strings.Join(strings.Split(realIP, ".")[0:1], ".") + var AllIP []string + for a := 0; a <= 255; a++ { + for b := 0; b <= 255; b++ { + AllIP = append(AllIP, IPrange+"."+strconv.Itoa(a)+"."+strconv.Itoa(b)+"."+strconv.Itoa(1)) + AllIP = append(AllIP, IPrange+"."+strconv.Itoa(a)+"."+strconv.Itoa(b)+"."+strconv.Itoa(254)) + } + } + return AllIP, nil +} + +func Readipfile(filename string) ([]string, error) { file, err := os.Open(filename) - if err!=nil{ - fmt.Println("Open %s error, %v", filename,err) + if err != nil { + fmt.Println("Open %s error, %v", filename, err) os.Exit(0) } defer file.Close() @@ -176,16 +204,15 @@ func Readipfile(filename string)([]string,error){ for scanner.Scan() { text := strings.TrimSpace(scanner.Text()) if text != "" { - host,err := ParseIPs(text) - CheckErr(text,err) - content=append(content,host...) + host, err := ParseIPs(text) + CheckErr(text, err) + content = append(content, host...) } } - return content,nil + return content, nil } - -func RemoveDuplicate(old []string) ([]string) { +func RemoveDuplicate(old []string) []string { result := make([]string, 0, len(old)) temp := map[string]struct{}{} for _, item := range old { @@ -196,4 +223,3 @@ func RemoveDuplicate(old []string) ([]string) { } return result } -