From 99658cb49ce60cc9dd7a85503608e0bd19531406 Mon Sep 17 00:00:00 2001 From: Christian Ulrich Date: Tue, 6 Oct 2020 23:50:13 +0200 Subject: [PATCH] close raw sockets too --- tcp_syni.nim | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/tcp_syni.nim b/tcp_syni.nim index 8e66def..0587b21 100644 --- a/tcp_syni.nim +++ b/tcp_syni.nim @@ -95,6 +95,7 @@ proc injectTcpPacket(rawFd: AsyncFD, ipPacket: IpPacket) {.async.} = proc captureSeqNumbers(attempt: ConnectAttempt, rawFd: AsyncFD, cb: PunchProgressCb) {.async.} = # FIXME: timeout? + # FIXME: create raw socket here var seqNums = newSeq[uint32]() while seqNums.len < attempt.dstPorts.len: let packet = await rawFd.recv(4000) @@ -110,26 +111,31 @@ proc captureSeqNumbers(attempt: ConnectAttempt, rawFd: AsyncFD, if parsed.tcpPortDst.int == port.int: seqNums.add(parsed.tcpSeqNumber) break + closeSocket(rawFd) await cb(seqNums) proc captureAndResendAck(attempt: ConnectAttempt, captureFd: AsyncFD, injectFd: AsyncFD) {.async.} = - while true: - let packet = await captureFd.recv(4000) - if packet == "": - break - var parsed = parseEthernetPacket(packet) - if parsed.protocol == tcp and - parsed.ipAddrSrc == attempt.srcIp and - parsed.tcpPortSrc.int == attempt.srcPort.int and - parsed.ipAddrDst == attempt.dstIp and - parsed.tcpFlags == {ACK}: - for port in attempt.dstPorts: - if parsed.tcpPortDst.int == port.int: - parsed.ipTTL = 64 - echo &"[{parsed.ipAddrSrc}:{parsed.tcpPortSrc.int} -> {parsed.ipAddrDst}:{parsed.tcpPortDst}, SEQ {parsed.tcpSeqNumber}] resending ACK with TTL {parsed.ipTTL}" - await injectFd.injectTcpPacket(parsed) - return + # FIXME: create raw socket here + block loops: + while true: + let packet = await captureFd.recv(4000) + if packet == "": + break + var parsed = parseEthernetPacket(packet) + if parsed.protocol == tcp and + parsed.ipAddrSrc == attempt.srcIp and + parsed.tcpPortSrc.int == attempt.srcPort.int and + parsed.ipAddrDst == attempt.dstIp and + parsed.tcpFlags == {ACK}: + for port in attempt.dstPorts: + if parsed.tcpPortDst.int == port.int: + parsed.ipTTL = 64 + echo &"[{parsed.ipAddrSrc}:{parsed.tcpPortSrc.int} -> {parsed.ipAddrDst}:{parsed.tcpPortDst}, SEQ {parsed.tcpSeqNumber}] resending ACK with TTL {parsed.ipTTL}" + await injectFd.injectTcpPacket(parsed) + break loops + closeSocket(captureFd) + closeSocket(injectFd) proc initPuncher*(): TcpSyniPuncher = TcpSyniPuncher() @@ -291,6 +297,7 @@ proc accept*(puncher: TcpSyniPuncher, srcPort: Port, dstIp: IpAddress, tcpFlags: {SYN}, tcpWindowSize: 1452 * 10) echo &"[{ipPacket.ipAddrSrc}:{ipPacket.tcpPortSrc} -> {ipPacket.ipAddrDst}:{ipPacket.tcpPortDst}, SEQ {ipPacket.tcpSeqNumber}] injecting SYN" asyncCheck rawFd.injectTcpPacket(ipPacket) + closeSocket(rawFd) await attempt.future or sleepAsync(Timeout) await attempt.deleteFirewallRules() puncher.acceptAttempts.del(puncher.acceptAttempts.find(attempt))