github.com/chainreactors/fingers@v1.2.1/nmap/init_data.go (about)

     1  package gonmap
     2  
     3  import (
     4  	"github.com/chainreactors/fingers/resources"
     5  )
     6  
     7  // loadServicesFromBytes 从bytes加载services数据(支持gzip压缩或未压缩的JSON格式)
     8  func (n *Nmap) loadServicesFromBytes(servicesData []byte) {
     9  	var data ServicesData
    10  	if err := resources.UnmarshalData(servicesData, &data); err != nil {
    11  		return // 忽略错误,使用默认值
    12  	}
    13  
    14  	// 保存数据
    15  	n.servicesData = &data
    16  
    17  	// 构建nmapServices数组以保持兼容性
    18  	n.nmapServices = n.buildNmapServicesArray(&data)
    19  }
    20  
    21  // loadProbesFromBytes 从bytes加载probes数据(支持gzip压缩或未压缩的JSON格式)
    22  func (n *Nmap) loadProbesFromBytes(probesData []byte) {
    23  	var data NmapProbesData
    24  
    25  	if err := resources.UnmarshalData(probesData, &data); err != nil {
    26  		return
    27  	}
    28  
    29  	// 加载探针数据并重新编译正则表达式
    30  	for _, probe := range data.Probes {
    31  		// 重新编译每个Match中的正则表达式
    32  		for _, match := range probe.MatchGroup {
    33  			// 重新编译PatternRegexp,从JSON反序列化时不会保存正则对象
    34  			match.PatternRegexp = match.getPatternRegexp(match.Pattern, "")
    35  		}
    36  		n.pushProbe(*probe)
    37  	}
    38  }
    39  
    40  // addCustomMatches 添加自定义指纹
    41  func (n *Nmap) addCustomMatches() {
    42  	//新增自定义指纹信息
    43  	n.AddMatch("TCP_GetRequest", `echo m|^GET / HTTP/1.0\r\n\r\n$|s`)
    44  	n.AddMatch("TCP_GetRequest", `mongodb m|.*It looks like you are trying to access MongoDB.*|s p/MongoDB/`)
    45  	n.AddMatch("TCP_GetRequest", `http m|^HTTP/1\.[01] \d\d\d (?:[^\r\n]+\r\n)*?Server: ([^\r\n]+)| p/$1/`)
    46  	n.AddMatch("TCP_GetRequest", `http m|^HTTP/1\.[01] \d\d\d|`)
    47  	n.AddMatch("TCP_NULL", `mysql m|.\x00\x00..j\x04Host '.*' is not allowed to connect to this MariaDB server| p/MariaDB/`)
    48  	n.AddMatch("TCP_NULL", `mysql m|.\x00\x00..j\x04Host '.*' is not allowed to connect to this MySQL server| p/MySQL/`)
    49  	n.AddMatch("TCP_NULL", `mysql m|.\x00\x00\x00\x0a(\d+\.\d+\.\d+)\x00.*caching_sha2_password\x00| p/MariaDB/ v/$1/`)
    50  	n.AddMatch("TCP_NULL", `mysql m|.\x00\x00\x00\x0a(\d+\.\d+\.\d+)\x00.*caching_sha2_password\x00| p/MariaDB/ v/$1/`)
    51  	n.AddMatch("TCP_NULL", `mysql m|.\x00\x00\x00\x0a([\d.-]+)-MariaDB\x00.*mysql_native_password\x00| p/MariaDB/ v/$1/`)
    52  	n.AddMatch("TCP_NULL", `redis m|-DENIED Redis is running in.*| p/Redis/ i/Protected mode/`)
    53  	n.AddMatch("TCP_NULL", `telnet m|^.*Welcome to visit (.*) series router!.*|s p/$1 Router/`)
    54  	n.AddMatch("TCP_NULL", `telnet m|^Username: ??|`)
    55  	n.AddMatch("TCP_NULL", `telnet m|^.*Telnet service is disabled or Your telnet session has expired due to inactivity.*|s i/Disabled/`)
    56  	n.AddMatch("TCP_NULL", `telnet m|^.*Telnet connection from (.*) refused.*|s i/Refused/`)
    57  	n.AddMatch("TCP_NULL", `telnet m|^.*Command line is locked now, please retry later.*\x0d\x0a\x0d\x0a|s i/Locked/`)
    58  	n.AddMatch("TCP_NULL", `telnet m|^.*Warning: Telnet is not a secure protocol, and it is recommended to use Stelnet.*|s`)
    59  	n.AddMatch("TCP_NULL", `telnet m|^telnetd:|s`)
    60  	n.AddMatch("TCP_NULL", `telnet m|^.*Quopin CLI for (.*)\x0d\x0a\x0d\x0a|s p/$1/`)
    61  	n.AddMatch("TCP_NULL", `telnet m|^\x0d\x0aHello, this is FRRouting \(version ([\d.]+)\).*|s p/FRRouting/ v/$1/`)
    62  	n.AddMatch("TCP_NULL", `telnet m|^.*User Access Verification.*Username:|s`)
    63  	n.AddMatch("TCP_NULL", `telnet m|^Connection failed.  Windows CE Telnet Service cannot accept anymore concurrent users.|s o/Windows/`)
    64  	n.AddMatch("TCP_NULL", `telnet m|^\x0d\x0a\x0d\x0aWelcome to the host.\x0d\x0a.*|s o/Windows/`)
    65  	n.AddMatch("TCP_NULL", `telnet m|^.*Welcome Visiting Huawei Home Gateway\x0d\x0aCopyright by Huawei Technologies Co., Ltd.*Login:|s p/Huawei/`)
    66  	n.AddMatch("TCP_NULL", `telnet m|^..\x01..\x03..\x18..\x1f|s p/Huawei/`)
    67  	n.AddMatch("TCP_NULL", `smtp m|^220 ([a-z0-1.-]+).*| h/$1/`)
    68  	n.AddMatch("TCP_NULL", `ftp m|^220 H3C Small-FTP Server Version ([\d.]+).* | p/H3C Small-FTP/ v/$1/`)
    69  	n.AddMatch("TCP_NULL", `ftp m|^421[- ]Service not available..*|`)
    70  	n.AddMatch("TCP_NULL", `ftp m|^220[- ].*filezilla.*|i p/FileZilla/`)
    71  
    72  	// Add DCERPC/MSRPC match for TCP_NULL probe - matches the bind_ack response
    73  	n.AddMatch("TCP_NULL", `msrpc m|^\x05\x00\x0d\x03|s p/Microsoft Windows RPC/`)
    74  
    75  	n.AddMatch("TCP_TerminalServerCookie", `ms-wbt-server m|^\x03\0\0\x13\x0e\xd0\0\0\x124\0\x02.*\0\x02\0\0\0| p/Microsoft Terminal Services/ o/Windows/ cpe:/o:microsoft:windows/a`)
    76  	n.AddMatch("TCP_redis-server", `redis m|^.*redis_version:([.\d]+)\n|s p/Redis key-value store/ v/$1/ cpe:/a:redislabs:redis:$1/`)
    77  	n.AddMatch("TCP_redis-server", `redis m|^-NOAUTH Authentication required.|s p/Redis key-value store/`)
    78  }
    79  
    80  // optimizeProbes 优化探针配置
    81  func (n *Nmap) optimizeProbes() {
    82  	// HTTP端口优化
    83  	httpPorts := []int{80, 443, 8080, 8443, 8000, 8888, 9090}
    84  	for _, port := range httpPorts {
    85  		if port < len(n.portProbeMap) {
    86  			// 将HTTP探针放在前面
    87  			n.portProbeMap[port] = append([]string{"TCP_GetRequest"}, n.portProbeMap[port]...)
    88  		}
    89  	}
    90  
    91  	// SSL端口优化
    92  	sslPorts := []int{443, 8443, 3389}
    93  	for _, port := range sslPorts {
    94  		if port < len(n.portProbeMap) {
    95  			// 将SSL探针放在前面
    96  			for _, sslProbe := range n.sslProbeMap {
    97  				n.portProbeMap[port] = append([]string{sslProbe}, n.portProbeMap[port]...)
    98  			}
    99  		}
   100  	}
   101  }