优化ip段处理模块,新增支持192.168.1.1-192.168.255.255

This commit is contained in:
shadow1ng
2020-12-03 15:55:29 +08:00
parent 13a3cacd93
commit 8ca4d2c89a
2 changed files with 136 additions and 109 deletions
+1
View File
@@ -14,6 +14,7 @@
因为用习惯了f-scrack,习惯一条命令跑完所有模块,省去一个个模块单独调用的时间,当然我附加了-m 指定模块的功能。 因为用习惯了f-scrack,习惯一条命令跑完所有模块,省去一个个模块单独调用的时间,当然我附加了-m 指定模块的功能。
## 最近更新 ## 最近更新
[+] 2020/12/03 优化ip段处理模块,新增支持192.168.1.1-192.168.255.255
[+] 2020/11/17 增加-ping 参数,作用是存活探测模块用ping代替icmp发包。 [+] 2020/11/17 增加-ping 参数,作用是存活探测模块用ping代替icmp发包。
[+] 2020/11/17 增加WebScan模块,新增shiro简单识别。https访问时,跳过证书认证。将服务模块和web模块的超时分开,增加-wt 参数(WebTimeout)。 [+] 2020/11/17 增加WebScan模块,新增shiro简单识别。https访问时,跳过证书认证。将服务模块和web模块的超时分开,增加-wt 参数(WebTimeout)。
[+] 2020/11/16 对icmp模块进行优化,增加-it 参数(IcmpThreads),默认11000,适合扫B段 [+] 2020/11/16 对icmp模块进行优化,增加-it 参数(IcmpThreads),默认11000,适合扫B段
+135 -109
View File
@@ -11,162 +11,190 @@ import (
"strings" "strings"
) )
var ParseIPErr =errors.New("host parsing error\n" + var ParseIPErr = errors.New(" host parsing error\n" +
"format: \n"+ "format: \n" +
"192.168.1.1\n" + "192.168.1.1\n" +
"192.168.1.1/8\n"+ "192.168.1.1/8\n" +
"192.168.1.1/16\n"+ "192.168.1.1/16\n" +
"192.168.1.1/24\n"+ "192.168.1.1/24\n" +
"192.168.1.1,192.168.1.2\n" + "192.168.1.1,192.168.1.2\n" +
"192.168.1.1-192.168.255.255\n" +
"192.168.1.1-255") "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 != ""{ if ip != "" {
hosts,err = ParseIPs(ip) hosts, err = ParseIPs(ip)
} }
if filename != ""{ if filename != "" {
var filehost []string var filehost []string
filehost,_ = Readipfile(filename) filehost, _ = Readipfile(filename)
hosts = append(hosts,filehost...) hosts = append(hosts, filehost...)
} }
hosts = RemoveDuplicate(hosts) hosts = RemoveDuplicate(hosts)
return hosts,err return hosts, err
} }
func ParseIPs(ip string)(hosts []string,err error){ func ParseIPs(ip string) (hosts []string, err error) {
if strings.Contains(ip,","){ if strings.Contains(ip, ",") {
IPList:=strings.Split(ip,",") IPList := strings.Split(ip, ",")
var ips []string var ips []string
for _,ip:=range IPList{ for _, ip := range IPList {
ips,err = ParseIPone(ip) ips, err = ParseIPone(ip)
CheckErr(ip,err) CheckErr(ip, err)
hosts = append(hosts,ips...) hosts = append(hosts, ips...)
} }
return hosts,err return hosts, err
}else { } else {
hosts,err = ParseIPone(ip) hosts, err = ParseIPone(ip)
CheckErr(ip,err) CheckErr(ip, err)
return hosts,err return hosts, err
} }
} }
func ParseIPone(ip string)([]string,error){ func ParseIPone(ip string) ([]string, error) {
reg:=regexp.MustCompile(`[a-zA-Z]+`) reg := regexp.MustCompile(`[a-zA-Z]+`)
switch { switch {
case strings.Contains(ip[len(ip)-3:len(ip)],"/24"): case strings.Contains(ip[len(ip)-3:len(ip)], "/24"):
return ParseIPA(ip) 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) 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) return ParseIPE(ip)
case strings.Count(ip,"-")==1: case strings.Count(ip, "-") == 1:
return ParseIPC(ip) return ParseIPC(ip)
case reg.MatchString(ip): case reg.MatchString(ip):
_, err := net.LookupHost(ip) _, err := net.LookupHost(ip)
if err != nil { if err != nil {
return nil,err return nil, err
} }
return []string{ip},nil return []string{ip}, nil
default: default:
testIP:=net.ParseIP(ip) testIP := net.ParseIP(ip)
if testIP==nil{ if testIP == nil {
return nil,ParseIPErr 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{ //Parsing CIDR IP
return nil,ParseIPErr 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 var AllIP []string
for i:=0;i<=255;i++{ for i := 0; i <= 255; i++ {
AllIP=append(AllIP,IPrange+"."+strconv.Itoa(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 //Resolving multiple IPS, for example: 192.168.111.1,192.168.111.2
func ParseIPB(ip string)([]string,error){ func ParseIPB(ip string) ([]string, error) {
IPList:=strings.Split(ip,",") IPList := strings.Split(ip, ",")
for _,i:=range IPList{ for _, i := range IPList {
testIP:=net.ParseIP(i) testIP := net.ParseIP(i)
if testIP==nil{ if testIP == nil {
return nil,ParseIPErr return nil, ParseIPErr
} }
} }
return IPList,nil return IPList, nil
} }
//Resolving a range of IP,for example: 192.168.111.1-255 //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){ func ParseIPC(ip string) ([]string, error) {
IPRange:=strings.Split(ip,"-") IPRange := strings.Split(ip, "-")
testIP:=net.ParseIP(IPRange[0]) 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],".")
var AllIP []string var AllIP []string
if ip1>ip2 || err1!=nil || err2!=nil{ if len(IPRange[1]) < 4 {
return nil,ParseIPErr Range, err := strconv.Atoi(IPRange[1])
} if testIP == nil || Range > 255 || err != nil {
for i:=ip1;i<=ip2;i++{ return nil, ParseIPErr
AllIP=append(AllIP,PrefixIP+"."+strconv.Itoa(i)) }
} SplitIP := strings.Split(IPRange[0], ".")
return AllIP,nil 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 {
func ParseIPD(ip string)([]string,error){ return nil, ParseIPErr
realIP:=ip[:len(ip)-3] }
testIP:=net.ParseIP(realIP) for i := ip1; i <= ip2; i++ {
AllIP = append(AllIP, PrefixIP+"."+strconv.Itoa(i))
if testIP==nil{ }
return nil,ParseIPErr } else {
} SplitIP1 := strings.Split(IPRange[0], ".")
IPrange:=strings.Join(strings.Split(realIP,".")[0:2],".") SplitIP2 := strings.Split(IPRange[1], ".")
var AllIP []string fmt.Println(SplitIP1, SplitIP2, len(SplitIP1), len(SplitIP2))
for a:=0;a<=255;a++{ if len(SplitIP1) != 4 || len(SplitIP2) != 4 {
for b:=0;b<=255;b++{ return nil, ParseIPErr
AllIP=append(AllIP,IPrange+"."+strconv.Itoa(a)+"."+strconv.Itoa(b)) }
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){ func ParseIPD(ip string) ([]string, error) {
realIP:=ip[:len(ip)-2] realIP := ip[:len(ip)-3]
testIP:=net.ParseIP(realIP) testIP := net.ParseIP(realIP)
if testIP==nil{ if testIP == nil {
return nil,ParseIPErr return nil, ParseIPErr
} }
IPrange:=strings.Join(strings.Split(realIP,".")[0:1],".") IPrange := strings.Join(strings.Split(realIP, ".")[0:2], ".")
var AllIP []string var AllIP []string
for a:=0;a<=255;a++{ for a := 0; a <= 255; a++ {
for b:=0;b<=255;b++{ 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))
AllIP=append(AllIP,IPrange+"."+strconv.Itoa(a)+"."+strconv.Itoa(b)+"."+strconv.Itoa(254))
} }
} }
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) file, err := os.Open(filename)
if err!=nil{ if err != nil {
fmt.Println("Open %s error, %v", filename,err) fmt.Println("Open %s error, %v", filename, err)
os.Exit(0) os.Exit(0)
} }
defer file.Close() defer file.Close()
@@ -176,16 +204,15 @@ func Readipfile(filename string)([]string,error){
for scanner.Scan() { for scanner.Scan() {
text := strings.TrimSpace(scanner.Text()) text := strings.TrimSpace(scanner.Text())
if text != "" { if text != "" {
host,err := ParseIPs(text) host, err := ParseIPs(text)
CheckErr(text,err) CheckErr(text, err)
content=append(content,host...) 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)) result := make([]string, 0, len(old))
temp := map[string]struct{}{} temp := map[string]struct{}{}
for _, item := range old { for _, item := range old {
@@ -196,4 +223,3 @@ func RemoveDuplicate(old []string) ([]string) {
} }
return result return result
} }