SpoofDPI/proxy/https.go

119 lines
2.9 KiB
Go
Raw Normal View History

2024-07-22 04:49:18 +00:00
package proxy
import (
2024-07-22 10:59:11 +00:00
"net"
"strconv"
2024-07-22 04:49:18 +00:00
log "github.com/sirupsen/logrus"
"github.com/xvzc/SpoofDPI/packet"
)
func (pxy *Proxy) handleHttps(lConn *net.TCPConn, exploit bool, initPkt *packet.HttpPacket, ip string) {
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 {
log.Debug("[HTTPS] Error while parsing port for ", initPkt.Domain(), " aborting..")
}
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-07-22 04:49:18 +00:00
log.Debug("[HTTPS] ", err)
return
}
defer func() {
lConn.Close()
log.Debug("[HTTPS] Closing client Connection.. ", lConn.RemoteAddr())
rConn.Close()
log.Debug("[HTTPS] Closing server Connection.. ", initPkt.Domain(), " ", rConn.LocalAddr())
}()
log.Debug("[HTTPS] New connection to the server ", initPkt.Domain(), " ", rConn.LocalAddr())
_, err = lConn.Write([]byte(initPkt.Version() + " 200 Connection Established\r\n\r\n"))
if err != nil {
log.Debug("[HTTPS] Error sending 200 Connection Established to the client", err)
return
}
log.Debug("[HTTPS] Sent 200 Connection Estabalished to ", lConn.RemoteAddr())
// Read client hello
2024-08-11 13:54:06 +00:00
hello, err := ReadClientHello(lConn)
2024-07-22 04:49:18 +00:00
if err != nil {
log.Debug("[HTTPS] Error reading client hello from the client", err)
return
}
2024-08-11 13:54:06 +00:00
clientHello := hello.Raw
2024-07-22 04:49:18 +00:00
log.Debug("[HTTPS] Client sent hello ", len(clientHello), "bytes")
// Generate a go routine that reads from the server
chPkt := packet.NewHttpsPacket(clientHello)
2024-07-22 11:58:30 +00:00
// lConn.SetLinger(3)
// rConn.SetLinger(3)
2024-07-22 10:59:11 +00:00
go Serve(rConn, lConn, "[HTTPS]", rConn.RemoteAddr().String(), initPkt.Domain(), pxy.timeout, pxy.bufferSize)
2024-07-22 04:49:18 +00:00
if exploit {
2024-07-22 10:59:11 +00:00
log.Debug("[HTTPS] Writing chunked client hello to ", initPkt.Domain())
chunks := splitInChunks(chPkt.Raw(), pxy.windowSize)
2024-07-22 10:59:11 +00:00
if _, err := WriteChunks(rConn, chunks); err != nil {
log.Debug("[HTTPS] Error writing chunked client hello to ", initPkt.Domain(), err)
return
}
} else {
log.Debug("[HTTPS] Writing plain client hello to ", initPkt.Domain())
if _, err := rConn.Write(chPkt.Raw()); err != nil {
log.Debug("[HTTPS] Error writing plain client hello to ", initPkt.Domain(), err)
return
}
2024-07-22 10:59:11 +00:00
}
Serve(lConn, rConn, "[HTTPS]", lConn.RemoteAddr().String(), initPkt.Domain(), pxy.timeout, pxy.bufferSize)
2024-07-22 04:49:18 +00:00
}
func splitInChunks(bytes []byte, size int) [][]byte {
2024-07-22 04:49:18 +00:00
var chunks [][]byte
var raw []byte = bytes
log.Debug("[HTTPS] window-size: ", 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
// 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
}
log.Debug("[HTTPS] 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
}