-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgo-scan-http.go
121 lines (106 loc) · 3.24 KB
/
go-scan-http.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
go-scan-http -- Fast http network scanner
Copyright (C) 2019 Lars Kiesow <lkiesow@uos.de>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// go-scan-http scans a range of IPv4 addresses for HTTP servers by sending a
// simple, short request and listening for any kind of answer. It keeps the
// response header for further investigation.
package main
import (
"bufio"
"errors"
"fmt"
"net"
"sync"
"time"
)
var requestBegin = []byte("HEAD / HTTP/1.1\r\nHost:")
var requestEnd = []byte("\r\n\r\n")
const timeout time.Duration = 5 * time.Second
// probe takes a single IPv4 address and sends a simple HTTP request to this
// address. It returns the resulting HTTP header or an error.
func probe(addr string) (string, error) {
d := net.Dialer{Timeout: timeout}
conn, err := d.Dial("tcp", addr)
if err != nil {
return "", err
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(timeout))
conn.Write(requestBegin)
conn.Write([]byte(addr))
conn.Write(requestEnd)
reader := bufio.NewReader(conn)
header := addr + "\n"
for {
line, _ := reader.ReadString('\n')
header += line
if len(line) <= 2 {
if line == "" {
return "", errors.New("read timeout for " + addr)
}
break
}
}
return header, nil
}
// handleResults reads and prints the number of results defined by num from the
// results channel and writes a single value to the done channel once it is
// finished.
func handleResults(results <-chan string, waitgroup *sync.WaitGroup) {
for {
addr := <-results
fmt.Println(addr)
waitgroup.Done()
}
}
// main is the entry point for the executable.
func main() {
settings := parseArgs()
results := make(chan string, settings.threads)
maxqueue := make(chan struct{}, settings.threads)
waitgroup := sync.WaitGroup{}
// result handler
nRequests := len(settings.ports)
for i := 0; i < 4; i++ {
nRequests *= 1 + int(settings.bytes[i][1]) - int(settings.bytes[i][0])
}
waitgroup.Add(nRequests)
// initialize result handler
go handleResults(results, &waitgroup)
// launch requests
for b0 := settings.bytes[0][0]; b0 <= settings.bytes[0][1]; b0++ {
for b1 := settings.bytes[1][0]; b1 <= settings.bytes[1][1]; b1++ {
for b2 := settings.bytes[2][0]; b2 <= settings.bytes[2][1]; b2++ {
for b3 := settings.bytes[3][0]; b3 <= settings.bytes[3][1]; b3++ {
for _, port := range settings.ports {
addr := fmt.Sprintf("%d.%d.%d.%d:%d",
b0, b1, b2, b3, port)
maxqueue <- struct{}{}
go func() {
header, err := probe(addr)
if err != nil {
results <- fmt.Sprintf("%s", err)
} else {
results <- header
}
<-maxqueue
}()
}
}
}
}
}
// wait until all messages are handled
waitgroup.Wait()
}