Repository navigation
Expand file tree
/
Copy pathproxy.go
More file actions
278 lines (245 loc) · 7 KB
/
Copy pathproxy.go
File metadata and controls
278 lines (245 loc) · 7 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package engine
import (
"bufio"
"context"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
"github.com/projectdiscovery/nuclei/v3/pkg/types"
)
// headlessProxy is a local loopback HTTP/CONNECT proxy that intercepts
// and enforces network policy on all network traffic initiated by the headless browser,
// including WebSocket handshakes (which bypass CDP request blocking in Chromium).
type headlessProxy struct {
listener net.Listener
options *types.Options
closed chan struct{}
closeOnce sync.Once
}
// newHeadlessProxy creates and starts a loopback HTTP/CONNECT proxy for headless Chrome.
func newHeadlessProxy(options *types.Options) (*headlessProxy, error) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return nil, fmt.Errorf("could not start headless proxy listener: %w", err)
}
p := &headlessProxy{
listener: ln,
options: options,
closed: make(chan struct{}),
}
go p.serve()
return p, nil
}
// Addr returns the listening address of the proxy (e.g. "127.0.0.1:54321").
func (p *headlessProxy) Addr() string {
if p.listener == nil {
return ""
}
return p.listener.Addr().String()
}
// Close terminates the proxy listener.
func (p *headlessProxy) Close() error {
var err error
p.closeOnce.Do(func() {
close(p.closed)
if p.listener != nil {
err = p.listener.Close()
}
})
return err
}
func (p *headlessProxy) serve() {
for {
conn, err := p.listener.Accept()
if err != nil {
select {
case <-p.closed:
return
default:
return
}
}
go p.handleConn(conn)
}
}
func (p *headlessProxy) handleConn(clientConn net.Conn) {
defer func() {
_ = clientConn.Close()
}()
reader := bufio.NewReader(clientConn)
req, err := http.ReadRequest(reader)
if err != nil {
return
}
host := req.Host
if host == "" {
host = req.URL.Host
}
// Validate host against network policy
executionId := ""
if p.options != nil {
executionId = p.options.ExecutionId
}
h := host
if splitH, _, err := net.SplitHostPort(host); err == nil {
h = splitH
}
canonH := protocolstate.CanonicalizeHost(h)
if !protocolstate.IsHostAllowed(executionId, host) || !protocolstate.IsHostAllowed(executionId, canonH) {
if req.Method == http.MethodConnect {
_, _ = clientConn.Write([]byte("HTTP/1.1 403 Forbidden\r\n\r\n"))
} else {
resp := &http.Response{
StatusCode: http.StatusForbidden,
Status: "403 Forbidden",
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Header: make(http.Header),
Body: io.NopCloser(strings.NewReader("Blocked by network policy")),
}
_ = resp.Write(clientConn)
}
return
}
// Handle CONNECT tunnel (for HTTPS and WebSocket handshakes).
// Keep the buffered reader: the client hello is often already in it.
if req.Method == http.MethodConnect {
p.handleConnect(clientConn, reader, host)
return
}
// Handle standard HTTP requests
p.handleHTTP(clientConn, reader, req, host)
}
func (p *headlessProxy) dialTarget(target string) (net.Conn, error) {
host := target
port := "80"
if h, pt, err := net.SplitHostPort(target); err == nil {
host = h
port = pt
}
canonHost := protocolstate.CanonicalizeHost(host)
executionId := ""
if p.options != nil {
executionId = p.options.ExecutionId
}
if !protocolstate.IsHostAllowed(executionId, host) || !protocolstate.IsHostAllowed(executionId, canonHost) {
return nil, fmt.Errorf("target %s blocked by network policy", target)
}
// Dial an address that was just checked. A later lookup of the name can
// return a different, excluded address. A resolution failure refuses the
// dial instead of connecting to whatever the name means a moment later.
dialHost := canonHost
if net.ParseIP(canonHost) == nil {
ips, err := net.LookupIP(canonHost)
if err != nil {
return nil, fmt.Errorf("resolve %s: %w", canonHost, err)
}
var chosen net.IP
for _, resolvedIP := range ips {
canonIP := protocolstate.CanonicalizeHost(resolvedIP.String())
if !protocolstate.IsHostAllowed(executionId, canonIP) {
return nil, fmt.Errorf("target %s resolves to blocked IP %s", target, canonIP)
}
if chosen == nil {
chosen = resolvedIP
}
}
if chosen == nil {
return nil, fmt.Errorf("resolve %s: no addresses", canonHost)
}
dialHost = chosen.String()
}
dialAddr := net.JoinHostPort(dialHost, port)
// If upstream proxy is configured, route through it after validation
if p.options != nil && p.options.AliveHttpProxy != "" {
proxyURL, err := url.Parse(p.options.AliveHttpProxy)
if err == nil {
proxyConn, err := net.DialTimeout("tcp", proxyURL.Host, 10*time.Second)
if err != nil {
return nil, err
}
// CONNECT through upstream proxy with canonical address
connectReq := fmt.Sprintf("CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n", dialAddr, dialAddr)
if _, err := proxyConn.Write([]byte(connectReq)); err != nil {
_ = proxyConn.Close()
return nil, err
}
resp, err := http.ReadResponse(bufio.NewReader(proxyConn), nil)
if err != nil || resp.StatusCode != http.StatusOK {
_ = proxyConn.Close()
if err != nil {
return nil, err
}
return nil, fmt.Errorf("upstream proxy CONNECT returned %d", resp.StatusCode)
}
return proxyConn, nil
}
}
// Use fastdialer if available for network policy compliance and DNS caching
if dialer := protocolstate.GetDialersWithId(executionId); dialer != nil && dialer.Fastdialer != nil {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
return dialer.Fastdialer.Dial(ctx, "tcp", dialAddr)
}
return net.DialTimeout("tcp", dialAddr, 10*time.Second)
}
func (p *headlessProxy) handleConnect(clientConn net.Conn, clientReader *bufio.Reader, target string) {
destConn, err := p.dialTarget(target)
if err != nil {
_, _ = clientConn.Write([]byte("HTTP/1.1 502 Bad Gateway\r\n\r\n"))
return
}
defer func() {
_ = destConn.Close()
}()
if _, err := clientConn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n")); err != nil {
return
}
// Tunnel traffic bidirectionally. clientReader carries bytes already
// read with the CONNECT request. One direction ending is enough to
// tear the tunnel down.
done := make(chan struct{}, 2)
go func() {
_, _ = io.Copy(destConn, clientReader)
done <- struct{}{}
}()
go func() {
_, _ = io.Copy(clientConn, destConn)
done <- struct{}{}
}()
<-done
}
func (p *headlessProxy) handleHTTP(clientConn net.Conn, clientReader *bufio.Reader, req *http.Request, host string) {
destHost := host
if !strings.Contains(destHost, ":") {
destHost += ":80"
}
destConn, err := p.dialTarget(destHost)
if err != nil {
_, _ = clientConn.Write([]byte("HTTP/1.1 502 Bad Gateway\r\n\r\n"))
return
}
defer func() {
_ = destConn.Close()
}()
if err := req.Write(destConn); err != nil {
return
}
done := make(chan struct{}, 2)
go func() {
_, _ = io.Copy(destConn, clientReader)
done <- struct{}{}
}()
go func() {
_, _ = io.Copy(clientConn, destConn)
done <- struct{}{}
}()
<-done
}