From 94121a796fbfab94b821388888c18b6878d99eaf Mon Sep 17 00:00:00 2001 From: ZacharyZcR <2903735704@qq.com> Date: Mon, 23 Dec 2024 06:16:35 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0Modbus=E6=89=AB?= =?UTF-8?q?=E6=8F=8F=E5=92=8C=E6=B5=8B=E8=AF=95=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/ParseScanMode.go | 4 +- Common/Ports.go | 4 +- Core/Registry.go | 6 ++ Plugins/Modbus.go | 105 +++++++++++++++++++++++++++++++++++ TestDocker/Modbus/README.txt | 1 + 5 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 Plugins/Modbus.go create mode 100644 TestDocker/Modbus/README.txt diff --git a/Common/ParseScanMode.go b/Common/ParseScanMode.go index e112a6c..d8992a5 100644 --- a/Common/ParseScanMode.go +++ b/Common/ParseScanMode.go @@ -22,7 +22,7 @@ var pluginGroups = map[string][]string{ "web", "fcgi", // web类 "mysql", "mssql", "redis", "mongodb", "postgres", // 数据库类 "oracle", "memcached", "elasticsearch", "rabbitmq", "kafka", "activemq", // 数据库类 - "ftp", "ssh", "telnet", "smb", "rdp", "vnc", "netbios", "ldap", "smtp", "imap", "pop3", "snmp", "zabbix", // 服务类 + "ftp", "ssh", "telnet", "smb", "rdp", "vnc", "netbios", "ldap", "smtp", "imap", "pop3", "snmp", "zabbix", "modbus", // 服务类 "ms17010", "smbghost", "smb2", // 漏洞类 "findnet", // 其他 }, @@ -37,7 +37,7 @@ var pluginGroups = map[string][]string{ "web", "fcgi", }, ModeService: { - "ftp", "ssh", "telnet", "smb", "rdp", "vnc", "netbios", "ldap", "smtp", "imap", "pop3", "zabbix", + "ftp", "ssh", "telnet", "smb", "rdp", "vnc", "netbios", "ldap", "smtp", "imap", "pop3", "zabbix", "modbus", }, ModeVul: { "ms17010", "smbghost", "smb2", diff --git a/Common/Ports.go b/Common/Ports.go index 66d40a6..b24141f 100644 --- a/Common/Ports.go +++ b/Common/Ports.go @@ -5,12 +5,12 @@ import ( "strings" ) -var ServicePorts = "21,22,23,25,110,135,139,143,161,162,389,445,465,587,636,993,995,1433,1521,2222,3306,3389,5432,5672,5671,6379,8161,8443,9000,9092,9093,9200,10051,11211,15672,15671,27017,61616,61613" +var ServicePorts = "21,22,23,25,110,135,139,143,161,162,389,445,465,502,587,636,993,995,1433,1521,2222,3306,3389,5020,5432,5672,5671,6379,8161,8443,9000,9092,9093,9200,10051,11211,15672,15671,27017,61616,61613" var DbPorts = "1433,1521,3306,5432,5672,6379,9093,9200,11211,27017,61616" var WebPorts = "80,81,82,83,84,85,86,87,88,89,90,91,92,98,99,443,800,801,808,880,888,889,1000,1010,1080,1081,1082,1099,1118,1888,2008,2020,2100,2375,2379,3000,3008,3128,3505,5555,6080,6648,6868,7000,7001,7002,7003,7004,7005,7007,7008,7070,7071,7074,7078,7080,7088,7200,7680,7687,7688,7777,7890,8000,8001,8002,8003,8004,8005,8006,8008,8009,8010,8011,8012,8016,8018,8020,8028,8030,8038,8042,8044,8046,8048,8053,8060,8069,8070,8080,8081,8082,8083,8084,8085,8086,8087,8088,8089,8090,8091,8092,8093,8094,8095,8096,8097,8098,8099,8100,8101,8108,8118,8161,8172,8180,8181,8200,8222,8244,8258,8280,8288,8300,8360,8443,8448,8484,8800,8834,8838,8848,8858,8868,8879,8880,8881,8888,8899,8983,8989,9000,9001,9002,9008,9010,9043,9060,9080,9081,9082,9083,9084,9085,9086,9087,9088,9089,9090,9091,9092,9093,9094,9095,9096,9097,9098,9099,9100,9200,9443,9448,9800,9981,9986,9988,9998,9999,10000,10001,10002,10004,10008,10010,10051,10250,12018,12443,14000,15672,15671,16080,18000,18001,18002,18004,18008,18080,18082,18088,18090,18098,19001,20000,20720,20880,21000,21501,21502,28018" var UDPPorts = "161" var AllPorts = "1-65535" -var MainPorts = "21,22,23,80,81,110,135,139,143,161,389,443,445,993,995,1433,1521,3306,5432,5672,6379,7001,8000,8005,8009,8080,8089,8443,9000,9200,9092,10051,11211,15672,27017,61616" +var MainPorts = "21,22,23,80,81,110,135,139,143,161,389,443,445,502,993,995,1433,1521,3306,5432,5672,6379,7001,8000,8005,8009,8080,8089,8443,9000,9200,9092,10051,11211,15672,27017,61616" func ParsePortsFromString(portsStr string) []int { var ports []int diff --git a/Core/Registry.go b/Core/Registry.go index 6490b9d..e24e226 100644 --- a/Core/Registry.go +++ b/Core/Registry.go @@ -121,6 +121,12 @@ func init() { ScanFunc: Plugins.ZabbixScan, }) + Common.RegisterPlugin("modbus", Common.ScanPlugin{ + Name: "Modbus", + Ports: []int{502, 5020}, // Modbus 默认端口 + ScanFunc: Plugins.ModbusScan, + }) + Common.RegisterPlugin("rdp", Common.ScanPlugin{ Name: "RDP", Ports: []int{3389}, diff --git a/Plugins/Modbus.go b/Plugins/Modbus.go new file mode 100644 index 0000000..a00da6e --- /dev/null +++ b/Plugins/Modbus.go @@ -0,0 +1,105 @@ +package Plugins + +import ( + "encoding/binary" + "fmt" + "github.com/shadow1ng/fscan/Common" + "net" + "time" +) + +// ModbusScan 执行 Modbus 服务扫描 +func ModbusScan(info *Common.HostInfo) error { + host, port := info.Host, info.Ports + timeout := time.Duration(Common.Timeout) * time.Second + + // 尝试建立连接 + conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%s", host, port), timeout) + if err != nil { + return err + } + defer conn.Close() + + // 构造Modbus TCP请求包 - 读取设备ID + request := buildModbusRequest() + + // 设置读写超时 + conn.SetDeadline(time.Now().Add(timeout)) + + // 发送请求 + _, err = conn.Write(request) + if err != nil { + return fmt.Errorf("发送Modbus请求失败: %v", err) + } + + // 读取响应 + response := make([]byte, 256) + n, err := conn.Read(response) + if err != nil { + return fmt.Errorf("读取Modbus响应失败: %v", err) + } + + // 验证响应 + if isValidModbusResponse(response[:n]) { + result := fmt.Sprintf("[+] Modbus服务 %v:%v 无认证访问", host, port) + Common.LogSuccess(result) + + // 尝试读取更多设备信息 + deviceInfo := parseModbusResponse(response[:n]) + if deviceInfo != "" { + Common.LogSuccess(fmt.Sprintf("[+] 设备信息: %s", deviceInfo)) + } + return nil + } + + return fmt.Errorf("非Modbus服务或访问被拒绝") +} + +// buildModbusRequest 构建Modbus TCP请求包 +func buildModbusRequest() []byte { + request := make([]byte, 12) + + // Modbus TCP头部 + binary.BigEndian.PutUint16(request[0:], 0x0001) // 事务标识符 + binary.BigEndian.PutUint16(request[2:], 0x0000) // 协议标识符 + binary.BigEndian.PutUint16(request[4:], 0x0006) // 长度 + request[6] = 0x01 // 单元标识符 + + // Modbus 请求 + request[7] = 0x01 // 功能码: Read Coils + binary.BigEndian.PutUint16(request[8:], 0x0000) // 起始地址 + binary.BigEndian.PutUint16(request[10:], 0x0001) // 读取数量 + + return request +} + +// isValidModbusResponse 验证Modbus响应是否有效 +func isValidModbusResponse(response []byte) bool { + if len(response) < 9 { + return false + } + + // 检查协议标识符 + protocolID := binary.BigEndian.Uint16(response[2:]) + if protocolID != 0 { + return false + } + + // 检查功能码 + funcCode := response[7] + if funcCode == 0x81 { // 错误响应 + return false + } + + return true +} + +// parseModbusResponse 解析Modbus响应获取设备信息 +func parseModbusResponse(response []byte) string { + if len(response) < 9 { + return "" + } + + unitID := response[6] + return fmt.Sprintf("Unit ID: %d", unitID) +} diff --git a/TestDocker/Modbus/README.txt b/TestDocker/Modbus/README.txt new file mode 100644 index 0000000..d593864 --- /dev/null +++ b/TestDocker/Modbus/README.txt @@ -0,0 +1 @@ +docker run --rm -p 5020:5020 oitc/modbus-server:latest \ No newline at end of file