1#!/usr/bin/env python
2
3# sorry, this is very ugly, but I'm in python 2.5
4import sys
5sys.path.insert(0,"../..")
6
7import impacket.dot11
8import impacket.ImpactPacket
9from impacket.Dot11KeyManager import KeyManager
10from impacket.ImpactDecoder import Dot11Decoder
11from binascii import hexlify
12import unittest
13
14class TestDot11WEPData(unittest.TestCase):
15
16    def setUp(self):
17        self.dot11 = impacket.dot11.Dot11(FCS_at_end = False)
18
19        # dot11.fc
20        self.dot11.set_version(0)
21        self.dot11.set_type_n_subtype(impacket.dot11.Dot11Types.DOT11_TYPE_DATA_SUBTYPE_DATA)
22
23        # dot11.fc.flags
24        self.dot11.set_fromDS(0)
25        self.dot11.set_toDS(1)
26        self.dot11.set_moreFrag(0)
27        self.dot11.set_retry(0)
28        self.dot11.set_powerManagement(0)
29        self.dot11.set_moreData(0)
30        self.dot11.set_protectedFrame(1)
31        self.dot11.set_order(0)
32
33        # dot11.Data
34        self.dot11data = impacket.dot11.Dot11DataFrame()
35        self.dot11data.set_duration(44)
36        self.dot11data.set_address1([0x00,0x21,0x29,0x68,0x33,0x5d]) # Bssid
37        self.dot11data.set_address2([0x00,0x18,0xde,0x7c,0x37,0x9f]) # Source
38        self.dot11data.set_address3([0x00,0x21,0x29,0x68,0x33,0x5d]) # Destination
39        self.dot11data.set_fragment_number(0)
40        self.dot11data.set_sequence_number(3439)
41
42        # WEP
43        self.wep = impacket.dot11.Dot11WEP()
44        self.wep.set_iv(0x0c3165)
45        self.wep.set_keyid(0)
46
47        # WEPData
48        self.wepdata = impacket.dot11.Dot11WEPData()
49
50        # LLC
51        self.llc = impacket.dot11.LLC()
52        self.llc.set_DSAP(0xaa)
53        self.llc.set_SSAP(0xaa)
54        self.llc.set_control(0x03)
55
56        # SNAP
57        self.snap = impacket.dot11.SNAP()
58        self.snap.set_OUI(0x000000)
59        self.snap.set_protoID(0x0800)
60
61        # IP
62        self.ip = impacket.ImpactPacket.IP()
63        self.ip.set_ip_v(0x04)
64        self.ip.set_ip_tos(0x00)
65        self.ip.set_ip_id(0xa607)
66        # IP.flags
67        self.ip.set_ip_rf(0)
68        self.ip.set_ip_df(0)
69        self.ip.set_ip_mf(0)
70        #
71        self.ip.set_ip_off(0)
72        self.ip.set_ip_ttl(128)
73        self.ip.set_ip_p(0x01) # ICMP
74        self.ip.set_ip_src('192.168.1.102')
75        self.ip.set_ip_dst('64.233.163.103')
76
77        # ICMP
78        self.icmp = impacket.ImpactPacket.ICMP()
79        self.icmp.set_icmp_type(self.icmp.ICMP_ECHO)
80        self.icmp.set_icmp_code(0x00)
81        self.icmp.set_icmp_id(0x0400)
82        self.icmp.set_icmp_seq(0x8405)
83
84        # Data
85        datastring = 'abcdefghijklmnopqrstuvwabcdefghi'
86        self.data = impacket.ImpactPacket.Data( datastring )
87
88        # Build the protocol stack
89        self.dot11.contains(self.dot11data)
90        self.dot11data.contains(self.wep)
91        self.wep.contains(self.wepdata)
92        self.wepdata.contains(self.llc)
93        self.llc.contains(self.snap)
94        self.snap.contains(self.ip)
95        self.ip.contains(self.icmp)
96        self.icmp.contains(self.data)
97
98        # Instantiated the Key Manager
99        self.km=KeyManager()
100        self.km.add_key([0x00,0x21,0x29,0x68,0x33,0x5b],'999cbb701ca2ef030e302dcc35'.decode('hex_codec'))
101
102    def test_02(self):
103        'Test ICV methods'
104        self.assertEqual(self.wepdata.get_icv(),0x00000000)
105        self.assertEqual(self.wepdata.get_computed_icv(),0xA1F93985)
106        self.wepdata.set_icv(0xA1F93985)
107        self.assertEqual(self.wepdata.get_icv(), self.wepdata.get_computed_icv())
108        self.wepdata.set_icv(0x01020304)
109        self.assertEqual(self.wepdata.get_icv(),0x01020304)
110
111    def test_03(self):
112        'Test WEPData creation from scratch with encryption'
113
114        #print "\nWEP Data Decrypted [%s]"%hexlify(self.wepdata.get_packet())
115        self.wepdata.set_icv(0xA1F93985)
116        wep_enc=self.wep.get_encrypted_data('999cbb701ca2ef030e302dcc35'.decode('hex_codec'))
117        #print "\nWEP Data Encrypted [%s]"%hexlify(wep_enc)
118        self.assertEqual(wep_enc,'8d2381e9251cb5aa83d2c716ba6ee18e7d3a2c71c00f6ab82fbc54c4b014ab03115edeccab2b18ebeb250f75eb6bf57fd65cb9e1b26e50ba4bb48b9f3471da9ecf12cb8f361b0253'.decode('hex_codec'))
119
120        #print "\nDot11 decrypted [%s]"%hexlify(self.dot11.get_packet())
121        self.wep.encrypt_frame('999cbb701ca2ef030e302dcc35'.decode('hex_codec'))
122        #print "\nDot11 encrypted [%s]"%hexlify(self.dot11.get_packet())
123
124suite = unittest.TestLoader().loadTestsFromTestCase(TestDot11WEPData)
125unittest.TextTestRunner(verbosity=1).run(suite)
126
127