/
utils.go
240 lines (206 loc) · 6.81 KB
/
utils.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* NETCAP - Traffic Analysis Framework
* Copyright (c) 2017-2020 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package proxy
import (
"bytes"
"compress/gzip"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httputil"
"os"
"os/signal"
"strings"
"syscall"
"github.com/dreadl0ck/netcap/io"
)
func printHeader() {
io.PrintLogo()
fmt.Println()
fmt.Println("proxy tool usage examples:")
fmt.Println(" $ net proxy -local 127.0.0.1:4444 -remote https://github.com")
fmt.Println(" $ net proxy -local 127.0.0.1:4444 -remote https://github.com -maxIdle 300")
fmt.Println(" $ net proxy -local 127.0.0.1:4444 -remote https://github.com -dump")
fmt.Println()
}
// usage prints the use.
func printUsage() {
printHeader()
fs.PrintDefaults()
}
// cleanup when receiving OS signals.
func cleanup() {
for _, p := range proxies {
// pass numRecords > 0 so that files do not get removed.
// TODO: add support to determine the correct number of records at this place
p.writer.Close(1)
}
}
// handle OS signals.
func handleSignals() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
// start signal handler and cleanup routine
go func() {
sig := <-sigs
fmt.Println("\nreceived signal:", sig)
fmt.Println("exiting")
cleanup()
os.Exit(0)
}()
}
// TrimPortIPv4 trims the port number from an IPv4 address string.
//func TrimPortIPv4(addr string) string {
// slice := strings.Split(addr, ":")
// if len(slice) == 2 {
// return slice[0]
// }
// return addr
//}
// dumpHTTPResponse dumps an http.Response for debugging purposes.
func dumpHTTPResponse(resp *http.Response, proxyName string, rawbody []byte) {
fmt.Println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------")
fmt.Println(proxyName + " received an HTTP Response:")
fmt.Println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------")
var deflated []byte
if resp.Header.Get("Content-Encoding") == "gzip" {
var gzipBuf bytes.Buffer
gzipBuf.Write(rawbody)
gr, err := gzip.NewReader(&gzipBuf)
if err != nil {
log.Fatal("failed to create gzip reader for response: ", err)
}
deflated, err = ioutil.ReadAll(gr)
if err != nil {
log.Fatal("failed to decompress gzipped response: ", err)
}
}
var (
contentType = resp.Header.Get("Content-Type")
isHTML = strings.Contains(contentType, "text/html")
)
if len(deflated) != 0 || isHTML {
// dump header only
data, err := httputil.DumpResponse(resp, false)
if err != nil {
log.Println("failed to read response header")
}
fmt.Println(string(data))
// dump deflated content if there is any
// and if its not HTML
if len(deflated) != 0 && !isHTML {
fmt.Println(string(deflated))
}
} else {
if !strings.Contains(contentType, "multipart") {
// dump full response
data, err := httputil.DumpResponse(resp, true)
if err != nil {
log.Println("failed to read response header")
}
fmt.Println(string(data))
} else {
fmt.Println("multipart form data: ", len(rawbody))
}
}
fmt.Println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------")
}
// dumpHTTPRequest dumps an http.Request for debugging purposes.
func dumpHTTPRequest(req *http.Request, proxyName string) {
fmt.Println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------")
fmt.Println(proxyName+" received an HTTP Request: ", req.URL)
fmt.Println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------")
data, err := httputil.DumpRequest(req, true)
if err != nil {
fmt.Println("failed to read req body for debug message")
} else {
fmt.Println(string(data))
}
fmt.Println("--------------------------------------------------------------------------------------------------------------------------------------------------------------------")
}
// Get Remote Address, handles load balancers
// see: https://husobee.github.io/golang/ip-address/2015/12/17/remote-ip-go.html
// ipRange - a structure that holds the start and end of a range of ip addresses.
type ipRange struct {
start net.IP
end net.IP
}
// inRange - check to see if a given ip address is within a range given.
func inRange(r ipRange, ipAddress net.IP) bool {
// strcmp type byte comparison
if bytes.Compare(ipAddress, r.start) >= 0 && bytes.Compare(ipAddress, r.end) < 0 {
return true
}
return false
}
var privateRanges = []ipRange{
{
start: net.ParseIP("10.0.0.0"),
end: net.ParseIP("10.255.255.255"),
},
{
start: net.ParseIP("100.64.0.0"),
end: net.ParseIP("100.127.255.255"),
},
{
start: net.ParseIP("172.16.0.0"),
end: net.ParseIP("172.31.255.255"),
},
{
start: net.ParseIP("192.0.0.0"),
end: net.ParseIP("192.0.0.255"),
},
{
start: net.ParseIP("192.168.0.0"),
end: net.ParseIP("192.168.255.255"),
},
{
start: net.ParseIP("198.18.0.0"),
end: net.ParseIP("198.19.255.255"),
},
}
// isPrivateSubnet - check to see if this ip is in a private subnet.
func isPrivateSubnet(ipAddress net.IP) bool {
// my use case is only concerned with ipv4 atm
if ipCheck := ipAddress.To4(); ipCheck != nil {
// iterate over all our ranges
for _, r := range privateRanges {
// check if this ip is in a private range
if inRange(r, ipAddress) {
return true
}
}
}
return false
}
// getIPAddress will retrieve the ip address of a http.Request.
func getIPAdress(r *http.Request) string {
for _, h := range []string{"X-Forwarded-For", "X-Real-Ip"} {
addresses := strings.Split(r.Header.Get(h), ",")
// march from right to left until we get a public address
// that will be the address right before our proxy.
for i := len(addresses) - 1; i >= 0; i-- {
ip := strings.TrimSpace(addresses[i])
// header can contain spaces too, strip those out.
realIP := net.ParseIP(ip)
if !realIP.IsGlobalUnicast() || isPrivateSubnet(realIP) {
// bad address, go to next
continue
}
return ip
}
}
return ""
}