mirror of
https://github.com/phishingclub/phishingclub.git
synced 2026-12-06 01:53:42 +01:00
125 lines
4.1 KiB
Go
125 lines
4.1 KiB
Go
package proxy
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// allowedProxySchemes are the forward proxy schemes the upstream client can
|
|
// dial. socks4 is left out on purpose, the same as validateForwardProxy in the
|
|
// proxy service, because the standard library dialer cannot reach a socks4
|
|
// proxy.
|
|
var allowedProxySchemes = map[string]bool{
|
|
"http": true,
|
|
"https": true,
|
|
"socks5": true,
|
|
}
|
|
|
|
// createUpstreamClient builds the http.Client that forwards a proxied request
|
|
// to the target server. It uses the standard library transport. The client is
|
|
// built after the session is resolved so it can use the forward proxy the
|
|
// session script picked for this session.
|
|
//
|
|
// The forward proxy is the proxy from the yaml config by default. A session
|
|
// script may override it per session. The override is checked against the
|
|
// allowed schemes and ignored, falling back to the config proxy, when it is not
|
|
// usable, the same as when the script itself errors.
|
|
func (m *ProxyHandler) createUpstreamClient(reqCtx *RequestContext) (*http.Client, error) {
|
|
proxyConfig := reqCtx.ProxyConfig
|
|
|
|
proxyStr := proxyConfig.Proxy
|
|
if reqCtx.Session != nil && reqCtx.Session.UpstreamProxy != "" {
|
|
override := reqCtx.Session.UpstreamProxy
|
|
if _, err := m.parseAllowedProxyURL(override); err != nil {
|
|
m.logger.Warnw("session script proxy rejected, using config proxy",
|
|
"error", err,
|
|
)
|
|
} else {
|
|
proxyStr = override
|
|
}
|
|
}
|
|
|
|
// default to the environment proxy settings so the behavior matches the
|
|
// previous client when no proxy is configured. when a proxy is set, route
|
|
// every request through it.
|
|
proxyFunc := http.ProxyFromEnvironment
|
|
if proxyStr != "" {
|
|
proxyURL, err := m.parseAllowedProxyURL(proxyStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
proxyFunc = http.ProxyURL(proxyURL)
|
|
m.logger.Debugw("configured upstream client with proxy",
|
|
"proxy", proxyURL.Redacted(),
|
|
)
|
|
}
|
|
|
|
transport := &http.Transport{
|
|
Proxy: proxyFunc,
|
|
DialContext: (&net.Dialer{
|
|
Timeout: 10 * time.Second,
|
|
KeepAlive: 15 * time.Second,
|
|
}).DialContext,
|
|
ForceAttemptHTTP2: true,
|
|
DisableCompression: true,
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
ResponseHeaderTimeout: 10 * time.Second,
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
IdleConnTimeout: 20 * time.Second,
|
|
MaxIdleConns: 512,
|
|
MaxIdleConnsPerHost: 128,
|
|
MaxConnsPerHost: 128,
|
|
// the previous client did not verify the target certificate, so a
|
|
// target that serves its own certificate keeps working. the proxy
|
|
// carries victim traffic to the operator chosen target.
|
|
// #nosec G402 target verification is off to preserve proxy behavior
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
}
|
|
|
|
return &http.Client{
|
|
Transport: transport,
|
|
Timeout: 30 * time.Second,
|
|
}, nil
|
|
}
|
|
|
|
// parseAllowedProxyURL parses a forward proxy string and rejects any scheme the
|
|
// upstream client cannot dial. Only http, https and socks5 are allowed.
|
|
func (m *ProxyHandler) parseAllowedProxyURL(proxyStr string) (*url.URL, error) {
|
|
proxyURL, err := m.parseProxyURL(proxyStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !allowedProxySchemes[strings.ToLower(proxyURL.Scheme)] {
|
|
return nil, fmt.Errorf("unsupported proxy scheme %q", proxyURL.Scheme)
|
|
}
|
|
return proxyURL, nil
|
|
}
|
|
|
|
// parseProxyURL parses and normalizes the proxy URL string.
|
|
// If the proxy string is just an IP:port, it prepends "http://".
|
|
// Otherwise it uses the full string to support socks5 and authentication.
|
|
func (m *ProxyHandler) parseProxyURL(proxyStr string) (*url.URL, error) {
|
|
// check if the string already contains a scheme (http://, https://, socks5://)
|
|
hasScheme := strings.Contains(proxyStr, "://")
|
|
|
|
// check if it contains authentication credentials
|
|
hasAuth := strings.Contains(proxyStr, "@")
|
|
|
|
// if it has a scheme or auth, use it unchanged
|
|
if hasScheme || hasAuth {
|
|
// if it has auth but no scheme, default to http://
|
|
if hasAuth && !hasScheme {
|
|
proxyStr = "http://" + proxyStr
|
|
}
|
|
return url.Parse(proxyStr)
|
|
}
|
|
|
|
// otherwise, it's just an IP:port, so prepend http://
|
|
return url.Parse("http://" + proxyStr)
|
|
}
|