more precise naming
This commit is contained in:
parent
17c4fa47f2
commit
a415842398
|
@ -47,7 +47,7 @@ type
|
||||||
tcp
|
tcp
|
||||||
other
|
other
|
||||||
|
|
||||||
PacketInfo* = object
|
IpPacket* = object
|
||||||
case protocol*: Protocol
|
case protocol*: Protocol
|
||||||
of tcp:
|
of tcp:
|
||||||
tcpIpSrc*: string
|
tcpIpSrc*: string
|
||||||
|
@ -62,21 +62,21 @@ var
|
||||||
ETHERTYPE_IP {.importc: "ETHERTYPE_IP", header: "<netinet/if_ether.h>".}: cushort
|
ETHERTYPE_IP {.importc: "ETHERTYPE_IP", header: "<netinet/if_ether.h>".}: cushort
|
||||||
IPPROTO_TCP {.importc: "IPPROTO_TCP", header: "<netinet/in.h>".}: cint
|
IPPROTO_TCP {.importc: "IPPROTO_TCP", header: "<netinet/in.h>".}: cint
|
||||||
|
|
||||||
proc fromString*(packet: string): PacketInfo =
|
proc parseEthernetPacket*(input: string): IpPacket =
|
||||||
let etherHeader = cast[ptr Ether_header](packet.cstring)
|
let etherHeader = cast[ptr Ether_header](input.cstring)
|
||||||
if ntohs(etherHeader.ether_type) == ETHERTYPE_IP:
|
if ntohs(etherHeader.ether_type) == ETHERTYPE_IP:
|
||||||
let ipHeader = cast[ptr Ip](cast[int](packet.cstring) + sizeof(Ether_header))
|
let ipHeader = cast[ptr Ip](cast[int](input.cstring) + sizeof(Ether_header))
|
||||||
let ipSrc = $inet_ntoa(ipHeader.ip_src)
|
let ipSrc = $inet_ntoa(ipHeader.ip_src)
|
||||||
let ipDst = $inet_ntoa(ipHeader.ip_dst)
|
let ipDst = $inet_ntoa(ipHeader.ip_dst)
|
||||||
if ipHeader.ip_p.int == IPPROTO_TCP:
|
if ipHeader.ip_p.int == IPPROTO_TCP:
|
||||||
let tcpHeader = cast[ptr Tcphdr](cast[int](ipHeader) + ipHeader.ip_hl.int * 4)
|
let tcpHeader = cast[ptr Tcphdr](cast[int](ipHeader) + ipHeader.ip_hl.int * 4)
|
||||||
result = PacketInfo(protocol: tcp,
|
result = IpPacket(protocol: tcp,
|
||||||
tcpIpSrc: ipSrc,
|
tcpIpSrc: ipSrc,
|
||||||
tcpIpDst: ipDst,
|
tcpIpDst: ipDst,
|
||||||
tcpPortSrc: Port(ntohs(tcpHeader.th_sport)),
|
tcpPortSrc: Port(ntohs(tcpHeader.th_sport)),
|
||||||
tcpPortDst: Port(ntohs(tcpHeader.th_dport)),
|
tcpPortDst: Port(ntohs(tcpHeader.th_dport)),
|
||||||
tcpSeqNumber: ntohl(tcpHeader.th_seq))
|
tcpSeqNumber: ntohl(tcpHeader.th_seq))
|
||||||
else:
|
else:
|
||||||
result = PacketInfo(protocol: other)
|
result = IpPacket(protocol: other)
|
||||||
else:
|
else:
|
||||||
result = PacketInfo(protocol: other)
|
result = IpPacket(protocol: other)
|
||||||
|
|
|
@ -30,7 +30,7 @@ var
|
||||||
PACKET_ADD_MEMBERSHIP {.importc: "PACKET_ADD_MEMBERSHIP", header: "<linux/if_packet.h>".}: cushort
|
PACKET_ADD_MEMBERSHIP {.importc: "PACKET_ADD_MEMBERSHIP", header: "<linux/if_packet.h>".}: cushort
|
||||||
PACKET_MR_PROMISC {.importc: "PACKET_MR_PROMISC", header: "<linux/if_packet.h>".}: cushort
|
PACKET_MR_PROMISC {.importc: "PACKET_MR_PROMISC", header: "<linux/if_packet.h>".}: cushort
|
||||||
|
|
||||||
proc setupRawSocket*(iface: NetworkInterface): AsyncFD =
|
proc setupEthernetCapturingSocket*(iface: NetworkInterface): AsyncFD =
|
||||||
# Create a raw packet socket. For accessing outgoing packets we need to use
|
# Create a raw packet socket. For accessing outgoing packets we need to use
|
||||||
# ETH_P_ALL which is needed in network byte order, see packet(7) man page.
|
# ETH_P_ALL which is needed in network byte order, see packet(7) man page.
|
||||||
result = createAsyncNativeSocket(AF_PACKET.cint,
|
result = createAsyncNativeSocket(AF_PACKET.cint,
|
||||||
|
|
16
tcp_syni.nim
16
tcp_syni.nim
|
@ -66,14 +66,14 @@ proc captureSeqNumbers(puncher: TcpSyniPuncher, rawFd: AsyncFD,
|
||||||
if packet == "":
|
if packet == "":
|
||||||
break
|
break
|
||||||
echo "packet len: ", packet.len
|
echo "packet len: ", packet.len
|
||||||
let packetInfo = fromString(packet)
|
let parsed = parseEthernetPacket(packet)
|
||||||
if packetInfo.protocol == tcp and
|
if parsed.protocol == tcp and
|
||||||
packetInfo.tcpIpSrc == $puncher.srcIp and
|
parsed.tcpIpSrc == $puncher.srcIp and
|
||||||
packetInfo.tcpPortSrc.int == puncher.srcPort.int and
|
parsed.tcpPortSrc.int == puncher.srcPort.int and
|
||||||
packetInfo.tcpIpDst == $puncher.dstIp:
|
parsed.tcpIpDst == $puncher.dstIp:
|
||||||
for i, port in puncher.dstPorts.pairs:
|
for i, port in puncher.dstPorts.pairs:
|
||||||
if packetInfo.tcpPortDst.int == port.int:
|
if parsed.tcpPortDst.int == port.int:
|
||||||
seqNums.add(packetInfo.tcpSeqNumber)
|
seqNums.add(parsed.tcpSeqNumber)
|
||||||
break
|
break
|
||||||
await cb(seqNums)
|
await cb(seqNums)
|
||||||
|
|
||||||
|
@ -111,7 +111,7 @@ proc connect*(puncher: TcpSyniPuncher,
|
||||||
progressCb: PunchProgressCb): Future[AsyncSocket] =
|
progressCb: PunchProgressCb): Future[AsyncSocket] =
|
||||||
result = newFuture[AsyncSocket]("tcp_syni.connect")
|
result = newFuture[AsyncSocket]("tcp_syni.connect")
|
||||||
let iface = fromIpAddress($puncher.srcIp)
|
let iface = fromIpAddress($puncher.srcIp)
|
||||||
let rawFd = setupRawSocket(iface)
|
let rawFd = setupEthernetCapturingSocket(iface)
|
||||||
asyncCheck puncher.captureSeqNumbers(rawFd, progressCb)
|
asyncCheck puncher.captureSeqNumbers(rawFd, progressCb)
|
||||||
for dstPort in puncher.dstPorts:
|
for dstPort in puncher.dstPorts:
|
||||||
asyncCheck doConnect(puncher.srcIp, puncher.srcPort, puncher.dstIp,
|
asyncCheck doConnect(puncher.srcIp, puncher.srcPort, puncher.dstIp,
|
||||||
|
|
Loading…
Reference in New Issue