Ticket #26: scapysec.html

File scapysec.html, 36.3 kB (added by frederic.roudaut@free.fr, 2 years ago)
Line 
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
5 <title>Document sans nom</title>
6 <style type="text/css">
7 <!--
8 .Style1 {color: #FF0000}
9 .Style2 {font-family: Arial, Helvetica, sans-serif}
10 .Style3 {
11         color: #000000;
12         font-weight: bold;
13 }
14 .Style4 {font-family: Arial, Helvetica, sans-serif; font-style: italic; }
15 .Style5 {font-family: "Courier New", Courier, monospace}
16 .Style6 {color: #0080FF}
17 .Style7 {font-family: Arial, Helvetica, sans-serif; color: #0080FF; }
18 .Style8 {font-family: Arial, Helvetica, sans-serif; color: #0080FF; font-weight: bold; }
19 -->
20 </style>
21 </head>
22
23 <body>
24 <p></p>
25 <p class="Style2"><span class="Style3">README : Scapysec v1.0 (IPsec
26 for Scapy)</span><br />
27   <br />
28 </p>
29 <p class="Style4"> Author: Frederic ROUDAUT <br />
30   (frederic.roudaut@free.fr)</p>
31 <p class="Style4"> Date : 2006</p>
32 <p class="Style2">&nbsp;</p>
33 <p class="Style8"><a href="#chap1">1 - INTRODUCTION : Scapy & Scapysec</a><br />
34   <a href="#chap2">2 - OVERVIEW</a><br />
35   <a href="#chap3">3 - NEEDS AND INSTALLATION</a><br />
36   <a href="#chap4">4 - THE ESP CLASS</a><br />
37   <a href="#chap5">5 - SECURITY ASSOCIATIONS DATABASE (SAD)</a><br />
38   <a href="#chap5_1">5.1 - PACKET MATCHING WITH SA</a><br />
39   <a href="#chap5_2">5.2 - ENCRYPTION ALGORITHMS</a><br />
40   <a href="#chap5_3">5.3 - AUTHENTICATION ALGORITHM</a><br />
41   <a href="#chap5_4">5.4 - SETTING THE SAD</a><br />
42   <a href="#chap5_4_1">5.4.1 - ADDING A SA</a><br />
43   <a href="#chap5_4_2">5.4.2 - DELETING A SA</a><br />
44   <a href="#chap5_4_3">5.4.3 - CLEARING THE DATABASE</a><br />
45   <a href="#chap6">6 - ENCRYPTION ON THE FLIGHT : IPsec Class</a><br />
46   <a href="#chap7">7 - WHAT'S MORE</a><br />
47   <a href="#chap8">8 - UPDATE</a><br />
48   <a href="#chap9">9 - POSSIBLE EXTENSIONS</a><br />
49   <a href="#chap10">10 - CREDITS AND LICENCE</a><br />
50   <a href="#chap11">11 - BUGS</a></p>
51 <p class="Style8"><a href="#chapA">ANNEX A - IPSEC ALGORITHMS AND KEYS</a><br />
52   <a href="#chapA.1">A.1 - ESP ALGORITHMS</a><br />
53   <a href="#chapA.1.1">A.1.1 - REQUIREMENTS</a><br />
54   <a href="#chapA.1.2">A.1.2 : TripleDES-CBC [RFC2451]</a><br />
55   <a href="#chapA.1.3">A.1.3 : AES-CBC with 128-bit keys [RFC3602]</a><br />
56   <a href="#chapA.1.4">A.1.4 : AES-CTR [RFC3686]</a><br />
57   <a href="#chapA.1.5">A.1.5 : DES-CBC [RFC2405]</a><br />
58   <a href="#chapA.1.6">A.1.6 : BLOWFISH-CBC [RFC2451]</a><br />
59   <a href="#chapA.1.7">A.1.7 : HMAC-MD5-96 [RFC2403]</a><br />
60   <a href="#chapA.1.8">A.1.8 : HMAC-SHA1-96 [RFC2404]</a><br />
61   <a href="#chapA.1.9">A.1.9 : HMAC-SHA256</a></p>
62 <p class="Style7">&nbsp;</p>
63 <p class="Style2">&nbsp;</p>
64 <h2 class="Style2"><span class="Style6">1 - INTRODUCTION : Scapy &amp; Scapysec</span><a name="chap1" id="chap1"></a><br />
65 </h2>
66 <p class="Style2">&quot;Scapy (<a href="http://www.secdev.org/projects/scapy/">http://www.secdev.org/projects/scapy/</a>) is a powerful<br />
67   interactive packet manipulation program. It is able to forge or decode<br />
68   packets of a wide number of protocols, send them on the wire, capture<br />
69   them, match requests and replies, and much more. It can easily handle<br />
70   most classical tasks like scanning, tracerouting, probing, unit tests,<br />
71   attacks or network discovery (it can replace hping, 85% of nmap,<br />
72   arpspoof, arp-sk, arping, tcpdump, tethereal, p0f, etc.). It also<br />
73   performs very well at a lot of other specific tasks that most other<br />
74   tools can't handle, like sending invalid frames, injecting your own<br />
75   802.11 frames, combining technics (VLAN hopping+ARP cache poisoning,<br />
76 VOIP decoding on WEP encrypted channel, ...), etc&quot;</p>
77
78 <p> Scapy6 (<a
79 href="http://namabiiru.hongo.wide.ad.jp/scapy6/">http://namabiiru.hongo.wide.ad.jp/scapy6/</a>)
80 is an extension to handle IPv6 Packets.   
81 </p>
82 <p class="Style2">Scapysec aims is to extend Scapy to handle IPsec packets with IPv4 or IPv6.<br />
83   Ie to encode/decode IPsec packets, generate any valid/invalid IPsec<br />
84   Packet using different encryption and authentication algorithms. <br />
85   On this basis, some more complex applications could be build up. </p>
86 <p class="Style2">Currently, Scapysec supports ESP with :</p>
87 <p class="Style2"><strong>Encryption Algorithm</strong> (Key Lengths in bits) &lt;block lenght in bytes&gt;<br />
88   --------------------<br />
89   <strong>NULL</strong> : (Any)<br />
90   <strong>TripleDES-CBC</strong> [RFC2451] : (192) &lt;8&gt;<br />
91   <strong>AES-CBC</strong> [RFC3602] : (128/192/256) &lt;16&gt;<br />
92   <strong>AES-CTR </strong>[RFC3686] : (160/224/288) The remaining 32 bits will be used as nonce. &lt;16&gt;<br />
93   <strong>DES-CBC</strong> [RFC2405] : (64) &lt;8&gt;<br />
94   <strong>BLOWFISH-CBC</strong> : (128) &lt;8&gt;<br />
95   <strong>CAST-CBC</strong> : (Any) &lt;8&gt;</p>
96 <p class="Style2"><strong>Authentication Algorithm</strong> (Key Lengths in bits)<br />
97   ------------------------<br />
98   <strong>NULL</strong><br />
99   <strong>HMAC-SHA1-96</strong> [RFC2404] : (Any)<br />
100   <strong>HMAC-MD5-96</strong> [RFC2403] : (Any)<br />
101   <strong>HMAC-SHA256</strong> : (Any)</p>
102 <p>&nbsp;</p>
103 <h2 class="Style2"><span class="Style6">2 - OVERVIEW</span><a name="chap2" id="chap2"></a><br />
104 </h2>
105 <p class="Style2">The example will be explained later but shows how it is simple to<br />
106   generate encrypted IPsec packets. </p>
107 <p><span class="Style1">&gt;&gt;&gt;</span> q = IP(dst=&quot;192.168.0.6&quot;)/ESP(spi=5)/TCP()<br />
108   <span class="Style1">&gt;&gt;&gt;</span> q.show2()</p>
109 <p>== IPv4 IPsec SA Used for: &lt;Src: 192.168.0.2&gt; &lt;Dst: 192.168.0.6&gt; &lt;Spi: 5&gt; ==<br />
110   &lt;Src: *&gt; &lt;Dst: 192.168.0.6&gt; &lt;Spi: *&gt;<br />
111   Crypt Algo: AES-CTR<br />
112   Crypt Key: 00001234abc12ffffbc1<br />
113   Auth Algo: HMAC-MD5<br />
114   Auth Key: 5632abc1azefvc</p>
115 <p><span class="Style1">###[ IP ]###</span><br />
116   version= 4L<br />
117   ihl= 5L<br />
118   tos= 0x0<br />
119   len= 80<br />
120   id= 1<br />
121   flags=<br />
122   frag= 0L<br />
123   ttl= 64<br />
124   proto= IPv6-Crypt<br />
125   chksum= 0xf922<br />
126   src= 192.168.0.2<br />
127   dst= 192.168.0.6<br />
128   options= ''<br />
129   <span class="Style1">###[ ESP ]###</span><br />
130   spi= 0x5L<br />
131   seq= 0L<br />
132   data= 0<br />
133   iv= '\xc7\xaem\xba&lt;\x08i\x93'<br />
134   pad= 'XyYkQx8RDl'<br />
135   padlen= 10<br />
136   nh= TCP<br />
137   authentication= '\n\xf8[\x1a\xe0\xaf\xc1\xab4f\x0f\xda'<br />
138   <span class="Style1">###[ TCP ]###</span><br />
139   sport= ftp-data<br />
140   dport= http<br />
141   seq= 0L<br />
142   ack= 0L<br />
143   dataofs= 5L<br />
144   reserved= 0L<br />
145   flags= S<br />
146   window= 8192<br />
147   chksum= 0xe26<br />
148   urgptr= 0<br />
149   options= {}<br />
150   <span class="Style1">&gt;&gt;&gt;</span> q.show2()</p>
151 <p><span class="Style2">In this example we have several things. Indeed the packet matchs what<br />
152   we will call a Security Association:</span><br />
153   &lt;Src: *&gt; &lt;Dst: 192.168.0.6&gt; &lt;Spi: *&gt;<br />
154   <span class="Style2">Then the packet is created. All the fields are filled, the packet is<br />
155   encrypted and the authentication is computed and appended according to<br />
156   the Algorithm specified in the Security Association</span></p>
157 <p class="Style2">When q.show2() is called, the reverse is done. The security<br />
158   Association matching the packet is found and the packet is decrypted<br />
159   before the printing as it has been sent by another host on the<br />
160   network.</p>
161 <p>&nbsp;</p>
162 <h2 class="Style2"><span class="Style6">3 - NEEDS AND INSTALLATION</span><a name="chap3" id="chap3"></a><br />
163 </h2>
164 <p class="Style2">Scapysec uses the Python language 2.3 or upcomming versions. There is<br />
165   no paticular need. You just need to have scapy.py and scapy6.py if you<br />
166   want to be able to generate IPsec packets with IPv6.<br />
167   You also need pycrypto since all the encryption/decryption is done<br />
168   using this library.</p>
169 <p><span class="Style2">just do : </span>python ./scapysec.py</p>
170 <p class="Style2">Oups, you also need a few adaptions to Scapy. <br />
171   Normaly it should be included with this file. If not you may
172   download it to  <a href="http://roudaut.frederic.free.fr/data/scapysec/scapy.py"> http://roudaut.frederic.free.fr/data/scapysec/scapy.py</a>.
173  </p>
174 <p class="Style2">
175   Scapy6 may be downloaded to <a href="http://namabiiru.hongo.wide.ad.jp/scapy6/"> http://namabiiru.hongo.wide.ad.jp/scapy6/  </a>.
176  </p>
177
178  </p>
179  
180 <p>&nbsp;</p>
181 <h2 class="Style2"><span class="Style6">4 - The ESP CLASS</span><a name="chap4" id="chap4"></a><br />
182 </h2>
183 <p class="Style2">The different fields of the ESP layer may be seen using ls()<br />
184   If you do not know about Scapy, this command gives the field name,<br />
185   the type, the value and the default value for this field</p>
186 <p><span class="Style1">&gt;&gt;&gt;</span> ls(ESP())<br />
187   spi        : XIntField            = 0               (0)<br />
188   seq        : IntField             = 0               (0)<br />
189   data       : StrField             = None            (None)<br />
190   iv         : StrField             = None            (None)<br />
191   pad        : StrField             = None            (None)<br />
192   padlen     : ByteField            = None            (None)<br />
193   nh         : ByteEnumField        = None            (None)<br />
194   authentication : StrField             = None            (None)<br />
195 </p>
196 <p>The fields are the following : <br />
197   - spi : Security Parameters Index<br />
198   - seq : Sequence Number<br />
199   - data : data field is used to give directly encrypted data for iv + pad + padlen + nh<br />
200   - iv : Initialization Vector. Computed if not set. The size is<br />
201   precised by the Encryption Algorithm<br />
202   - pad : Filled if not set and depending from the Encryption Algorithm<br />
203   - padlen : Pad length. Computed if not set<br />
204   - nh : Next Header. Set if not set<br />
205   - authentication : Computed if available<br />
206 </p>
207 <p class="Style2">pad, padlen and iv lengths are depending from the Encryption<br />
208   Algorithms. As you may create packets that will fail with Encryption,<br />
209   they will be corrected if inadequate.</p>
210 <p class="Style2">The data field will be used to directly give encrypted data<br />
211   for iv + pad + padlen + nh</p>
212 <p class="Style2">As you may see, you now have to find a way to indicate:<br />
213   - the Encryption Algorithm<br />
214   - the Encryption Key<br />
215   - the Authentication Algorithm<br />
216   - the Authentication Key</p>
217 <p class="Style2">These elements will be indicated in the Security Associations Database</p>
218 <p class="Style2">&nbsp;</p>
219 <h2 class="Style2"><span class="Style6">5 - SECURITY ASSOCIATIONS DATABASE (SAD)</span><a name="chap5" id="chap5"></a><br />
220 </h2>
221 <p class="Style2">Here is an example of the SAD<br />
222 </p>
223 <p>ipsec_sad_example = {'IPV4' : [['192.168.0.2','192.168.0.1','5','ESP','BLOWFISH-CBC','azdregrnytnytftg','HMAC-SHA1','ecuheznbevnbevabgj'],\<br />
224   ['192.168.*.2','192.168.0.5','5','ESP','DES-CBC','5632abc1','HMAC-SHA1','5632abc1azefvc'],\<br />
225   ['192.168.0.2','192.168.0.3','0x0**0**05','ESP','AES-CBC','1234abc12ffffbc1','HMAC-SHA1','5632abc1azefvc'],\<br />
226   ['192.168.0.2','192.168.0.7','*','ESP','AES-CBC','1234abc12ffffbc1','HMAC-SHA256','5llll632abc1azefvc'],\<br />
227   ['*','192.168.0.6','*','ESP','AES-CTR','1234abc12ffffbc1','HMAC-MD5','5632abc1azefvc'],\<br />
228   ['192.168.*.2','192.168.0.8','*','ESP','DES-CBC','5632abcuyfyuf1','HMAC-SHA1','5632abc1azefvc'],\<br />
229   ['192.168.*.2','192.168.0.12','*','ESP','DES-CBC','5632abcuyfyuf1','HMAC-MD5','5632abc1azefvc'],\<br />
230   ['192.168.0.2','192.*.0.4','*','ESP','NULL','','NULL','']],\<br />
231   'IPV6' : [['*','fe80::240:96ff:fea7:c5d1','5','ESP','DES-CBC','5632abc1','HMAC-SHA1','5632abc1azefvc'],\<br />
232   ['*','3ffe::3','0x0**0**05','ESP','AES-CBC','1234abc12ffffbc1','HMAC-SHA1','5632abc1azefvc'],\<br />
233   ['','3f*e::4','*','ESP','AES-CBC','1234abc12ffffbc1','HMAC-SHA256','5llll632abc1azefvc']]}<br />
234 </p>
235 <p class="Style2">The SAD is a dictionnary {'IPV4':[],'IPV6':[]}<br />
236   One entry is for IPv4, the other one is for IPv6. <br />
237   Each one contains a list of Security Associations (SAs).<br />
238   The SA described hereafter gives:<br />
239   - the source address filter: '192.168.0.2'<br />
240   - the destination address filter: '192.168.0.1'<br />
241   - the SPI filter: '5'<br />
242   - the protocol: 'AH' or 'ESP' : <br />
243   - the Encryption Algorithm : 'BLOWFISH-CBC'<br />
244   - the Encryption Key : 'azdregrnytnytftg'<br />
245   - the Authentication Algorithm : 'HMAC-SHA1'<br />
246   - the Authentication Key : 'ecuheznbevnbevabgj'</p>
247 <p>['192.168.0.2','192.168.0.1','5','ESP','BLOWFISH-CBC','azdregrnytnytftg','HMAC-SHA1','ecuheznbevnbevabgj']<br />
248 </p>
249 <p class="Style2">When an IPsec Packet is sent or received, the corresponding SAD entry<br />
250   (IPv4 or IPv6) is checked. If the packet is catched by one security<br />
251   association, it will be encrypted or decrypted using the associated<br />
252   algorithms and keys. </p>
253 <p>&nbsp;</p>
254 <h2 class="Style2"><span class="Style6">5.1 - PACKET MATCHING WITH SA</span><a name="chap5_1" id="chap5_1"></a><br />
255 </h2>
256 <p class="Style2">An IPsec packet is catched by a security association if its source,<br />
257   destination address and SPI match a corresponding entry in the SAD.<br />
258   The matching uses the wildcard '*'.</p>
259 <p><span class="Style2">Address are string. It may be IPv4 (</span>'192.168.0.2'<span class="Style2">) or any IPv6 address</span><br />
260   ('3FFE::1'<span class="Style2"> or </span>'fe80::240:96ff:fea7:c5d0'<span class="Style2">)</span><br />
261   <span class="Style2">You may use</span> '*'<span class="Style2"> to indicate any address or</span> '*' <span class="Style2">in the address string.<br />
262   For IPv4, we then may have </span>'192.168.*.2'<span class="Style2">. Each</span> '*' <span class="Style2">means 1 byte. <br />
263   For IPv6, each</span> '*' <span class="Style2">means 4 bits. (</span>'3FF*::1', 'fe80::240:96ff:fea7:c5d0'<span class="Style2">)</span></p>
264 <p><span class="Style2">The SPI is a string but it may represent an integer value (</span>'8') <span class="Style2">or an<br />
265   hexadecimal value (</span>'0x23'<span class="Style2">).</span> <br />
266   <span class="Style2">You also may use</span> '*'<span class="Style2"> to indicate any SPI or even</span> '*' <span class="Style2">in the<br />
267   hexadecimal SPI string as</span> '0x34*6'.<span class="Style2"> In this case we do not mind about<br />
268   the hexadecimal value between the</span> '4' <span class="Style2">and the </span>'6' </p>
269 <p>&nbsp;</p>
270 <h2 class="Style2"><span class="Style6">5.2 - ENCRYPTION ALGORITHMS</span><a name="chap5_2" id="chap5_2"></a><br />
271 </h2>
272 <p class="Style2">The Encryption Algorithms available are the following:<br />
273   Between () we give the key length available in bits.<br />
274   Between &lt;&gt; we indicate the block size in bytes</p>
275 <p>NULL               : (Any)<br />
276   3DES-CBC [RFC2451] : (192) &lt;8&gt;<br />
277   AES-CBC            : (128/192/256) &lt;16&gt;<br />
278   AES-CTR            : (160/224/288) The remaining 32 bits will be used as nonce. &lt;16&gt;<br />
279   DES-CBC [RFC2405]  : (64) &lt;8&gt;<br />
280   BLOWFISH-CBC       : (128) &lt;8&gt;<br />
281   CAST-CBC           : (Any) &lt;8&gt;<br />
282 </p>
283 <p class="Style2">A key with an incorrect length will not be used. Instead it may be<br />
284   truncated or enhanced.</p>
285 <p class="Style2">&nbsp;</p>
286 <h2 class="Style2"><span class="Style6">5.3 - AUTHENTICATION ALGORITHM</span><a name="chap5_3" id="chap5_3"></a><br />
287 </h2>
288 <p class="Style2">The Authentication Algorithms available are the following:<br />
289   Between () we give the key lenght available.</p>
290 <p>NULL                   : (Any)<br />
291   HMAC-SHA1-96 [RFC2404] : (Any)<br />
292   HMAC-MD5-96 [RFC2403]  : (Any)<br />
293   HMAC-SHA256            : (Any)<br />
294 </p>
295 <p class="Style2">A key with an incorrect length will not be used. Instead it may be<br />
296   truncated or enhanced.</p>
297 <p>&nbsp;</p>
298 <h2 class="Style2"><span class="Style6">5.4 - SETTING THE SAD</span><a name="chap5_4" id="chap5_4"></a><br />
299 </h2>
300 <p><span class="Style2">The conf Instance has been enhanced with a setkey variable that uses<br />
301   the</span> _IPSEC_SAD <span class="Style2">Python Class.</span></p>
302 <p class="Style2">Then if you do conf.setkey you get the current SAD</p>
303 <p><span class="Style1">&gt;&gt;&gt;</span> conf.setkey<br />
304   == IPv4 Security Associations ==<br />
305   &lt;Src : 192.168.0.2&gt;   &lt;Dst : 192.168.0.1&gt;   &lt;SPI : 5&gt;<br />
306   Mode ESP<br />
307   Crypt Algo : BLOWFISH-CBC   Crypt Key : azdregrnytnytftg<br />
308   Auth Algo  : HMAC-SHA1   Auth Key : ecuheznbevnbevabgj</p>
309 <p>&lt;Src : 192.168.*.2&gt;   &lt;Dst : 192.168.0.5&gt;   &lt;SPI : 5&gt;<br />
310   Mode ESP<br />
311   Crypt Algo : DES-CBC   Crypt Key : 5632abc1<br />
312   Auth Algo  : HMAC-SHA1   Auth Key : 5632abc1azefvc</p>
313 <p>&lt;Src : 192.168.0.2&gt;   &lt;Dst : 192.168.0.3&gt;   &lt;SPI : 0x0**0**05&gt;<br />
314   Mode ESP<br />
315   Crypt Algo : AES-CBC   Crypt Key : 1234abc12ffffbc1<br />
316   Auth Algo  : HMAC-SHA1   Auth Key : 5632abc1azefvc</p>
317 <p>&lt;Src : 192.168.0.2&gt;   &lt;Dst : 192.168.0.7&gt;   &lt;SPI : *&gt;<br />
318   Mode ESP<br />
319   Crypt Algo : AES-CBC   Crypt Key : 1234abc12ffffbc1<br />
320   Auth Algo  : HMAC-SHA256   Auth Key : 5llll632abc1azefvc</p>
321 <p>&lt;Src : *&gt;   &lt;Dst : 192.168.0.6&gt;   &lt;SPI : *&gt;<br />
322   Mode ESP<br />
323   Crypt Algo : AES-CTR   Crypt Key : 1234abc12ffffbc1<br />
324   Auth Algo  : HMAC-MD5   Auth Key : 5632abc1azefvc</p>
325 <p>&lt;Src : 192.168.*.2&gt;   &lt;Dst : 192.168.0.8&gt;   &lt;SPI : *&gt;<br />
326   Mode ESP<br />
327   Crypt Algo : DES-CBC   Crypt Key : 5632abcuyfyuf1<br />
328   Auth Algo  : HMAC-SHA1   Auth Key : 5632abc1azefvc</p>
329 <p>&lt;Src : 192.168.*.2&gt;   &lt;Dst : 192.168.0.12&gt;   &lt;SPI : *&gt;<br />
330   Mode ESP<br />
331   Crypt Algo : DES-CBC   Crypt Key : 5632abcuyfyuf1<br />
332   Auth Algo  : HMAC-MD5   Auth Key : 5632abc1azefvc<br />
333 </p>
334 <p><span class="Style2">Then you may create a static configuration by doing : </span><br />
335   conf.setkey = _IPSEC_SAD(ipsec_sad_example) # Security Association Database for IPsec</p>
336 <p>&nbsp;</p>
337 <h2 class="Style2"><span class="Style6">5.4.1 - ADDING A SA</span><a name="chap5_4_1" id="chap5_4_1"></a><br />
338 </h2>
339 <p class="Style2">The simplest way to do this is to use the add command.</p>
340 <p>def add(self,proto,src,dst,spi,mode,algo_crypt,algo_key_crypt,algo_auth,algo_key_auth)</p>
341 <p class="Style2">All Parameters types are strings:</p>
342 <p>proto : <span class="Style2">either </span>'IPV4' <span class="Style2">or</span> 'IPV6'<br />
343   src : <span class="Style2">source address filter </span><br />
344   dst : <span class="Style2">destination address filter</span><br />
345   spi :<span class="Style2"> spi filter</span><br />
346   mode : <span class="Style2">either</span> 'ESP' <span class="Style2">or</span> 'AH'<br />
347   algo_crypt : <span class="Style2">Ecncryption Algorithm</span><br />
348   algo_key_crypt :  <span class="Style2">Encryption Key</span><br />
349   algo_auth :  <span class="Style2">Authentication Algorithm</span><br />
350   algo_key_auth : <span class="Style2">Authentication</span><br />
351 </p>
352 <p><span class="Style1">&gt;&gt;&gt;</span> conf.setkey.add('IPV4','*','192.168.*.42','6','ESP','AES-CBC', '1234abc12ffffbc1', 'HMAC-SHA256' ,'5llll632abc1azefvc')</p>
353 <p>&nbsp;</p>
354 <h2 class="Style2"><span class="Style6">5.4.2 - DELETING A SA</span><a name="chap5_4_2" id="chap5_4_2"></a><br />
355 </h2>
356 <p class="Style2">The simplest way to do this is to use the delete command.</p>
357 <p>def delete(self,proto,src,dst,spi)</p>
358 <p class="Style2">All Parameters types are strings:</p>
359 <p>proto : <span class="Style2">either </span>'IPV4' <span class="Style2">or</span> 'IPV6'<br />
360 src : <span class="Style2">source address filter </span><br />
361 dst : <span class="Style2">destination address filter</span><br />
362 spi :<span class="Style2"> spi filter</span></p>
363 <p><span class="Style2">if proto, src, dst and spi are </span>'*'<span class="Style2">, the SAD is clear. </span><br />
364   <span class="Style2">If proto is not</span> '*' <span class="Style2">only the corresponding one is clear. <br />
365   Otherwise all the entries that exactly match the parameters are<br />
366   removed. The character</span> '*' <span class="Style2">used as parameter in the function means<br />
367   that we do not care about this field. </span></p>
368 <p><span class="Style2">Thus the following example remove all entries in the IPv4 SAD that<br />
369   have</span> '192.168.*.42' <span class="Style2">as destination.</span></p>
370 <p><span class="Style1">&gt;&gt;&gt;</span> conf.setkey.delete('IPV4','*','192.168.*.42','*')</p>
371 <p>&nbsp;</p>
372 <h2 class="Style2"><span class="Style6">5.4.3 - CLEARING THE DATABASE</span><a name="chap5_4_3" id="chap5_4_3"></a><br />
373 </h2>
374 <p class="Style2">The following command will clear the database associated to IPv4 or<br />
375   IPv6</p>
376 <p class="Style2">def erase(self,proto)</p>
377 <p><span class="Style2">where proto is either</span> 'IPV4' <span class="Style2">or</span> 'IPV6'<br />
378 </p>
379 <p>&nbsp;</p>
380 <h2 class="Style2"><span class="Style6">6 - ENCRYPTION ON THE FLIGHT : IPsec Class</span><a name="chap6" id="chap6"></a><br />
381 </h2>
382 <p class="Style2">Sometimes it is not convenient to set the database prior to send<br />
383   packets. In this case you may use the IPsec class</p>
384 <p><span class="Style1">&gt;&gt;&gt;</span> ls(IPsec())<br />
385   crypt      : StrField             = 'NULL'          ('NULL')<br />
386   crypt_key  : StrField             = ''              ('')<br />
387   auth       : StrField             = 'NULL'          ('NULL')<br />
388   auth_key   : StrField             = ''              ('')<br />
389 </p>
390 <p><span class="Style1">&gt;&gt;&gt;</span> q = IP(dst=&quot;192.168.0.6&quot;)/IPsec(crypt='AES-CTR', crypt_key='00001234abc12ffffbc1', auth='HMAC-MD5', auth_key ='5632abc1azefvc')/<br />
391   ESP(spi=5)/TCP()<br />
392   <span class="Style1">&gt;&gt;&gt;</span> q.show()</p>
393 <p><span class="Style1">###[ IP ]###</span><br />
394   version= 4<br />
395   ihl= 0<br />
396   tos= 0x0<br />
397   len= 0<br />
398   id= 1<br />
399   flags=<br />
400   frag= 0<br />
401   ttl= 64<br />
402   proto= IPv6-Crypt<br />
403   chksum= 0x0<br />
404   src= 192.168.0.2<br />
405   dst= 192.168.0.6<br />
406   options= ''<br />
407   <span class="Style1">###[ IPsec ]###</span><br />
408   crypt= 'AES-CTR'<br />
409   crypt_key= '00001234abc12ffffbc1'<br />
410   auth= 'HMAC-MD5'<br />
411   auth_key= '5632abc1azefvc'<br />
412   <span class="Style1">###[ ESP ]###</span><br />
413   spi= 0x5<br />
414   seq= 0<br />
415   data= 0<br />
416   iv= 0<br />
417   pad= 0<br />
418   padlen= 0<br />
419   nh= TCP<br />
420   authentication= 0<br />
421   <span class="Style1">###[ TCP ]###</span><br />
422   sport= ftp-data<br />
423   dport= http<br />
424   seq= 0<br />
425   ack= 0<br />
426   dataofs= 0<br />
427   reserved= 0<br />
428   flags= S<br />
429   window= 8192<br />
430   chksum= 0x0<br />
431   urgptr= 0<br />
432   options= {}<br />
433 </p>
434 <p><span class="Style2">In this case we have no SAD for the packet then we cannot decrypt it. <br />
435   Thus we have to print it using</span> show()<span class="Style2">. If we use</span> show2()<span class="Style2">, the<br />
436   decryption will be tried and will certainly be uncorrect. <br />
437   </span></p>
438 <h2 class="Style2"><span class="Style6">7 - WHAT'S MORE </span><a name="chap7" id="chap7"></a><br />
439 </h2>
440 <p class="Style2">All the previous examples were using transport mode and you have<br />
441   certainly notice that there is no such indication in the SAD. <br />
442   In fact you have to do it by yourself ;-) :</p>
443 <p>q = IP(dst=&quot;192.168.0.6&quot;)/IPsec(crypt='AES-CTR', crypt_key='00001234abc11', auth='HMAC-MD5', auth_key ='5632abc1azefvc')/ESP(spi=5)/IP(dst=&quot;10.0.0.1&quot;)/TCP()<br />
444 <span class="Style1">&gt;&gt;&gt;</span> q.show() </p>
445 <p><span class="Style1">###[ IP ]###</span><br />
446   version= 4<br />
447   ihl= 0<br />
448   tos= 0x0<br />
449   len= 0<br />
450   id= 1<br />
451   flags=<br />
452   frag= 0<br />
453   ttl= 64<br />
454   proto= IPv6-Crypt<br />
455   chksum= 0x0<br />
456   src= 192.168.0.2<br />
457   dst= 192.168.0.6<br />
458   options= ''<br />
459   <span class="Style1">###[ IPsec ]###</span><br />
460   crypt= 'AES-CTR'<br />
461   crypt_key= '00001234abc11'<br />
462   auth= 'HMAC-MD5'<br />
463   auth_key= '5632abc1azefvc'<br />
464   <span class="Style1">###[ ESP ]###</span><br />
465   spi= 0x5<br />
466   seq= 0<br />
467   data= 0<br />
468   iv= 0<br />
469   pad= 0<br />
470   padlen= 0<br />
471   nh= IP-ENCAP<br />
472   authentication= 0<br />
473   <span class="Style1">###[ IP ]###</span><br />
474   version= 4<br />
475   ihl= 0<br />
476   tos= 0x0<br />
477   len= 0<br />
478   id= 1<br />
479   flags=<br />
480   frag= 0<br />
481   ttl= 64<br />
482   proto= TCP<br />
483   chksum= 0x0<br />
484   src= 192.168.0.2<br />
485   dst= 10.0.0.1<br />
486   options= ''<br />
487   <span class="Style1">###[ TCP ]###</span><br />
488   sport= ftp-data<br />
489   dport= http<br />
490   seq= 0<br />
491   ack= 0<br />
492   dataofs= 0<br />
493   reserved= 0<br />
494   flags= S<br />
495   window= 8192<br />
496   chksum= 0x0<br />
497   urgptr= 0<br />
498   options= {}<br />
499 </p>
500 <p class="Style2">And using the same principles you may have a lot of different IPv4,<br />
501   IPv6, ESP encapsulations with some different algorithms.</p>
502 <p class="Style2">Some checks may be done using Wireshark (<a href="http://www.wireshark.org">www.wireshark.org</a>). Have a look to <a href="http://wiki.wireshark.org/ESP_Preferences"> the Wireshark wiki </a> for more information. <br  />
503 I currently found a bug in AES-CTR I will try to solve later since  I am the author of this plugin ;-(.</p>
504 <p class="Style2">The algorithms handled with Wireshark are the following:</p>
505 <p class="Style4">Encryption Algorithms : <br />
506   <br />
507   - NULL<br />
508   - TripleDES-CBC [RFC2451] : keylen 192 bits.<br />
509   - AES-CBC with 128-bit keys [RFC3602] : keylen 128 and 192/256 bits.<br />
510   - AES-CTR [RFC3686] : keylen 160/224/288 bits. The remaining 32 bits will be used as nonce.<br />
511   - DES-CBC [RFC2405] : keylen 64 bits<br />
512   - BLOWFISH-CBC : keylen 128 bits.<br />
513   - TWOFISH-CBC : keylen 128/256 bits.</p>
514 <p class="Style4">Authentication Algorithms:<br />
515   <br />
516   - NULL<br />
517   - HMAC-SHA1-96 [RFC2404] : any keylen<br />
518   - HMAC-MD5-96 [RFC2403] : any keylen<br />
519   - HMAC-SHA256 : any keylen</p>
520 <p>&nbsp;</p>
521 <h2 class="Style2"><span class="Style6">8 - UPDATE</span><a name="chap8" id="chap8"></a><br />
522 </h2>
523 <p class="Style2">&nbsp;</p>
524 <h2 class="Style2"><span class="Style6">9 - POSSIBLE EXTENSIONS</span><a name="chap9" id="chap9"></a><br />
525 </h2>
526 <p class="Style2">For sure, you may use some others ESP Encryption algorithms. I only<br />
527   have done the work for Algorithms specified in RFC4305. <br />
528   Nevertheless, it should not be very difficult to add some other ones. <br />
529   Have a look to the code ;-(</p>
530 <p class="Style2">Moreover, you may also add some other Authentication Algorithms. <br />
531   AES-XCBC-MAC-96 has not been added because i had trouble to get a<br />
532   working implementation.</p>
533 <p class="Style2">For the moment it works only for ESP but why not adding AH.<br />
534   On this basis it could be possible to have a better look to IKE. </p>
535 <p class="Style2">An interesting point with this tool is that if your kernel does not<br />
536   handle IPsec or a particular algorithm it could be quite easy as<br />
537   Wifitap (http://sid.rstack.org/index.php/Wifitap) does to have an<br />
538   interface to generate such encrypted packets