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

     1  package gonmap
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  // NmapProbesData 用于 JSON/YAML 序列化和反序列化的顶层数据结构
     8  type NmapProbesData struct {
     9  	Probes   []*Probe          `json:"probes" yaml:"probes"`
    10  	Services map[string]string `json:"services,omitempty" yaml:"services,omitempty"`
    11  }
    12  
    13  // TempNmapParser 临时的nmap解析器,用于transform工具
    14  type TempNmapParser struct {
    15  	probeNameMap map[string]*Probe
    16  }
    17  
    18  // NewTempParser 创建临时解析器
    19  func NewTempParser(content string) *TempNmapParser {
    20  	parser := &TempNmapParser{
    21  		probeNameMap: make(map[string]*Probe),
    22  	}
    23  	parser.loads(content)
    24  	return parser
    25  }
    26  
    27  // GetProbes 获取解析的探针
    28  func (t *TempNmapParser) GetProbes() map[string]*Probe {
    29  	return t.probeNameMap
    30  }
    31  
    32  // loads 解析nmap-service-probes内容(从type-nmap.go复制)
    33  func (t *TempNmapParser) loads(s string) {
    34  	lines := strings.Split(s, "\n")
    35  	var probeGroups [][]string
    36  	var probeLines []string
    37  	for _, line := range lines {
    38  		if !t.isCommand(line) {
    39  			continue
    40  		}
    41  		commandName := line[:strings.Index(line, " ")]
    42  		if commandName == "Exclude" {
    43  			continue // 忽略Exclude命令
    44  		}
    45  		if commandName == "Probe" {
    46  			if len(probeLines) != 0 {
    47  				probeGroups = append(probeGroups, probeLines)
    48  				probeLines = []string{}
    49  			}
    50  		}
    51  		probeLines = append(probeLines, line)
    52  	}
    53  	probeGroups = append(probeGroups, probeLines)
    54  
    55  	for _, lines := range probeGroups {
    56  		p := parseProbe(lines)
    57  		t.pushProbe(*p)
    58  	}
    59  }
    60  
    61  // pushProbe 添加探针到映射中
    62  func (t *TempNmapParser) pushProbe(p Probe) {
    63  	t.probeNameMap[p.Name] = &p
    64  }
    65  
    66  // isCommand 检查是否是有效命令行(从type-nmap.go复制)
    67  func (t *TempNmapParser) isCommand(line string) bool {
    68  	//删除注释行和空行
    69  	if len(line) < 2 {
    70  		return false
    71  	}
    72  	if line[:1] == "#" {
    73  		return false
    74  	}
    75  	//删除异常命令
    76  	commandName := line[:strings.Index(line, " ")]
    77  	commandArr := []string{
    78  		"Exclude", "Probe", "match", "softmatch", "ports", "sslports", "totalwaitms", "tcpwrappedms", "rarity", "fallback",
    79  	}
    80  	for _, item := range commandArr {
    81  		if item == commandName {
    82  			return true
    83  		}
    84  	}
    85  	return false
    86  }