/
nmapLib.go
203 lines (181 loc) · 5.33 KB
/
nmapLib.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
package deployer
import (
"bufio"
"fmt"
"math/rand"
"net"
"os"
"strconv"
"strings"
"time"
)
func cidrHosts(cidr string) ([]string, error) {
ip, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
var ips []string
for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
ips = append(ips, ip.String())
}
// remove network address and broadcast address
return ips, nil
}
func inc(ip net.IP) {
for j := len(ip) - 1; j >= 0; j-- {
ip[j]++
if ip[j] > 0 {
break
}
}
}
func ParseIPFile(path string) ([]string, error) {
var ipList []string
var cidrList []string
var endNum int
file, err := os.Open(path)
if err != nil {
return ipList, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
for _, ip := range lines {
ip = strings.TrimSpace(ip)
if _, _, err := net.ParseCIDR(ip); err == nil {
cidrList, _ = cidrHosts(ip)
ipList = append(ipList, cidrList...)
} else if net.ParseIP(ip) != nil {
ipList = append(ipList, ip)
} else if strings.Contains(ip, "-") {
ipRangeList := strings.Split(ip, "-")
digitList := strings.Split(ipRangeList[0], ".")
threeNumbers := strings.Join(digitList[:3], ".")
lastDigit := digitList[3]
startNum, _ := strconv.Atoi(lastDigit)
if net.ParseIP(ipRangeList[1]) != nil {
digitList = strings.Split(ipRangeList[1], ".")
endNum, _ = strconv.Atoi(digitList[3])
} else {
endNum, _ = strconv.Atoi(ipRangeList[1])
}
for i := startNum; i <= endNum; i++ {
incrementToString := strconv.Itoa(i)
ipList = append(ipList, threeNumbers+"."+incrementToString)
}
} else {
return ipList, fmt.Errorf("Incorrectly formatted ip, ip range, or subnet in file: %s", ip)
}
}
return ipList, nil
}
func normalizeTargets(targets []string) string {
return strings.Join(targets, " ")
}
func ValidatePorts(ports []string) (allports []string, err error) {
var validatePort, startPort, endPort int
for _, portString := range ports {
portString = strings.TrimSpace(portString)
if strings.Contains(portString, "-") {
portRange := strings.Split(portString, "-")
if len(portRange) != 2 {
err = fmt.Errorf("Incorrectly formatted port string")
return
}
startPort, err = strconv.Atoi(portRange[0])
if err != nil {
err = fmt.Errorf("Incorrectly formatted port string")
return
}
endPort, err = strconv.Atoi(portRange[1])
if err != nil {
err = fmt.Errorf("Incorrectly formatted port string")
return
}
if startPort > endPort {
err = fmt.Errorf("Incorrectly formatted port string")
return
} else if startPort < 0 || startPort > 65535 {
err = fmt.Errorf("Port is out of the range of valid ports")
return
} else if endPort < 0 || endPort > 65535 {
err = fmt.Errorf("Port is out of the range of valid ports")
return
}
for i := startPort; i <= endPort; i++ {
allports = append(allports, strconv.Itoa(i))
}
} else {
validatePort, err = strconv.Atoi(portString)
if err != nil {
err = fmt.Errorf("Incorrectly formatted port string")
return
}
if validatePort < 0 || validatePort > 65535 {
err = fmt.Errorf("Port is out of the range of valid ports")
return
}
allports = append(allports, strconv.Itoa(validatePort))
}
}
return
}
func generateIPPortList(targets []string, ports []string) []string {
var ipPortList []string
for _, port := range ports {
for _, ip := range targets {
ipPortList = append(ipPortList, ip+":"+port)
}
}
return ipPortList
}
func SplitNmapCommandsIntoHosts(ports []string, hostFile string, command string, count int, evasive bool) (commandList map[int][]string) {
hosts, _ := ParseIPFile(hostFile)
commandList = make(map[int][]string)
if evasive {
fmt.Println("Dividing hosts and ports for evasion...")
r := rand.New(rand.NewSource(time.Now().Unix()))
portList, _ := ValidatePorts(ports)
ipPortList := generateIPPortList(hosts, portList)
tempSlice := ipPortList
targetMap := make(map[int]map[string][]string)
for range ipPortList {
tempIndex := r.Intn(len(tempSlice))
p := tempIndex % count
if targetMap[p] == nil {
targetMap[p] = make(map[string][]string)
}
portSlice := strings.Split(ipPortList[tempIndex], ":")
targetMap[p][portSlice[1]] = append(targetMap[p][portSlice[1]], portSlice[0])
tempSlice = append(tempSlice[:tempIndex], tempSlice[tempIndex+1:]...)
}
for index, portIP := range targetMap {
for port, ips := range portIP {
tempCommand := command + " -p " + port + " -oA " + "/tmp/nmap/{{ ansible_host }}_$(date +\"%m%d%y_%H%M%S\") " + normalizeTargets(ips)
commandList[index] = append(commandList[index], tempCommand)
}
}
} else {
hostsPerServer := len(hosts) / count
remainderForServers := len(hosts) % count
for i := 0; i < count; i++ {
var targetHosts []string
remainder := 0
if remainderForServers > 0 {
remainder = 1
remainderForServers = remainderForServers - 1
}
if i == count-1 {
targetHosts = hosts[hostsPerServer*i:]
} else {
targetHosts = hosts[hostsPerServer*i : hostsPerServer*(i+1)+remainder]
}
tempCommand := command + " -p " + strings.Join(ports, ",") + " -oA " + "/tmp/nmap/{{ ansible_host }}_$(date +\"%m%d%y_%H%M%S\") " + normalizeTargets(targetHosts)
commandList[i] = append(commandList[i], tempCommand)
}
}
return
}