-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathicmp-encrypted-chat.py
92 lines (60 loc) · 2.38 KB
/
icmp-encrypted-chat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'''
Created on 2012-12-14
@author: Patrick Mathieu (@PathetiQ)
@requires: https://www.dlitz.net/software/pycrypto/
@requires: http://www.secdev.org/projects/scapy/
'''
from scapy.all import sr,sr1,IP,ICMP,sniff
from Crypto.Cipher import AES
from Crypto import Random
import argparse
import threading
#blogal var to check if the last message has already been seen
lastMsg=""
#received a packet payload, decrypt and print it if not already seen
def decrypt(val,password):
unpad = lambda s : s[0:-ord(s[-1])]
global lastMsg
enc = str(val[0][ICMP].load).decode("hex")
iv = enc[:16]
decryptor = AES.new(password,AES.MODE_CFB,iv)
decrypted = str(unpad(decryptor.decrypt( enc[16:] )))
#icmp can cause duplicate message
if lastMsg != decrypted:
lastMsg = decrypted
print decrypted
#received data from user input, encrypt and return
def encrypt(data,password):
BS = 32
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
iv = Random.new().read( AES.block_size )
cipher = AES.new(password,AES.MODE_CFB,iv)
#data = "%s : %s" % str(nickname),str(data)
raw = pad(data)
crypted = str(iv + cipher.encrypt(raw)).encode("hex") #must be a multiple of 16 in lenght
return crypted
def sniffing(ether,password,ip):
sniff(iface=ether, filter="icmp and host "+str(ip), prn=lambda x: decrypt(x,password))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-ip', required=True, help='Enter IP to send the encrypted message')
parser.add_argument('-p', required=True, help='Enter the password to encrypt message')
parser.add_argument('-e', required=True, help='Enter network interface (EX: eth0)')
parser.add_argument('-n', required=True, help='Enter your nickname')
args = parser.parse_args()
ip = args.ip
ether = args.e
password = args.p
nickname = args.n
sniffThread = threading.Thread(target=sniffing, args=(ether,password,ip))
sniffThread.start()
#Quitting is pretty badly handle, need interupt(ctrl+c management in here)
while(1):
txt = raw_input()
if txt == "quit()":
break
txt1 = "%s: %s" % (nickname, txt)
data = encrypt(txt1,password)
a = sr1(IP(dst=ip)/ICMP()/data)
#sniffThread.join()
quit()