-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (95 loc) · 2.4 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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/exec"
"strings"
)
type Configuration struct {
Scan struct {
Title string
Targets string
Rate string
Ports string
PortTypes string
osDetection bool
}
}
func getConfig() Configuration {
// Try to open config file "scan.conf" in same directory - Prompt if not found.
configLoc := flag.String("configLoc", "./scan.conf", "Specify the configuration file.")
flag.Parse()
file, err := os.Open(*configLoc)
if err != nil {
log.Fatal("Failed to open config file: ", err)
}
defer file.Close()
// Read in JSON and return Configuration struct
decoder := json.NewDecoder(file)
Config := Configuration{}
err = decoder.Decode(&Config)
if err != nil {
log.Fatal("can't decode config JSON: ", err)
}
log.Println(Config.Scan.osDetection)
return Config
}
func readLines(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func masscanParser(path string) (string, string) {
lines, err := readLines(path)
if err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
var addresses_raw []string
var ports_raw []string
for _, line := range lines {
if len(strings.Split(line, " ")) > 2{
addresses_raw = append(addresses_raw, strings.Split(line, " ")[3])
ports_raw = append(ports_raw, strings.Split(line, " ")[2])
}
}
addresses := strings.Join(addresses_raw, " ")
ports := strings.Join(ports_raw, ",")
return addresses, ports
}
// func scan_handler(target string, timestamp string, portQuant string, portType string, osDetection bool)
func main() {
config := getConfig().Scan
massCmd := fmt.Sprintf("masscan %s", config.Targets)
massCmd += fmt.Sprintf(" -p%s", config.Ports)
massCmd += fmt.Sprintf(" --rate %s -oL mass_results", config.Rate)
err := exec.Command(massCmd)
if err != nil {
log.Fatal(err)
}
fmt.Println("Masscan complete.")
addresses, ports := masscanParser("./mass_results")
nmapCmd := "nmap "
if config.osDetection {
nmapCmd += "-O "
}
nmapCmd += "-p " + ports + " -Pn " + addresses + " -oX nmap_results.txt"
err = exec.Command(nmapCmd)
if err != nil {
fmt.Printf("Error: %v", err)
os.Exit(1)
}
fmt.Println("Nmap complete.")
}