-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
312 lines (257 loc) · 7.93 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"context"
"fmt"
"github.com/Ullaakut/nmap"
"io/ioutil"
"log"
"net"
"os/exec"
"strings"
"time"
)
func main() {
fmt.Printf(
`
▄▄▄· .▄▄ · ▄▄· ▪ ▪ ▄▄▌ ▄▄▄· ▐ ▄ • ▌ ▄ ·. ▄▄▄· ▄▄▄·
▐█ ▀█ ▐█ ▀. ▐█ ▌▪██ ██ ██• ▐█ ▀█ •█▌▐█ ·██ ▐███▪▐█ ▀█ ▐█ ▄█
▄█▀▀█ ▄▀▀▀█▄██ ▄▄▐█·▐█· ██▪ ▄█▀▀█ ▐█▐▐▌ ▐█ ▌▐▌▐█·▄█▀▀█ ██▀·
▐█ ▪▐▌▐█▄▪▐█▐███▌▐█▌▐█▌ ▐█▌▐▌▐█ ▪▐▌██▐█▌ ██ ██▌▐█▌▐█ ▪▐▌▐█▪·•
▀ ▀ ▀▀▀▀ ·▀▀▀ ▀▀▀▀▀▀ .▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀ █▪▀▀▀ ▀ ▀ .▀
`)
ctx := context.Background()
localHosts(ctx)
result := scanLAN(ctx)
err := nmapWebReport(ctx, result)
if err != nil {
log.Fatal("Failed to generte web report", err)
}
generateDiagram(ctx, result)
ascii2png(ctx)
}
func scanLAN(cntxt context.Context) *nmap.Run {
ctx, cancel := context.WithTimeout(cntxt, 15*time.Minute)
defer cancel()
var (
resultBytes []byte
errorBytes []byte
)
s, err := nmap.NewScanner(
nmap.WithContext(ctx),
nmap.WithServiceInfo(),
nmap.WithScripts("vulners"),
nmap.WithTargetInput("ip.lst"),
nmap.WithStylesheet("oX-html.xsl"),
nmap.WithFilterHost(func(h nmap.Host) bool {
for idx := range h.Ports {
if h.Ports[idx].Status() == "open" {
return true
}
}
return false
}),
)
if err != nil {
log.Fatalf("unable to create nmap scanner: %v", err)
}
fmt.Println("Starting nmap LAN scan.")
// Executes asynchronously, allowing results to be streamed in real time.
if err := s.RunAsync(); err != nil {
panic(err)
}
// Connect to stdout of scanner.
stdout := s.GetStdout()
// Connect to stderr of scanner.
stderr := s.GetStderr()
// Goroutine to watch for stdout and print to screen. Additionally it stores
// the bytes intoa variable for processiing later.
go func() {
for stdout.Scan() {
//fmt.Println(stdout.Text())
resultBytes = append(resultBytes, stdout.Bytes()...)
}
}()
// Goroutine to watch for stderr and print to screen. Additionally it stores
// the bytes intoa variable for processiing later.
go func() {
for stderr.Scan() {
errorBytes = append(errorBytes, stderr.Bytes()...)
}
}()
// Blocks main until the scan has completed.
if err := s.Wait(); err != nil {
panic(err)
}
// Parsing the results into corresponding structs.
result, err := nmap.Parse(resultBytes)
// Parsing the results into the NmapError slice of our nmap Struct.
result.NmapErrors = strings.Split(string(errorBytes), "\n")
if err != nil {
panic(err)
}
fmt.Printf("Nmap done: Scanned in %.2f seconds\n\n", result.Stats.Finished.Elapsed)
return result
}
func generatePortInfo(ctx context.Context, host nmap.Host) string {
portASCII := []string{}
if len(host.Ports) != 0 {
for _, port := range host.Ports {
product := port.Service.Product
if len(port.Service.Product) > 8 {
product = port.Service.Product[0:8]
}
details := fmt.Sprintf("[+] %d:%s %s", port.ID, strings.Replace(port.Service.Name, "-", " ", -1), product)
details = genSpacing(details)
portASCII = append(portASCII, fmt.Sprintf("%s", details))
}
}
return strings.Join(portASCII, "\n\t\t| ")
}
func generateHostBox(ctx context.Context, host nmap.Host) string {
if len(host.Addresses) == 0 {
return ""
}
portInfoASCII := generatePortInfo(ctx, host)
hostinfo := " <" + host.Addresses[0].Addr + ">"
hostinfo = genSpacing(hostinfo)
hostBoxASCII := fmt.Sprintf(`
+-----------------------|-----+
| * |
| %s
| %s
| |
| * |
+-----------------------+-----+
`, hostinfo, portInfoASCII)
return hostBoxASCII
}
func generateDiagram(ctx context.Context, result *nmap.Run) error {
// Use the results to print an example output
var networkASCII []string
fivetabspace := "\t\t "
joinerline := fmt.Sprintf("%s|\n%s|\n%s|", fivetabspace, fivetabspace, fivetabspace)
for _, host := range result.Hosts {
networkASCII = append(networkASCII, generateHostBox(ctx, host))
}
//diagramAll := strings.Join(networkASCII[0:], "\t\t\t\t\t|\n\t\t\t\t\t|\n\t\t\t\t\t|")
diagramAll := strings.Join(networkASCII[0:], joinerline)
fmt.Printf("%s", diagramAll)
return ioutil.WriteFile("ASCII_LAN.txt", []byte(diagramAll), 0666)
}
func nmapWebReport(ctx context.Context, result *nmap.Run) error {
err := result.ToFile("LANscan.xml")
if err != nil {
return err
}
xml2html := exec.CommandContext(ctx, "bash", "-c", "xsltproc LANscan.xml -o LAN_WebReport.html")
fmt.Println("Converting xml scan data into html report LAN_WebReport.html")
xml2html.CombinedOutput()
if err != nil {
return err
}
fmt.Println("Done", "\n\n", "ASCII HOST MAP")
return nil
}
func ascii2png(ctx context.Context) error {
cmdstr := "diagram -in ASCII_LAN.txt -out LAN_DRAWING.png -preview=\"false\""
ascii2png := exec.CommandContext(ctx, "bash", "-c", cmdstr)
fmt.Println("Drawing Network Diagram to LAN_DRAWING.png")
stdout, err := ascii2png.CombinedOutput()
if err != nil {
return err
}
fmt.Println(string(stdout))
return nil
}
func genSpacing(curLine string) string {
remaningLength := 28 - len(curLine)
i := 0
for i < remaningLength {
curLine = curLine + " "
i++
}
curLine = curLine + "|"
return curLine
}
func localCIDR() string {
var octets string
ifaces, err := net.Interfaces()
if err != nil {
log.Fatal(err)
}
for _, iface := range ifaces {
// must have mac address, FlagUp and FlagBroadcast
if iface.HardwareAddr != nil && iface.Flags&net.FlagUp != 0 && iface.Flags&net.FlagBroadcast != 0 {
lanaddrs, err := iface.Addrs()
if err != nil {
log.Fatal(err)
}
octets = strings.Join((strings.SplitAfterN(lanaddrs[0].String(), ".", 4)[:3]), "") + "0/24"
return octets
}
}
return octets
}
func localHosts(cntxt context.Context) error {
ctx, cancel := context.WithTimeout(cntxt, 2*time.Minute)
defer cancel()
LANAddr := localCIDR()
fmt.Println("Discovering live hosts for ", LANAddr)
var (
resultBytes []byte
errorBytes []byte
)
s, err := nmap.NewScanner(
nmap.WithTargets(LANAddr),
nmap.WithContext(ctx),
nmap.WithPingScan(),
nmap.WithDisabledDNSResolution(),
)
if err != nil {
log.Fatalf("unable to create nmap scanner: %v", err)
}
// Executes asynchronously, allowing results to be streamed in real time.
if err := s.RunAsync(); err != nil {
panic(err)
}
// Connect to stdout of scanner.
stdout := s.GetStdout()
// Connect to stderr of scanner.
stderr := s.GetStderr()
// Goroutine to watch for stdout and print to screen. Additionally it stores
// the bytes intoa variable for processiing later.
go func() {
for stdout.Scan() {
//fmt.Println(stdout.Text())
resultBytes = append(resultBytes, stdout.Bytes()...)
}
}()
// Goroutine to watch for stderr and print to screen. Additionally it stores
// the bytes intoa variable for processiing later.
go func() {
for stderr.Scan() {
errorBytes = append(errorBytes, stderr.Bytes()...)
}
}()
// Blocks main until the scan has completed.
if err := s.Wait(); err != nil {
panic(err)
}
// Parsing the results into corresponding structs.
result, err := nmap.Parse(resultBytes)
// Parsing the results into the NmapError slice of our nmap Struct.
result.NmapErrors = strings.Split(string(errorBytes), "\n")
if err != nil {
panic(err)
}
fmt.Printf("Host discovery done: %d hosts up. Finished in %.2f seconds\n\n\n", len(result.Hosts), result.Stats.Finished.Elapsed)
LANIPs := []string{}
for _, host := range result.Hosts {
LANIPs = append(LANIPs, host.Addresses[0].String())
}
err = ioutil.WriteFile("ip.lst", []byte(strings.Join(LANIPs, "\n")), 0666)
if err != nil {
panic(err)
}
return nil
}