-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
262 lines (228 loc) · 6.01 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
package main
import (
"bufio"
"bytes"
"fmt"
"github.com/gin-gonic/gin"
"io"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
)
type IP struct {
IP string `json:"ip"`
}
type Port struct {
IP string `json:"ip"`
Port string `json:"port"`
Service string `json:"service"`
Protocol string `json:"protocol"`
}
// to string
func (p *Port) ToString() string {
return fmt.Sprintf("%s:%s, service: %s, protocol: %s", p.IP, p.Port, p.Service, p.Protocol)
}
func main() {
r := gin.Default()
r.POST("/scan/json", func(c *gin.Context) {
var ips []IP
if err := c.BindJSON(&ips); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// 处理IP列表
ips_str := ""
for _, ip := range ips {
if !IsIp(ip.IP) {
c.JSON(http.StatusBadRequest, gin.H{"error": "ip is error"})
return
}
ips_str += ip.IP + ","
}
ts := time.Now().Unix()
//randomStr := fmt.Sprintf("%d", rand.Intn(1000))
filename := fmt.Sprintf("ips_%d.txt", ts)
fmt.Println(filename)
go Run(ips_str[:len(ips_str)-1], filename)
// return status
c.JSON(http.StatusOK, gin.H{"filename": filename})
})
r.POST("/scan/text", func(c *gin.Context) {
bodyBytes, err := io.ReadAll(c.Request.Body)
if err != nil {
// 处理读取错误...
return
}
bodyString := string(bodyBytes)
// 处理IP列表
var ips []IP
ipstr := strings.Split(bodyString, "\r\n")
for _, ip := range ipstr {
if ip == "" {
continue
}
ips = append(ips, IP{IP: ip})
}
// 处理IP列表
ips_str := ""
for _, ip := range ips {
if !IsIp(ip.IP) {
c.JSON(http.StatusBadRequest, gin.H{"error": "ip is error"})
return
}
ips_str += ip.IP + ","
}
ts := time.Now().Unix()
//randomStr := fmt.Sprintf("%d", rand.Intn(1000))
filename := fmt.Sprintf("ips_%d.txt", ts)
fmt.Println(filename)
go Run(ips_str[:len(ips_str)-1], filename)
// return status
c.JSON(http.StatusOK, gin.H{"filename": filename})
})
r.GET("/show/result", func(c *gin.Context) {
// 获取当前目录下的cache目录路径
cacheDir := "./cache/"
// 读取cache目录下的所有文件
files, err := os.ReadDir(cacheDir)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
// 将文件名添加到列表中
var filenames []string
for _, file := range files {
if !file.IsDir() {
filenames = append(filenames, file.Name())
}
}
// 返回文件名列表
c.JSON(http.StatusOK, gin.H{"files": filenames})
})
r.GET("/download/:filename", func(c *gin.Context) {
// 获取当前目录下的cache目录路径
cacheDir := "./cache/"
// 获取请求参数中的文件名
filename := c.Param("filename")
// 规范化文件路径
filePath := filepath.Clean(filepath.Join(cacheDir, filename))
// 检查文件路径是否合法
if !strings.HasPrefix(filePath, "cache") {
c.JSON(http.StatusBadRequest, gin.H{"error": "非法文件路径"})
return
}
// 打开文件
file, err := os.Open(filePath)
if err != nil {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "无法打开文件"})
return
}
defer file.Close()
// 设置响应头
stat, err := file.Stat()
if err != nil {
log.Println(err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "无法读取文件信息"})
return
}
c.Writer.Header().Set("Content-Disposition", "attachment; filename="+filename)
c.Writer.Header().Set("Content-Type", "application/octet-stream")
c.Writer.Header().Set("Content-Length", string(stat.Size()))
// 发送文件内容给客户端
http.ServeContent(c.Writer, c.Request, filename, stat.ModTime(), file)
})
r.Run(":50500")
}
func Run(targets string, filename string) {
output := RunRustScan(targets)
ports := NmapPortDataCleaning(output)
out_str := ""
for _, port := range ports {
fmt.Println(port.ToString())
out_str += port.ToString() + "\n"
}
WriteFile("./cache/"+filename, []byte(out_str))
}
// write file to disk
func WriteFile(path string, data []byte) error {
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
fmt.Println(err)
return err
}
defer file.Close()
_, err = file.Write(data)
if err != nil {
fmt.Println(err)
return err
}
return nil
}
func getIPs(body string) []IP {
var ips []IP
for _, addr := range strings.Split(string(body), "\n") {
ips = append(ips, IP{IP: strings.TrimSpace(addr)})
}
return ips
}
// cmd run rustscan
func RunRustScan(ips string) []byte {
cmd := "rustscan --ulimit 5000 -a " + ips
command := exec.Command("/bin/sh", "-c", cmd)
//fmt.Println(cmd)
output, err := command.Output()
fmt.Println(string(output))
if err != nil {
fmt.Println(err)
return nil
}
return output
}
// check ip string
func IsIp(ip string) bool {
ipPattern := regexp.MustCompile(`((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)`)
if matches := ipPattern.FindStringSubmatch(ip); matches != nil {
return true
}
return false
}
// nmap port data clean
func NmapPortDataCleaning(output []byte) []Port {
//result, _ := os.Open(path)
// 创建正则表达式匹配模式
ipPattern := regexp.MustCompile(`Nmap scan report for.*?(((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))`)
portPattern := regexp.MustCompile(`(\d+)/(tcp|udp)\s+open\s+([\w\-\.\+]+)\s*`)
// 逐行读取文件并匹配ip和端口信息
scanner := bufio.NewScanner(bytes.NewReader(output))
var ip string
var ports []Port
for scanner.Scan() {
line := scanner.Text()
port := Port{}
// 匹配ip地址
if matches := ipPattern.FindStringSubmatch(line); matches != nil {
if matches[1] != "" {
ip = matches[1]
}
}
port.IP = ip
// 匹配端口信息
if matches := portPattern.FindStringSubmatch(line); matches != nil {
portvalue := matches[1]
protocol := matches[2]
service := matches[3]
port.Port = portvalue
port.Protocol = protocol
port.Service = service
// add port to ports
ports = append(ports, port)
}
}
return ports
}