Skip to content

Commit 29dcd78

Browse files
committed
update
修改云函数配置,增加详细说明
1 parent 3ada2b1 commit 29dcd78

File tree

5 files changed

+110
-78
lines changed

5 files changed

+110
-78
lines changed

README.md

+61-31
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@
33

44
## 安装
55

6-
下载release中的二进制文件使用
6+
下载 release 中的二进制文件使用
77

8-
或使用Makefile进行编译二进制文件后使用
8+
或使用 Makefile 进行编译二进制文件后使用
99

1010
## 配置
11-
当首次运行Serverless_PortScan时,会检测config.json文件是否存在,不存在则会自动创建
12-
13-
config.json的填写内容应该如下:
14-
```
15-
{
16-
"ServerUrl":"http://",
17-
"PortList":"21,22,23,25,80,135,139,389,443,445,1433,1521,3306,3389"
18-
}
19-
```
20-
ServerUrl为你的云函数地址,PortList为默认扫描的端口列表
11+
当首次运行 Serverless_PortScan 时,会检测 config.yaml 文件是否存在,不存在则会自动创建
2112

13+
config.yaml 中的 ServerUrl 需要在云函数配置好后才能配置,请看下面:
2214

2315
## 云函数配置
24-
将以下内容配置到云函数,并建议将云函数执行时间和api网关后端超时设置为15秒或以上:
16+
控制台搜索云函数,进入云函数控制台后,新建函数
17+
![img.png](img.png)
18+
点击从头开始,选择事件函数,运行环境选择python3.x(都可以)
19+
![img_1.png](img_1.png)
20+
在函数代码中中复制粘贴下面的代码:
2521

