2024-07-22 04:49:18 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2024-08-22 08:49:05 +00:00
|
|
|
"context"
|
2024-07-22 10:59:11 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
|
2024-07-22 04:49:18 +00:00
|
|
|
"github.com/xvzc/SpoofDPI/packet"
|
2024-09-08 08:38:59 +00:00
|
|
|
"github.com/xvzc/SpoofDPI/util"
|
2024-08-22 08:49:05 +00:00
|
|
|
"github.com/xvzc/SpoofDPI/util/log"
|
2024-07-22 04:49:18 +00:00
|
|
|
)
|
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
const protoHTTPS = "HTTPS"
|
|
|
|
|
|
|
|
func (pxy *Proxy) handleHttps(ctx context.Context, lConn *net.TCPConn, exploit bool, initPkt *packet.HttpRequest, ip string) {
|
|
|
|
ctx = util.GetCtxWithScope(ctx, protoHTTPS)
|
|
|
|
logger := log.GetCtxLogger(ctx)
|
|
|
|
|
2024-07-22 04:49:18 +00:00
|
|
|
// Create a connection to the requested server
|
2024-07-22 10:59:11 +00:00
|
|
|
var port int = 443
|
|
|
|
var err error
|
2024-07-22 04:49:18 +00:00
|
|
|
if initPkt.Port() != "" {
|
2024-07-22 10:59:11 +00:00
|
|
|
port, err = strconv.Atoi(initPkt.Port())
|
|
|
|
if err != nil {
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("error parsing port for %s aborting..", initPkt.Domain())
|
2024-07-22 10:59:11 +00:00
|
|
|
}
|
2024-07-22 04:49:18 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 10:59:11 +00:00
|
|
|
rConn, err := net.DialTCP("tcp", nil, &net.TCPAddr{IP: net.ParseIP(ip), Port: port})
|
2024-07-22 04:49:18 +00:00
|
|
|
if err != nil {
|
2024-07-22 10:59:11 +00:00
|
|
|
lConn.Close()
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("%s", err)
|
2024-07-22 04:49:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("new connection to the server %s -> %s", rConn.LocalAddr(), initPkt.Domain())
|
2024-07-22 04:49:18 +00:00
|
|
|
|
|
|
|
_, err = lConn.Write([]byte(initPkt.Version() + " 200 Connection Established\r\n\r\n"))
|
|
|
|
if err != nil {
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("error sending 200 connection established to the client: %s", err)
|
2024-07-22 04:49:18 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-08 08:38:59 +00:00
|
|
|
logger.Debug().Msgf("sent connection established to %s", lConn.RemoteAddr())
|
2024-07-22 04:49:18 +00:00
|
|
|
|
|
|
|
// Read client hello
|
2024-08-14 08:01:14 +00:00
|
|
|
m, err := packet.ReadTLSMessage(lConn)
|
|
|
|
if err != nil || !m.IsClientHello() {
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("error reading client hello from %s: %s", lConn.RemoteAddr().String(), err)
|
2024-07-22 04:49:18 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-13 21:33:58 +00:00
|
|
|
clientHello := m.Raw
|
2024-07-22 04:49:18 +00:00
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("client sent hello %d bytes", len(clientHello))
|
2024-07-22 04:49:18 +00:00
|
|
|
|
|
|
|
// Generate a go routine that reads from the server
|
2024-08-22 08:49:05 +00:00
|
|
|
go Serve(ctx, rConn, lConn, protoHTTPS, initPkt.Domain(), lConn.RemoteAddr().String(), pxy.timeout)
|
2024-07-22 04:49:18 +00:00
|
|
|
|
2024-08-06 08:48:18 +00:00
|
|
|
if exploit {
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("writing chunked client hello to %s", initPkt.Domain())
|
|
|
|
chunks := splitInChunks(ctx, clientHello, pxy.windowSize)
|
2024-08-14 08:01:14 +00:00
|
|
|
if _, err := writeChunks(rConn, chunks); err != nil {
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("error writing chunked client hello to %s: %s", initPkt.Domain(), err)
|
2024-07-22 10:59:11 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-06 08:48:18 +00:00
|
|
|
} else {
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("writing plain client hello to %s", initPkt.Domain())
|
2024-08-14 08:01:14 +00:00
|
|
|
if _, err := rConn.Write(clientHello); err != nil {
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("error writing plain client hello to %s: %s", initPkt.Domain(), err)
|
2024-08-06 08:48:18 +00:00
|
|
|
return
|
|
|
|
}
|
2024-07-22 10:59:11 +00:00
|
|
|
}
|
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
go Serve(ctx, lConn, rConn, protoHTTPS, lConn.RemoteAddr().String(), initPkt.Domain(), pxy.timeout)
|
2024-07-22 04:49:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
func splitInChunks(ctx context.Context, bytes []byte, size int) [][]byte {
|
|
|
|
logger := log.GetCtxLogger(ctx)
|
|
|
|
|
2024-07-22 04:49:18 +00:00
|
|
|
var chunks [][]byte
|
|
|
|
var raw []byte = bytes
|
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msgf("window-size: %d", size)
|
2024-08-03 07:43:51 +00:00
|
|
|
|
|
|
|
if size > 0 {
|
2024-07-23 00:41:13 +00:00
|
|
|
for {
|
|
|
|
if len(raw) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// necessary check to avoid slicing beyond
|
|
|
|
// slice capacity
|
|
|
|
if len(raw) < size {
|
|
|
|
size = len(raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
chunks = append(chunks, raw[0:size])
|
|
|
|
raw = raw[size:]
|
2024-07-22 04:49:18 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 00:41:13 +00:00
|
|
|
return chunks
|
|
|
|
}
|
2024-07-22 04:49:18 +00:00
|
|
|
|
2024-08-06 08:48:18 +00:00
|
|
|
// When the given window-size <= 0
|
2024-07-27 02:00:53 +00:00
|
|
|
|
2024-07-23 00:41:13 +00:00
|
|
|
if len(raw) < 1 {
|
|
|
|
return [][]byte{raw}
|
2024-07-22 04:49:18 +00:00
|
|
|
}
|
|
|
|
|
2024-08-22 08:49:05 +00:00
|
|
|
logger.Debug().Msg("using legacy fragmentation")
|
2024-07-23 00:41:13 +00:00
|
|
|
|
|
|
|
return [][]byte{raw[:1], raw[1:]}
|
2024-07-22 04:49:18 +00:00
|
|
|
}
|
2024-08-14 08:01:14 +00:00
|
|
|
|
|
|
|
func writeChunks(conn *net.TCPConn, c [][]byte) (n int, err error) {
|
|
|
|
total := 0
|
|
|
|
for i := 0; i < len(c); i++ {
|
|
|
|
b, err := conn.Write(c[i])
|
|
|
|
if err != nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
total += b
|
|
|
|
}
|
|
|
|
|
|
|
|
return total, nil
|
|
|
|
}
|