Writing a function that will create a three way handshake and send a string. It works, but i also need to put the reply in a string var and return it as str and also return a boolean of True if it works(Or false if not). I try str(ans.show()) / str(ans.summary()) But i'm getting None as str although when running just ans.show() i can see this output:
0000 IP / TCP 192.168.1.77:11008 > 192.168.1.60:4444 PA / Raw ==> IP / TCP 192.168.1.60:4444 > 192.168.1.77:11008 A / Padding
This is the full code:
import random
from scapy.layers.inet import IP, TCP
from scapy.sendrecv import sr1, sr
def handshake(dst, dport, message):
sport = random.randint(1024, 65535)
# sport = 1027
i = IP(dst=dst)
syn = TCP(sport=sport, dport=dport, flags="S", seq=1000)
print("Starting hand shake")
synack = sr1((i / syn), timeout=1)
ack = TCP(sport=sport, dport=dport, flags="A", seq=syn.seq + 1, ack=synack.seq + 1)
sr((i / ack), timeout=1)
ack = TCP(sport=sport, dport=dport, flags="PA", seq=syn.seq + 1, ack=synack.seq + 1)
ans, unans = sr((i / ack / message), timeout=1)
ans.nsummary()
unans.summary()
replay = str(ans.show())
return replay
print("running main")
somevar = str(handshake("192.168.1.60", 4444, "Hope it works"))
print(type(somevar))
print(somevar)
Result:
running main
Starting hand shake
Begin emission:
Finished sending 1 packets.
...*
Received 4 packets, got 1 answers, remaining 0 packets
IP / TCP 192.168.1.60:4444 > 192.168.1.77:15617 SA / Padding
Begin emission:
Finished sending 1 packets.
.....
Received 5 packets, got 0 answers, remaining 1 packets
Begin emission:
Finished sending 1 packets.
.*
Received 2 packets, got 1 answers, remaining 0 packets
0000 IP / TCP 192.168.1.77:15617 > 192.168.1.60:4444 PA / Raw ==> IP / TCP 192.168.1.60:4444 > 192.168.1.77:15617 A / Padding
0000 IP / TCP 192.168.1.77:15617 > 192.168.1.60:4444 PA / Raw ==> IP / TCP 192.168.1.60:4444 > 192.168.1.77:15617 A / Padding
<class 'str'>
None