1 | 1 | | package modules |
2 | 2 | | |
3 | 3 | | import ( |
| 4 | + | "bytes" |
4 | 5 | | "fmt" |
5 | 6 | | "io/ioutil" |
6 | 7 | | "os/exec" |
7 | 8 | | "path/filepath" |
8 | 9 | | "strings" |
| 10 | + | "text/template" |
9 | 11 | | |
10 | 12 | | jsoniter "github.com/json-iterator/go" |
11 | 13 | | |
| skipped 53 lines |
65 | 67 | | return realResult |
66 | 68 | | } |
67 | 69 | | |
| 70 | + | type nmap struct { |
| 71 | + | } |
| 72 | + | |
68 | 73 | | // RunNmap run nmap command and return list of port open |
69 | 74 | | func RunNmap(input string, ports string, options core.Options) []string { |
70 | 75 | | // use nmap as overview scan |
| skipped 9 lines |
80 | 85 | | tmpFile, _ = ioutil.TempFile(nmapOutput, fmt.Sprintf("nmap-%v-*", core.StripPath(input))) |
81 | 86 | | } |
82 | 87 | | nmapOutput = tmpFile.Name() |
83 | | - | nmapCmd := fmt.Sprintf("sudo nmap -sSV -p %v %v -T4 --open -oA %v", ports, input, nmapOutput) |
| 88 | + | |
| 89 | + | // build nmap command |
| 90 | + | nmapCommand := make(map[string]string) |
| 91 | + | nmapCommand["output"] = nmapOutput |
| 92 | + | nmapCommand["ports"] = ports |
| 93 | + | nmapCommand["input"] = input |
84 | 94 | | if options.Scan.NmapScripts != "" { |
85 | | - | nmapCmd = fmt.Sprintf("sudo nmap -sSV -p %v %v -T4 --open --script %v -oA %v", ports, input, options.Scan.NmapScripts, nmapOutput) |
| 95 | + | nmapCommand["script"] = fmt.Sprintf("--script %v", options.Scan.NmapScripts) |
| 96 | + | } else { |
| 97 | + | nmapCommand["script"] = "" |
86 | 98 | | } |
| 99 | + | nmapCmd := ResolveData(options.Scan.NmapTemplate, nmapCommand) |
| 100 | + | // |
| 101 | + | //nmapCmd := fmt.Sprintf("sudo nmap -sSV -p %v %v -T4 --open -oA %v", ports, input, nmapOutput) |
| 102 | + | //if options.Scan.NmapScripts != "" { |
| 103 | + | // nmapCmd = fmt.Sprintf("sudo nmap -sSV -p %v %v --script %v -T4 --open -oA %v", ports, input, options.Scan.NmapScripts, nmapOutput) |
| 104 | + | //} |
| 105 | + | |
87 | 106 | | core.DebugF("Execute: %v", nmapCmd) |
88 | 107 | | command := []string{ |
89 | 108 | | "bash", |
| skipped 104 lines |
194 | 213 | | return result |
195 | 214 | | } |
196 | 215 | | |
| 216 | + | // ResolveData resolve template from signature file |
| 217 | + | func ResolveData(format string, data map[string]string) string { |
| 218 | + | t := template.Must(template.New("").Parse(format)) |
| 219 | + | buf := &bytes.Buffer{} |
| 220 | + | err := t.Execute(buf, data) |
| 221 | + | if err != nil { |
| 222 | + | return format |
| 223 | + | } |
| 224 | + | return buf.String() |
| 225 | + | } |
| 226 | + | |