-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from hanc00l/custom_dev
Update: 修改输出结果的显示方式,增加nbtscan代码
- Loading branch information
Showing
4 changed files
with
145 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters