Skip to content

Commit

Permalink
[√]ok~
Browse files Browse the repository at this point in the history
  • Loading branch information
lcvvvv committed Apr 8, 2021
1 parent 92fd323 commit bded8b6
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 28 deletions.
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module gonmap
module github.com/lcvvvv/gonmap

go 1.16

require github.com/dlclark/regexp2 v1.4.0
go 1.16
36 changes: 23 additions & 13 deletions gonmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (

var NMAP *nmap

func Init() {
fmt.Println("初始化了")
//r["PROBE"] 总探针数、r["MATCH"] 总指纹数 、r["USED_PROBE"] 已使用探针数、r["USED_MATCH"] 已使用指纹数
func Init(filter int) map[string]int {
//fmt.Println("初始化了")
r := make(map[string]int)
NMAP_SERVICE_PROBES = strings.Replace(NMAP_SERVICE_PROBES, "${backquote}", "`", -1)
NMAP = &nmap{
exclude: newPort(),
Expand All @@ -23,21 +25,26 @@ func Init() {
finger: nil,
filter: 5,
}
for i := 1; i <= 65535; i++ {
NMAP.filter = filter
for i := 0; i <= 65535; i++ {
NMAP.portMap[i] = []string{}
}
NMAP.loads(NMAP_SERVICE_PROBES)
NMAP.allPortMap = append(NMAP.allPortMap, "TCP_GetRequest")
NMAP.probeGroup["TCP_GetRequest"].sslports.Fill()
NMAP.probeGroup["TCP_GetRequest"].ports.Fill()

PROBE_COUNT := len(NMAP.probeSort)
MATCH_COUNT := 0
r["PROBE"] = len(NMAP.probeSort)
r["MATCH"] = 0
for _, p := range NMAP.probeGroup {
MATCH_COUNT += len(p.matchGroup)
r["MATCH"] += len(p.matchGroup)
}
fmt.Println("成功加载探针:", PROBE_COUNT)
fmt.Println("成功加载指纹库:", MATCH_COUNT)
//fmt.Printf("成功加载探针:【%d】个,指纹【%d】条\n", PROBE_COUNT,MATCH_COUNT)
r["USED_PROBE"] = len(NMAP.portMap[0])
r["USED_MATCH"] = 0
for _, p := range NMAP.portMap[0] {
r["USED_MATCH"] += len(NMAP.probeGroup[p].matchGroup)
}
//fmt.Printf("本次扫描将使用探针:[%d]个,指纹[%d]条\n", USED_PROBE_COUNT,USED_MATCH_COUNT)
return r
}

func New() *nmap {
Expand Down Expand Up @@ -72,7 +79,7 @@ func (n *nmap) Scan(ip string, port int) *portinfo {
portinfo := newPortInfo()
//开始特定端口探测
for _, requestName := range n.portMap[port] {
fmt.Println("开始探测:", requestName, "权重为", n.probeGroup[requestName].rarity)
//fmt.Println("开始探测:", requestName, "权重为", n.probeGroup[requestName].rarity)
tls := n.probeGroup[requestName].sslports.Exist(n.target.port)
portinfo = n.getPortInfo(n.probeGroup[requestName], n.target, tls)
if portinfo.status == "CLOSE" || portinfo.status == "MATCHED" {
Expand Down Expand Up @@ -112,8 +119,8 @@ func (n *nmap) getPortInfo(p *probe, target *target, tls bool) *portinfo {
} else {
portinfo.response.string = data
//若存在返回包,则开始捕获指纹
fmt.Printf("成功捕获到返回包,返回包为:%x\n", data)
fmt.Printf("成功捕获到返回包,返回包长度为:%x\n", len(data))
//fmt.Printf("成功捕获到返回包,返回包为:%x\n", data)
//fmt.Printf("成功捕获到返回包,返回包长度为:%x\n", len(data))
portinfo.finger = n.getFinger(data, p.request.name)
if portinfo.finger.service == "" {
return portinfo.OPEN()
Expand Down Expand Up @@ -198,6 +205,9 @@ func (n *nmap) pushProbe(p *probe) {
if p.rarity > n.filter {
return
}
//0记录所有使用的探针
n.portMap[0] = append(n.portMap[0], p.request.name)

if p.ports.length+p.sslports.length == 0 {
p.ports.Fill()
p.sslports.Fill()
Expand Down
8 changes: 5 additions & 3 deletions gonmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
)

func TestGonmap(t *testing.T) {
Init()
status := Init(9)
fmt.Printf("[INFO] 成功加载探针:[%d]个,指纹[%d]条\n", status["PROBE"], status["MATCH"])
fmt.Printf("[INFO] 本次扫描将使用探针:[%d]个,指纹[%d]条\n", status["USED_PROBE"], status["USED_MATCH"])
n := New()
r := n.Scan("www.baidu.com", 443)
fmt.Println(r)
r := n.Scan("home.firefoxchina.cn", 443)
fmt.Printf("%s\t%s\t%s\n", n.target.uri, r.Service(), r.Info())
//for i := 1; i <= 10000; i++ {
// fmt.Println("开始探测端口:",i)
// fmt.Println(n.Scan("192.168.217.1", 139))
Expand Down
4 changes: 2 additions & 2 deletions simplenet/simplenet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Send(protocol string, netloc string, data string, duration time.Duration, s
return "", err
}
length, err := conn.Read(buf)
if err != nil {
if err != nil && err.Error() != "EOF" {
_ = conn.Close()
return "", err
}
Expand Down Expand Up @@ -54,7 +54,7 @@ func TLSSend(protocol string, netloc string, data string, duration time.Duration
}
buf := make([]byte, size)
length, err := conn.Read(buf)
if err != nil {
if err != nil && err.Error() != "EOF" {
_ = conn.Close()
return "", err
}
Expand Down
12 changes: 12 additions & 0 deletions type-finger.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ func newFinger() *finger {
}
}

func (f *finger) Map() map[string]string {
r := make(map[string]string)
r["service"] = f.service
r["productname"] = f.productname
r["version"] = f.version
r["info"] = f.info
r["hostname"] = f.hostname
r["operatingsystem"] = f.operatingsystem
r["devicetype"] = f.devicetype
return r
}

//func (f *finger) Show() {
// fmt.Println("service:", f.service)
// fmt.Println("productname:", f.productname)
Expand Down
38 changes: 37 additions & 1 deletion type-portinfo.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gonmap

import "fmt"

type portinfo struct {
response *response
finger *finger
Expand All @@ -22,10 +24,44 @@ func (p *portinfo) Response() string {
return p.response.string
}

func (p *portinfo) STATUS() string {
func (p *portinfo) Status() string {
return p.status
}

func (p *portinfo) Service() string {
return p.finger.service
}

func (p *portinfo) Info() string {
var s string
if p.finger.productname != "" {
s += fmt.Sprintf("Product:%s,", p.finger.productname)
}
if p.finger.version != "" {
s += fmt.Sprintf("Version:%s,", p.finger.version)
}
if p.finger.operatingsystem != "" {
s += fmt.Sprintf("OS:%s,", p.finger.operatingsystem)
}
if p.finger.hostname != "" {
s += fmt.Sprintf("HostName:%s,", p.finger.hostname)
}
if p.finger.devicetype != "" {
s += fmt.Sprintf("DeviceType:%s,", p.finger.devicetype)
}
if p.finger.info != "" {
s += fmt.Sprintf("OtherInfo:%s,", p.finger.info)
}
if s != "" {
s = s[:len(s)-1]
}
return s
}

func (p *portinfo) Map() map[string]string {
return p.finger.Map()
}

func (p *portinfo) CLOSED() *portinfo {
p.status = "CLOSED"
return p
Expand Down
9 changes: 4 additions & 5 deletions type-probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package gonmap

import (
"errors"
"fmt"
"gonmap/simplenet"
"github.com/lcvvvv/gonmap/simplenet"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -53,10 +52,10 @@ func (p *probe) loads(sArr []string) {

func (p *probe) scan(t *target, ssl bool) (string, error) {
if ssl {
fmt.Println("开始TLS探测")
//fmt.Println("开始TLS探测")
return simplenet.TLSSend(p.request.protocol, t.uri, p.request.string, p.totalwaitms, 512)
} else {
fmt.Println("开始TCP探测")
//fmt.Println("开始TCP探测")
return simplenet.Send(p.request.protocol, t.uri, p.request.string, p.totalwaitms, 512)
}
}
Expand All @@ -72,7 +71,7 @@ func (p *probe) match(s string) *finger {
}
//fmt.Println("开始匹配正则:", m.service, m.patternRegexp.String())
if m.patternRegexp.MatchString(s) {
fmt.Println("成功匹配指纹:", m.pattern, "所在probe为:", p.request.name)
//fmt.Println("成功匹配指纹:", m.pattern, "所在probe为:", p.request.name)
if m.soft {
//如果为软捕获,这设置筛选器
f.service = m.service
Expand Down

0 comments on commit bded8b6

Please sign in to comment.