mirror of
https://github.com/shadow1ng/fscan.git
synced 2026-09-22 03:10:42 +08:00
优化ip解析模块
This commit is contained in:
+6
-2
@@ -118,7 +118,7 @@ func ParseInput(Info *HostInfo) {
|
||||
IsSave = false
|
||||
}
|
||||
if Info.Ports == DefaultPorts {
|
||||
Info.Ports += Webport
|
||||
Info.Ports += "," + Webport
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,14 +130,18 @@ func ParseScantype(Info *HostInfo) {
|
||||
if Info.Scantype != "all" {
|
||||
if Info.Ports == DefaultPorts {
|
||||
switch Info.Scantype {
|
||||
case "wmi":
|
||||
Info.Ports = "135"
|
||||
case "web":
|
||||
Info.Ports = Webport
|
||||
case "ms17010":
|
||||
Info.Ports = "445"
|
||||
case "cve20200796":
|
||||
Info.Ports = "445"
|
||||
case "smb2":
|
||||
Info.Ports = "445"
|
||||
case "portscan":
|
||||
Info.Ports = DefaultPorts
|
||||
Info.Ports = DefaultPorts + "," + Webport
|
||||
case "main":
|
||||
Info.Ports = DefaultPorts
|
||||
default:
|
||||
|
||||
+69
-97
@@ -22,120 +22,113 @@ var ParseIPErr = errors.New(" host parsing error\n" +
|
||||
"192.168.1.1-192.168.255.255\n" +
|
||||
"192.168.1.1-255")
|
||||
|
||||
func ParseIP(ip string, filename string, nohost string) (hosts []string, err error) {
|
||||
if ip != "" {
|
||||
hosts = ParseIPs(ip, true)
|
||||
func ParseIP(host string, filename string, nohosts ...string) (hosts []string, err error) {
|
||||
if host == "" {
|
||||
return
|
||||
}
|
||||
hosts = ParseIPs(host)
|
||||
if filename != "" {
|
||||
var filehost []string
|
||||
filehost, _ = Readipfile(filename)
|
||||
hosts = append(hosts, filehost...)
|
||||
}
|
||||
|
||||
if nohost != "" {
|
||||
nohosts := ParseIPs(nohost, true)
|
||||
if len(nohosts) > 0 {
|
||||
temp := map[string]struct{}{}
|
||||
for _, host := range hosts {
|
||||
temp[host] = struct{}{}
|
||||
}
|
||||
if len(nohosts) > 0 {
|
||||
nohost := nohosts[0]
|
||||
if nohost != "" {
|
||||
nohosts := ParseIPs(nohost)
|
||||
if len(nohosts) > 0 {
|
||||
temp := map[string]struct{}{}
|
||||
for _, host := range hosts {
|
||||
temp[host] = struct{}{}
|
||||
}
|
||||
|
||||
for _, host := range nohosts {
|
||||
delete(temp, host)
|
||||
}
|
||||
for _, host := range nohosts {
|
||||
delete(temp, host)
|
||||
}
|
||||
|
||||
var newDatas []string
|
||||
for host, _ := range temp {
|
||||
newDatas = append(newDatas, host)
|
||||
var newDatas []string
|
||||
for host := range temp {
|
||||
newDatas = append(newDatas, host)
|
||||
}
|
||||
hosts = newDatas
|
||||
sort.Strings(hosts)
|
||||
}
|
||||
hosts = newDatas
|
||||
sort.Strings(hosts)
|
||||
}
|
||||
}
|
||||
hosts = RemoveDuplicate(hosts)
|
||||
return hosts, err
|
||||
if len(hosts) == 0 {
|
||||
err = ParseIPErr
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ParseIPs(ip string, flag ...bool) (hosts []string) {
|
||||
var err error
|
||||
var flag1 bool
|
||||
if len(flag) > 0 {
|
||||
flag1 = flag[0]
|
||||
}
|
||||
func ParseIPs(ip string) (hosts []string) {
|
||||
if strings.Contains(ip, ",") {
|
||||
IPList := strings.Split(ip, ",")
|
||||
var ips []string
|
||||
for _, ip := range IPList {
|
||||
ips, err = ParseIPone(ip)
|
||||
CheckErr(ip, err, flag1)
|
||||
ips = parseIP(ip)
|
||||
hosts = append(hosts, ips...)
|
||||
}
|
||||
} else {
|
||||
hosts, err = ParseIPone(ip)
|
||||
hosts = parseIP(ip)
|
||||
}
|
||||
|
||||
CheckErr(ip, err, flag1)
|
||||
return hosts
|
||||
}
|
||||
|
||||
func ParseIPone(ip string) ([]string, error) {
|
||||
func parseIP(ip string) []string {
|
||||
reg := regexp.MustCompile(`[a-zA-Z]+`)
|
||||
switch {
|
||||
case strings.Contains(ip[len(ip)-3:], "/24"):
|
||||
return ParseIPA(ip)
|
||||
case strings.Contains(ip[len(ip)-3:], "/16"):
|
||||
return ParseIPD(ip)
|
||||
case strings.Contains(ip[len(ip)-2:], "/8"):
|
||||
return ParseIPE(ip)
|
||||
case strings.Count(ip, "-") == 1:
|
||||
return ParseIPC(ip)
|
||||
//解析 /24 /16 /8 /xxx 等
|
||||
case strings.Contains(ip, "/"):
|
||||
return parseIP2(ip)
|
||||
//192.168.1.1-192.168.1.100
|
||||
case strings.Contains(ip, "-"):
|
||||
return parseIP1(ip)
|
||||
//可能是域名,用lookup获取ip
|
||||
case reg.MatchString(ip):
|
||||
_, err := net.LookupHost(ip)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil
|
||||
}
|
||||
return []string{ip}, nil
|
||||
return []string{ip}
|
||||
//处理单个ip
|
||||
default:
|
||||
testIP := net.ParseIP(ip)
|
||||
if testIP == nil {
|
||||
return nil, ParseIPErr
|
||||
return nil
|
||||
}
|
||||
return []string{ip}, nil
|
||||
return []string{ip}
|
||||
}
|
||||
}
|
||||
|
||||
//Parsing CIDR IP
|
||||
func ParseIPA(ip string) ([]string, error) {
|
||||
realIP := ip[:len(ip)-3]
|
||||
testIP := net.ParseIP(realIP)
|
||||
|
||||
if testIP == nil {
|
||||
return nil, ParseIPErr
|
||||
// 把 192.168.x.x/xx 转换成 192.168.x.x-192.168.x.x
|
||||
func parseIP2(host string) (hosts []string) {
|
||||
_, ipNet, err := net.ParseCIDR(host)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
IPrange := strings.Join(strings.Split(realIP, ".")[0:3], ".")
|
||||
var AllIP []string
|
||||
for i := 0; i <= 255; i++ {
|
||||
AllIP = append(AllIP, IPrange+"."+strconv.Itoa(i))
|
||||
}
|
||||
return AllIP, nil
|
||||
hosts = parseIP1(IPRange(ipNet))
|
||||
return
|
||||
}
|
||||
|
||||
//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) {
|
||||
// 解析ip段: 192.168.111.1-255,192.168.111.1-192.168.112.255
|
||||
func parseIP1(ip string) []string {
|
||||
IPRange := strings.Split(ip, "-")
|
||||
testIP := net.ParseIP(IPRange[0])
|
||||
var AllIP []string
|
||||
if len(IPRange[1]) < 4 {
|
||||
Range, err := strconv.Atoi(IPRange[1])
|
||||
if testIP == nil || Range > 255 || err != nil {
|
||||
return nil, ParseIPErr
|
||||
return nil
|
||||
}
|
||||
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
|
||||
return nil
|
||||
}
|
||||
for i := ip1; i <= ip2; i++ {
|
||||
AllIP = append(AllIP, PrefixIP+"."+strconv.Itoa(i))
|
||||
@@ -144,14 +137,14 @@ func ParseIPC(ip string) ([]string, error) {
|
||||
SplitIP1 := strings.Split(IPRange[0], ".")
|
||||
SplitIP2 := strings.Split(IPRange[1], ".")
|
||||
if len(SplitIP1) != 4 || len(SplitIP2) != 4 {
|
||||
return nil, ParseIPErr
|
||||
return nil
|
||||
}
|
||||
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
|
||||
return nil
|
||||
}
|
||||
start[i], end[i] = ip1, ip2
|
||||
}
|
||||
@@ -162,46 +155,24 @@ func ParseIPC(ip string) ([]string, error) {
|
||||
AllIP = append(AllIP, ip)
|
||||
}
|
||||
}
|
||||
|
||||
return AllIP, nil
|
||||
|
||||
return AllIP
|
||||
}
|
||||
|
||||
func ParseIPD(ip string) ([]string, error) {
|
||||
realIP := ip[:len(ip)-3]
|
||||
testIP := net.ParseIP(realIP)
|
||||
|
||||
if testIP == nil {
|
||||
return nil, ParseIPErr
|
||||
// 获取起始IP、结束IP
|
||||
func IPRange(c *net.IPNet) string {
|
||||
start := c.IP.String()
|
||||
mask := c.Mask
|
||||
bcst := make(net.IP, len(c.IP))
|
||||
copy(bcst, c.IP)
|
||||
for i := 0; i < len(mask); i++ {
|
||||
ipIdx := len(bcst) - i - 1
|
||||
bcst[ipIdx] = c.IP[ipIdx] | ^mask[len(mask)-i-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))
|
||||
}
|
||||
}
|
||||
return AllIP, nil
|
||||
}
|
||||
|
||||
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
|
||||
end := bcst.String()
|
||||
return fmt.Sprintf("%s-%s", start, end) //返回用-表示的ip段,192.168.1.0-192.168.255.255
|
||||
}
|
||||
|
||||
// 按行读ip
|
||||
func Readipfile(filename string) ([]string, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
@@ -222,6 +193,7 @@ func Readipfile(filename string) ([]string, error) {
|
||||
return content, nil
|
||||
}
|
||||
|
||||
// 去重
|
||||
func RemoveDuplicate(old []string) []string {
|
||||
result := []string{}
|
||||
temp := map[string]struct{}{}
|
||||
|
||||
Reference in New Issue
Block a user