2622
```python
2723
import socket
@@ -32,27 +28,44 @@ import threading
3228
# 使用defaultdict存储每个线程找到的开放端口
3329
results_per_thread = defaultdict(list)
3430
ip = ""
35-
ports = ""
31+
lock = threading.Lock()
3632

3733
def Scan(port):
3834
try:
3935
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
40-
conn.settimeout(1)
36+
conn.settimeout(2)
4137
res = conn.connect_ex((str(ip), int(port)))
4238
if res == 0:
4339
# 将结果添加到当前线程局部变量中
40+
lock.acquire()
4441
results_per_thread[threading.current_thread()].append(port)
42+
lock.release()
4543
except Exception as err:
4644
print(err)
4745
finally:
4846
conn.close()
4947

5048
def main_handler(event, context):
51-
global ip, ports
52-
ip = event["queryString"]["ip"]
53-
ports = event["queryString"]["port"].split(",")
54-
55-
with ThreadPoolExecutor(max_workers=20) as executor:
49+
global ip
50+
51+
try:
52+
ip = event["queryString"]["ip"]
53+
port_str = event["queryString"]["port"]
54+
path = event["path"]
55+
if path != "/portscan" :
56+
return
57+
except:
58+
return event
59+
60+
ports = []
61+
for port_range in port_str.split(","):
62+
if "-" in port_range:
63+
start, end = map(int, port_range.split("-"))
64+
ports.extend(range(start, end+1))
65+
else:
66+
ports.append(int(port_range))
67+
68+
with ThreadPoolExecutor(max_workers=30) as executor:
5669
executor.map(Scan, ports)
5770

5871
# 合并所有线程的结果
@@ -62,13 +75,30 @@ def main_handler(event, context):
6275

6376
# 将端口列表转换为逗号分隔的字符串
6477
a = ",".join(str(port) for port in open_ports)
78+
6579
results_per_thread.clear()
6680
return a
6781
```
68-
69-
触发管理配置如下:
70-
![image](https://github.com/shadowabi/Serverless_PortScan/assets/50265741/899e0445-dd7c-4c2b-9bdd-26c248fa0eb6)
71-
82+
![img_2.png](img_2.png)
83+
往下拉,点击高级配置,设置执行超时时间为 900 秒,也可以自定义一个时间。内存选择 384MB 或以下即可(如果扫描端口过多会爆内存)
84+
![img_4.png](img_4.png)
85+
基础配置如上,最后勾选“我已阅读并同意 《腾讯云云函数网络服务协议》”,点击完成。
86+
87+
创建完毕后,会弹出下面界面:
88+
![img_5.png](img_5.png)
89+
点留在本页,然后点函数URL,创建URL,启用公网访问
90+
![img_6.png](img_6.png)
91+
![img_7.png](img_7.png)
92+
点击提交后,会生成一个地址
93+
![img_8.png](img_8.png)
94+
例如这里:
95+
```
96+
https://1304875526-enqewmqsrf-hk.scf.tencentcs.com
97+
```
98+
在 URL 地址最后拼接上 /portscan,配置到 config.yaml 中的 ServerUrl,即
99+
```
100+
ServerUrl: https://1304875526-enqewmqsrf-hk.scf.tencentcs.com/portscan
101+
```
72102

73103

74104
## 用法
@@ -85,22 +115,22 @@ Flags:
85115
--logLevel string 设置日志等级 (Set log level) [trace|debug|info|warn|error|fatal|panic] (default "info")
86116
-o, --output string 输入结果文件输出的位置 (Enter the location of the scan result output) (default "./result.txt")
87117
-p, --port string 输入需要被扫描的端口,逗号分割 (Enter the port to be scanned, separated by commas (,))
88-
-t, --timeout int 输入每个 http 请求的超时时间 (Enter the timeout period for every http request) (default 5)
118+
-t, --timeout int 输入每个 http 请求的超时时间 (Enter the timeout period for every http request) (default 900)
89119
-u, --url string 输入目标地址 (Input [ip|domain|url])
90120
```
91121

92122

93123
## 功能列表
94124

95-
1. 利用云函数特性扫描端口,防止封ip
96-
2. 本地多线程+云函数多线程发包,提高扫描速度
125+
1. 利用云函数特性扫描端口,防止封 ip
126+
2. 本地多线程 + 云函数多线程发包,提高扫描速度
97127
3. 自动去重
98-
4. 文件输出时为:ip+端口号形式,方便利用其他工具如指纹识别工具进行扫描
99-
5. 采用go语言编写,提升性能
128+
4. 文件输出时为:ip + 端口号形式,方便利用其他工具如指纹识别工具进行扫描
129+
5. 采用 go 语言编写,提升性能
100130

101131

102132
## 旧版本
103133

104-
python版本在python分支
134+
python 版本在 python 分支
105135

106-
旧的go版本在go-old
136+
旧的 go 版本在 go-old

cmd/rootCmd.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Serverless_PortScan is used to scan ports using cloud functions.
5252
}
5353

5454
hostList = pkg.RemoveDuplicates(hostList)
55-
client := pkg.GenerateHTTPClient(define.TimeOut * len(hostList)) // 动态分配 http 超时时间
55+
client := pkg.GenerateHTTPClient(define.TimeOut) // 动态分配 http 超时时间
5656

5757
reqList := pkg.ConvertToReqList(define.Port, hostList...)
5858
resp := pkg.FetchPortData(client, reqList...)
@@ -71,7 +71,7 @@ func init() {
7171
RootCmd.Flags().StringVarP(&define.File, "file", "f", "", "从文件中读取目标地址 (Input filename)")
7272
RootCmd.Flags().StringVarP(&define.Url, "url", "u", "", "输入目标地址 (Input [ip|domain|url])")
7373
RootCmd.Flags().StringVarP(&define.Port, "port", "p", "", "输入需要被扫描的端口,逗号分割 (Enter the port to be scanned, separated by commas (,))")
74-
RootCmd.Flags().IntVarP(&define.TimeOut, "timeout", "t", 5, "输入每个 http 请求的超时时间 (Enter the timeout period for every http request)")
74+
RootCmd.Flags().IntVarP(&define.TimeOut, "timeout", "t", 900, "输入每个 http 请求的超时时间 (Enter the timeout period for every http request)")
7575
RootCmd.Flags().StringVarP(&define.OutPUT, "output", "o", "./result.txt", "输入结果文件输出的位置 (Enter the location of the scan result output)")
7676
}
7777

config/config.go

+36-27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package config
22

33
import (
4+
"fmt"
45
"github.com/shadowabi/Serverless_PortScan_rebuild/define"
56
"github.com/shadowabi/Serverless_PortScan_rebuild/utils/Error"
67
"github.com/spf13/viper"
@@ -12,40 +13,48 @@ type Config struct {
1213
}
1314

1415
// GlobalConfig default Global Variable for Config
15-
var GlobalConfig *Config
16+
var GlobalConfig *viper.Viper
1617

1718
var C define.Configure
1819

19-
// DefaultInit func is default Init Config file without config file information
20-
func DefaultInit() {
21-
GlobalConfig = &Config{
22-
viper.New(),
23-
}
24-
25-
// Config filename (no suffix)
26-
GlobalConfig.SetConfigName("config")
27-
28-
// Config Type
20+
//// DefaultInit func is default Init Config file without config file information
21+
//func DefaultInit() {
22+
// GlobalConfig = &Config{
23+
// viper.New(),
24+
// }
25+
//
26+
// // Config filename (no suffix)
27+
// GlobalConfig.SetConfigName("config")
28+
//
29+
// // Config Type
30+
// GlobalConfig.SetConfigType("yaml")
31+
//
32+
// // Searching Path
33+
// GlobalConfig.AddConfigPath(".")
34+
// GlobalConfig.AddConfigPath("../") // For Debug
35+
// GlobalConfig.AddConfigPath("./config")
36+
//
37+
// // Reading Config
38+
// err := GlobalConfig.ReadInConfig()
39+
// Error.HandleFatal(err)
40+
//}
41+
42+
func InitConfigure(file string) {
43+
GlobalConfig = viper.New()
44+
GlobalConfig.SetConfigFile(file)
2945
GlobalConfig.SetConfigType("yaml")
30-
31-
// Searching Path
32-
GlobalConfig.AddConfigPath(".")
33-
GlobalConfig.AddConfigPath("../") // For Debug
34-
GlobalConfig.AddConfigPath("./config")
35-
36-
// Reading Config
3746
err := GlobalConfig.ReadInConfig()
38-
Error.HandleFatal(err)
47+
if err != nil {
48+
SaveConfig(file)
49+
Error.HandleFatal(fmt.Errorf("不存在 config.yaml,已生成"))
50+
}
51+
Error.HandleError(GlobalConfig.Unmarshal(&C))
3952
}
4053

41-
// SpecificInit func is Init with specific Config file
42-
func SpecificInit(file string) {
43-
GlobalConfig = &Config{
44-
viper.New(),
45-
}
54+
func SaveConfig(file string) {
55+
GlobalConfig.Set("ServerUrl", "")
56+
GlobalConfig.Set("PortList", "21-23,25,80,135,139,389,443,445,1433,1521,3306,3389")
4657
GlobalConfig.SetConfigFile(file)
47-
GlobalConfig.SetConfigType("json")
48-
err := GlobalConfig.ReadInConfig()
58+
err := GlobalConfig.WriteConfig()
4959
Error.HandleFatal(err)
50-
Error.HandleError(GlobalConfig.Unmarshal(&C))
5160
}

main/main.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
package main
22

33
import (
4-
"errors"
4+
"fmt"
55
"github.com/shadowabi/Serverless_PortScan_rebuild/cmd"
66
"github.com/shadowabi/Serverless_PortScan_rebuild/config"
77
"github.com/shadowabi/Serverless_PortScan_rebuild/pkg"
88
"github.com/shadowabi/Serverless_PortScan_rebuild/utils/Error"
9-
"github.com/shadowabi/Serverless_PortScan_rebuild/utils/File"
10-
"strings"
119
)
1210

1311
func init() {
14-
configFile := pkg.GetPwd()
15-
configFile = strings.Join([]string{configFile, "/config.json"}, "")
16-
err := File.FileNonExistCreate(configFile)
17-
Error.HandleFatal(err)
18-
config.SpecificInit(configFile)
19-
if config.C.ServerUrl == "http://" || config.C.ServerUrl == "" || config.C.PortList == "" {
20-
Error.HandleFatal(errors.New("请配置config.json"))
21-
return
22-
}
12+
config.InitConfigure("config.yaml")
2313
}
2414

2515
func main() {
16+
if pkg.IsEmptyConfig(config.C) {
17+
Error.HandleFatal(fmt.Errorf("请配置 config.yaml"))
18+
return
19+
}
20+
2621
cmd.Execute()
2722
}

pkg/client.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package pkg
22

33
import (
44
"crypto/tls"
5-
"github.com/shadowabi/Serverless_PortScan_rebuild/utils/Error"
5+
"github.com/shadowabi/Serverless_PortScan_rebuild/define"
66
"net/http"
7-
"os"
87
"time"
98
)
109

@@ -18,8 +17,7 @@ func GenerateHTTPClient(timeOut int) *http.Client {
1817
return client
1918
}
2019

21-
func GetPwd() (homePath string) {
22-
homePath, err := os.Getwd()
23-
Error.HandlePanic(err)
24-
return homePath
20+
func IsEmptyConfig(c define.Configure) bool {
21+
return c.ServerUrl == "" ||
22+
c.PortList == ""
2523
}

0 commit comments

Comments
 (0)