This repository was archived by the owner on Mar 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwep_attack.py
More file actions
77 lines (64 loc) · 2.16 KB
/
wep_attack.py
File metadata and controls
77 lines (64 loc) · 2.16 KB
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
#!/usr/bin/env python3.6
import sys
import re
import os.path
from collections import Counter
if __name__== "__main__":
print ("--- WEP Attacker ---")
if(len(sys.argv) < 2):
print("Usage: python wep_attack.py file")
sys.exit(1)
if (os.path.isfile(sys.argv[1]) is False):
print(sys.argv[1]+" is not a file or does not exist!")
sys.exit(2)
#Datei öffnen und mit regex ivs füllen
ivs = list()
chitexts = bytearray()
plaintext = bytearray()
with open(sys.argv[1], "r") as file:
#Plaintext aus erster Zeile einlesen
line = file.readline()
plmatch = re.findall('(?<=0x)[0-9a-f]{2}',line)
plaintext.extend(bytearray.fromhex(plmatch[0]))
#IVs und Chitexte einlesen
regex = re.compile('[0-9A-F]{2}')
for line in file:
all = regex.findall(line)
ivs.append(bytearray.fromhex(all[0]+all[1]+all[2])) #werte 0 bis 2 von all in ivs legen
chitexts.extend(bytearray.fromhex(all[3]))
#var initialisierung
n = 256
count = 0
candidatesForK = list()
#ivs sind nun bytearrays in list
for iv in ivs:
i = 0
j = 0
#s generieren
s = [i for i in range(256)]
#Schritte 0 bis 1
for t in range(2):
i = t
j = (j + s[i] + iv[t]) % n
s[i], s[j] = s[j], s[i] #swap
#Schritt 2
i += 1
j = (j + s[i] + iv[2]) % n
#wenn S[0] oder S[1] verändert werden: iv verwerfen
if(j == 0 or j == 1):
continue
s[i], s[j] = s[j], s[i] #swap
#z bestimmen
z = chitexts[count] ^ plaintext[0]
count += 1
#Nach Z(Chitext) in S2 suchen um j3 zu bestimmen
j3 = s.index(z)
#Schritt 3
#Annahme: S[0], S[1] & S[3] werden in Schritt 3 bis 256 nicht mehr verändert
possibleK = (j3 - j - s[3]) % n
candidatesForK.append(possibleK)
#Häufigstes Element in candidatesForK finden
k = Counter(candidatesForK).most_common(1)[0]
print("Probable K: "+hex(k[0]))
print("Found in %d out of %d instances." % (k[1], len(candidatesForK)))
sys.exit(0)