Skip to content

Commit

Permalink
Merge pull request #16 from hanc00l/custom_dev
Browse files Browse the repository at this point in the history
Update: 修改输出结果的显示方式,增加nbtscan代码
  • Loading branch information
linksucre authored May 2, 2021
2 parents ecf5d90 + e74d45d commit 332ff68
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 48 deletions.
58 changes: 25 additions & 33 deletions pkg/Ginfo/Ghttp/Ghttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ retry:
URL = fmt.Sprintf("%s://%s:%d", protocol, domain, port)
}



var client *http.Client
//DEBUG := false
//if DEBUG {
Expand Down Expand Up @@ -149,7 +147,6 @@ retry:
builder.WriteRune(']')
}


defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand Down Expand Up @@ -244,22 +241,17 @@ func (r *Result) ToString() string {
} else {
builder.WriteString("[")
builder.WriteString(color.GreenString(conversion.ToString(r.StatusCode)))
if r.ContentLength != -1 {
builder.WriteString("|")
builder.WriteString(color.YellowString(conversion.ToString(r.ContentLength)))
builder.WriteString("] ")
if r.WebServer != "" {
builder.WriteString("[")
builder.WriteString(color.GreenString(r.WebServer))
builder.WriteString("] ")
}
builder.WriteString("]")

if r.Title !=""{
if r.Title != "" {
builder.WriteString("[")
builder.WriteString(color.GreenString(r.Title))
builder.WriteString("]")
}else{
builder.WriteString("[")
builder.WriteString(color.GreenString(r.str[:10]))
builder.WriteString("]")
builder.WriteString("] ")
}

}

return builder.String()
Expand Down Expand Up @@ -314,8 +306,8 @@ func (h *hostinfo) getCerts(timeout time.Duration) error {
}

func CertInfo(host string, port string, timeout time.Duration) (commonName string, dnsNames []string, err error) {
port_int,err := strconv.Atoi(port)
if err != nil{
port_int, err := strconv.Atoi(port)
if err != nil {
return commonName, dnsNames, err
}
info := hostinfo{Host: host, Port: port_int}
Expand All @@ -331,20 +323,20 @@ func CertInfo(host string, port string, timeout time.Duration) (commonName strin
return commonName, dnsNames, errors.New("not found")
}

func GetCert(domain string, port int)(string,error){
var CN string
var DN []string
var ret string
var err error
if port > 0 {
CN, DN, err = CertInfo(domain, strconv.Itoa(port), 5*time.Second)
}else{
CN, DN, err = CertInfo(domain, "443", 5*time.Second)
}
ret = "CommonName:"+CN+"; "
if len(DN)>0 {
ret = ret + "DNSName:"
ret = ret + DN[0]
}
return ret,err
func GetCert(domain string, port int) (string, error) {
var CN string
var DN []string
var ret string
var err error
if port > 0 {
CN, DN, err = CertInfo(domain, strconv.Itoa(port), 5*time.Second)
} else {
CN, DN, err = CertInfo(domain, "443", 5*time.Second)
}
ret = "CommonName:" + CN + "; "
if len(DN) > 0 {
ret = ret + "DNSName:"
ret = ret + DN[0]
}
return ret, err
}
73 changes: 73 additions & 0 deletions pkg/Ginfo/Gnbtscan/Gnbtscan.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package Gnbtscan

import (
"bytes"
"fmt"
"net"
"time"
)

func Scan(ip string) (string, error) {
payload := []byte{
// see https://blog.skullsecurity.org/2009/nbstatnse-a-replacement-for-nbtscan-and-others
0x13, 0x37, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x43, 0x4b,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
0x00, 0x00, 0x21, 0x00, 0x01,
}

conn, err := net.Dial("udp", fmt.Sprintf("%s:137", ip))
if err != nil {
return "", err
}
defer conn.Close()

conn.SetDeadline(time.Now().Add(2 * time.Second))
if _, err := conn.Write(payload); err != nil {
return "", err
}

buffer := make([]byte, 256)
bufferLen, err := conn.Read(buffer)
if err != nil {
return "", err
}

if bufferLen < 12 {
return "", fmt.Errorf("invalid header")
}

body := buffer[:bufferLen]
if body[6] == byte(0x00) && body[7] == byte(0x00) {
return "", fmt.Errorf("no answer to our request")
}

body = body[12:] // remove header
offset := 0
for body[offset] != 0 {
offset++
if offset == len(body) {
return "", fmt.Errorf("invalid payload")
}
}

body = body[offset+1:]
if len(body) < 12 {
return "", fmt.Errorf("no answer to our request")
}

nameCnt := body[10]
if nameCnt == 0 {
return "", fmt.Errorf("no names available")
}

offset = 0
names := body[11:]
for names[offset] != 0 {
offset++
if offset == len(names) {
break
}
}
return string(bytes.TrimSpace(names[:offset])), nil
}
26 changes: 26 additions & 0 deletions pkg/Ginfo/Gnbtscan/Gnbtscan_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package Gnbtscan

import (
"fmt"
"sync"
"testing"
)

func TestNbtscan(t *testing.T){
ipList := make([]int,255)
wg := sync.WaitGroup{}
wg.Add(255)
for index := range ipList {
ip := fmt.Sprintf("192.168.120.%d",index)
go func(){
result,err :=Scan(ip)
if err !=nil{
//t.Log(err)
}else{
t.Log(fmt.Sprintf("%s -> %s",ip,result))
}
wg.Done()
}()
}
wg.Wait()
}
36 changes: 21 additions & 15 deletions pkg/output/format_screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,43 @@ import (
"github.com/4dogs-cn/TXPortMap/pkg/Ginfo/Ghttp"
"github.com/4dogs-cn/TXPortMap/pkg/conversion"
"github.com/fatih/color"
"strings"
)

// formatScreen formats the output for showing on screen.
func (w *StandardWriter) formatScreen(output *ResultEvent) []byte {
builder := &bytes.Buffer{}
builder.WriteRune('[')
builder.WriteString(color.CyanString(output.Time.Format("2006-01-02 15:04:05")))
builder.WriteString("] ")
builder.WriteRune('[')
builder.WriteString(color.RedString(output.Target))
builder.WriteString("] ")
builder.WriteRune('[')
builder.WriteString(" ")
builder.WriteString(color.YellowString(output.Info.Service))
builder.WriteString("] ")

if output.Info.Service == "ssl/tls" || output.Info.Service == "http"{
builder.WriteRune('[')
builder.WriteString(color.YellowString(output.Info.Cert))
builder.WriteString("] ")
if len(output.Info.Cert) > 0 {
builder.WriteString(" [")
builder.WriteString(color.YellowString(output.Info.Cert))
builder.WriteString("]")
}
}
if output.WorkingEvent != nil{
switch tmp := output.WorkingEvent.(type) {
case Ghttp.Result:
builder.WriteString(tmp.ToString())
httpBanner := tmp.ToString()
if len(httpBanner)>0 {
builder.WriteString(" | ")
builder.WriteString(httpBanner)
}
default:
builder.WriteString(conversion.ToString(tmp))
result := conversion.ToString(tmp)
if strings.HasPrefix(result,"\\x") == false && len(result)>0 {
builder.WriteString(" | ")
builder.WriteString(result)
}
}
}else{
builder.WriteRune('[')
builder.WriteString(color.GreenString(output.Info.Banner))
builder.WriteString("] ")
if strings.HasPrefix(output.Info.Banner, "\\x") == false && len(output.Info.Banner)>0{
builder.WriteString(" | ")
builder.WriteString(color.GreenString(output.Info.Banner))
}
}
return builder.Bytes()
}

0 comments on commit 332ff68

Please sign in to comment.