-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrabber.go
70 lines (59 loc) · 1.74 KB
/
grabber.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"fmt"
"io"
"net"
"os"
"strconv"
"strings"
"time"
)
func getBannerMessageData(port int) []byte {
return MessageData[PortMappings[port]]
}
func getBannerProtocolProbedProtocol(port int) string {
mapping := PortMappings[port]
if mapping == "" {
fmt.Fprintf(os.Stderr, "[error] No banner file for port: %d", port)
}
return mapping
}
// Read addresses from addrChan and grab banners from these hosts.
// Sends resultStructs to resultChan. Writes to doneChan when complete.
func GrabBanners(addrChan chan JsonRawIpPort, resultChan chan ProbeResult, doneChan chan int) {
for addr := range addrChan {
protocol := getBannerProtocolProbedProtocol(addr.Port)
deadline := time.Now().Add(time.Duration(*timeoutFlag) * time.Second)
dialer := net.Dialer{Deadline: deadline}
conn, err := dialer.Dial("tcp", net.JoinHostPort(addr.Ip, strconv.Itoa(addr.Port)))
if err != nil {
resultChan <- ProbeResult{addr.Ip, addr.Port, protocol, nil, fmt.Sprintf("%s", err)}
continue
}
conn.SetDeadline(deadline)
s := strings.Replace(string(getBannerMessageData(addr.Port)), "%s", addr.Ip, -1)
offset := 0
var buf [1024]byte
var connectionError error
for _, line := range strings.Split(s, "##WAIT_ANSWER##\n") {
if _, err := conn.Write([]byte(line)); err != nil {
connectionError = err
break
}
n, err := conn.Read(buf[offset:])
if err != nil && (err != io.EOF || offset == 0) {
connectionError = err
break
}
offset += n
}
if connectionError != nil {
conn.Close()
resultChan <- ProbeResult{addr.Ip, addr.Port, protocol, nil, fmt.Sprintf("%s", connectionError)}
continue
}
conn.Close()
resultChan <- ProbeResult{addr.Ip, addr.Port, protocol, buf[0:offset], ""}
}
doneChan <- 1
}