-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw.go
99 lines (81 loc) · 1.84 KB
/
raw.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bufio"
"flag"
"fmt"
"net"
"os"
"strings"
"sync"
"time"
)
var (
bannerString string
port string
output string
timeout time.Duration
threads int
)
func init() {
flag.StringVar(&bannerString, "b", "", "string to search for within the banner")
flag.StringVar(&port, "p", "80", "port number")
flag.StringVar(&output, "o", "output.txt", "output file")
flag.DurationVar(&timeout, "timeout", 5*time.Second, "timeout for banner grab")
flag.IntVar(&threads, "t", 1, "number of threads")
}
func grabBanner(ip string, port string) (string, error) {
conn, err := net.DialTimeout("tcp", ip+":"+port, timeout)
if err != nil {
return "", err
}
defer conn.Close()
conn.SetReadDeadline(time.Now().Add(timeout))
buffer := make([]byte, 4096)
n, err := conn.Read(buffer)
if err != nil {
return "", err
}
return string(buffer[:n]), nil
}
func main() {
flag.Parse()
var wg sync.WaitGroup
sem := make(chan struct{}, threads)
file, err := os.Create(output)
if err != nil {
fmt.Println("Error:", err)
return
}
defer file.Close()
writer := bufio.NewWriter(file)
defer writer.Flush()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
ip := scanner.Text()
wg.Add(1)
sem <- struct{}{}
go func(ip string) {
defer func() {
<-sem
wg.Done()
}()
banner, err := grabBanner(ip, port)
if err != nil {
fmt.Fprintf(os.Stderr, "Error grabbing banner: %v\n", err)
return
}
if contains(banner, bannerString) || bannerString == "" {
outputString := fmt.Sprintf("%s:%s\n%s\n\n", ip, port, banner)
fmt.Fprint(writer, outputString)
writer.Flush()
}
}(ip)
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "Error reading from input:", err)
}
wg.Wait()
}
func contains(s, substr string) bool {
return strings.Contains(s, substr)
}