-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmap-cli.go
146 lines (126 loc) · 2.78 KB
/
nmap-cli.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
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
)
var (
ipports map[string][]string
)
var inputFile = flag.String("f", "hostip.txt", "输入的文件")
var outputFile = flag.String("o", "", "输出的文件")
func main() {
flag.Parse()
//读取文件
contents := readFile(*inputFile)
//fmt.Println(contents)
//处理成ip集合
ips := handleIp(contents)
//fmt.Println(ips)
//处理成map结合ip[port port]
ipports = make(map[string][]string)
handleInput(ips, contents)
// for ip := range ips {
// fmt.Println(ipports[ip])
// }
for ip := range ips {
args := getArgs(ip)
nmap(args)
}
}
//结果处理
func handleOutput(outputString string) {
resultFile, err := os.OpenFile(*outputFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Println(nil)
os.Exit(1)
}
defer resultFile.Close()
writer := bufio.NewWriter(resultFile)
writer.WriteString(outputString)
writer.Flush()
}
//调用nmap
func nmap(args []string) {
// cmd := exec.Command(args[0], args[1:]...)
// fmt.Println(cmd)
// cmd.Stdout = os.Stdout
// err := cmd.Run()
// if err != nil {
// fmt.Println(err)
// }
cmd := exec.Command(args[0], args[1:]...)
fmt.Println(cmd)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
fmt.Println(err.Error(), stderr.String())
} else {
fmt.Println(out.String())
handleOutput(out.String())
}
}
func getArgs(ip string) (results []string) {
var args []string
portsStr := strings.Join(ipports[ip], ",")
//nmap ip -sV -n -p ports
args = append(args, "nmap")
args = append(args, "-n")
args = append(args, "-sV")
args = append(args, ip)
args = append(args, "-p", portsStr)
return args
}
func handleInput(ips map[string]struct{}, contents string) {
for ip := range ips {
//fmt.Println(ip)
ports := findports(ip, contents)
//fmt.Println(ports)
ipports[ip] = ports
}
}
//根据ip找ports
func findports(ip string, contents string) (results []string) {
ports := map[string]struct{}{}
list := strings.Split(contents, "\n")
for _, item := range list {
ipport := strings.Split(item, ":")
if ip == ipport[0] {
port := ipport[1]
ports[port] = struct{}{}
}
}
var result []string
for port := range ports {
result = append(result, port)
}
return result
}
func handleIp(contents string) (results map[string]struct{}) {
ips := map[string]struct{}{}
list := strings.Split(contents, "\n")
for _, item := range list {
ipport := strings.Split(item, ":")
ip := ipport[0]
ips[ip] = struct{}{}
}
return ips
}
func readFile(inputFile string) (contents string) {
file, err := os.Open(inputFile)
if err != nil {
fmt.Println(err)
}
defer file.Close()
content, err := ioutil.ReadAll(file)
return string(content)
}