Skip to content

Commit ae348f8

Browse files
authored
Merge pull request #1288 from karpierz/fix_jupyter_notebooks
Fixes for jupyter notebooks. [Please add to TODO]
2 parents 8f475a8 + 0cf3314 commit ae348f8

7 files changed

+62
-58
lines changed

doc/notebooks/HTTP_2_Tuto.ipynb

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
"\n",
5555
"s = socket.socket(l[0][0], l[0][1], l[0][2])\n",
5656
"s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)\n",
57-
"s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)\n",
57+
"if hasattr(socket, 'SO_REUSEPORT'):\n",
58+
" s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)\n",
5859
"ip_and_port = l[0][4]"
5960
],
6061
"language": "python",

doc/notebooks/Scapy in 15 minutes.ipynb

+16-16
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
],
8383
"source": [
8484
"ans = sr([IP(dst=\"8.8.8.8\", ttl=(1, 8), options=IPOption_RR())/ICMP(seq=RandShort()), IP(dst=\"8.8.8.8\", ttl=(1, 8), options=IPOption_Traceroute())/ICMP(seq=RandShort()), IP(dst=\"8.8.8.8\", ttl=(1, 8))/ICMP(seq=RandShort())], verbose=False, timeout=3)[0]\n",
85-
"ans.make_table(lambda (x, y): (\", \".join(z.summary() for z in x[IP].options) or '-', x[IP].ttl, y.sprintf(\"%IP.src% %ICMP.type%\")))"
85+
"ans.make_table(lambda x_y: (\", \".join(z.summary() for z in x_y[0][IP].options) or '-', x_y[0][IP].ttl, x_y[1].sprintf(\"%IP.src% %ICMP.type%\")))"
8686
]
8787
},
8888
{
@@ -259,11 +259,11 @@
259259
}
260260
],
261261
"source": [
262-
"print p.dst # first layer that has an src field, here Ether\n",
263-
"print p[IP].src # explicitly access the src field of the IP layer\n",
262+
"print(p.dst) # first layer that has an src field, here Ether\n",
263+
"print(p[IP].src) # explicitly access the src field of the IP layer\n",
264264
"\n",
265265
"# sprintf() is a useful method to display fields\n",
266-
"print p.sprintf(\"%Ether.src% > %Ether.dst%\\n%IP.src% > %IP.dst%\")"
266+
"print(p.sprintf(\"%Ether.src% > %Ether.dst%\\n%IP.src% > %IP.dst%\"))"
267267
]
268268
},
269269
{
@@ -287,7 +287,7 @@
287287
}
288288
],
289289
"source": [
290-
"print p.sprintf(\"%TCP.flags% %TCP.dport%\")"
290+
"print(p.sprintf(\"%TCP.flags% %TCP.dport%\"))"
291291
]
292292
},
293293
{
@@ -444,8 +444,8 @@
444444
],
445445
"source": [
446446
"# Access the first tuple\n",
447-
"print r[0][0].summary() # the packet sent\n",
448-
"print r[0][1].summary() # the answer received\n",
447+
"print(r[0][0].summary()) # the packet sent\n",
448+
"print(r[0][1].summary()) # the answer received\n",
449449
"\n",
450450
"# Access the ICMP layer. Scapy received a time-exceeded error message\n",
451451
"r[0][1][ICMP]"
@@ -658,7 +658,7 @@
658658
],
659659
"source": [
660660
"%matplotlib inline\n",
661-
"ans.multiplot(lambda (x, y): (y[IP].src, (y.time, y[IP].id)), plot_xy=True)"
661+
"ans.multiplot(lambda x_y: (x_y[1][IP].src, (x_y[1].time, x_y[1][IP].id)), plot_xy=True)"
662662
]
663663
},
664664
{
@@ -683,7 +683,7 @@
683683
],
684684
"source": [
685685
"pkt = IP() / UDP() / DNS(qd=DNSQR())\n",
686-
"print repr(raw(pkt))"
686+
"print(repr(raw(pkt)))"
687687
]
688688
},
689689
{
@@ -708,7 +708,7 @@
708708
}
709709
],
710710
"source": [
711-
"print pkt.summary()"
711+
"print(pkt.summary())"
712712
]
713713
},
714714
{
@@ -944,7 +944,7 @@
944944
"source": [
945945
"ans = sr(IP(dst=[\"scanme.nmap.org\", \"nmap.org\"])/TCP(dport=[22, 80, 443, 31337]), timeout=3, verbose=False)[0]\n",
946946
"ans.extend(sr(IP(dst=[\"scanme.nmap.org\", \"nmap.org\"])/UDP(dport=53)/DNS(qd=DNSQR()), timeout=3, verbose=False)[0])\n",
947-
"ans.make_table(lambda (x, y): (x[IP].dst, x.sprintf('%IP.proto%/{TCP:%r,TCP.dport%}{UDP:%r,UDP.dport%}'), y.sprintf('{TCP:%TCP.flags%}{ICMP:%ICMP.type%}')))"
947+
"ans.make_table(lambda x_y: (x_y[0][IP].dst, x_y[0].sprintf('%IP.proto%/{TCP:%r,TCP.dport%}{UDP:%r,UDP.dport%}'), x_y[1].sprintf('{TCP:%TCP.flags%}{ICMP:%ICMP.type%}')))"
948948
]
949949
},
950950
{
@@ -1082,7 +1082,7 @@
10821082
" parser.add_argument(\"ipv6_address\", help=\"An IPv6 address\")\n",
10831083
" args = parser.parse_args()\n",
10841084
"\n",
1085-
" print sr1(IPv6(dst=args.ipv6_address)/ICMPv6EchoRequest(), verbose=0).summary()"
1085+
" print(sr1(IPv6(dst=args.ipv6_address)/ICMPv6EchoRequest(), verbose=0).summary())"
10861086
]
10871087
},
10881088
{
@@ -1244,21 +1244,21 @@
12441244
"\n",
12451245
" @ATMT.state()\n",
12461246
" def SYN(self):\n",
1247-
" print \"-> SYN\"\n",
1247+
" print(\"-> SYN\")\n",
12481248
"\n",
12491249
" @ATMT.state()\n",
12501250
" def SYN_ACK(self):\n",
1251-
" print \"<- SYN/ACK\"\n",
1251+
" print(\"<- SYN/ACK\")\n",
12521252
" raise self.END()\n",
12531253
"\n",
12541254
" @ATMT.state()\n",
12551255
" def RST(self):\n",
1256-
" print \"<- RST\"\n",
1256+
" print(\"<- RST\")\n",
12571257
" raise self.END()\n",
12581258
"\n",
12591259
" @ATMT.state()\n",
12601260
" def ERROR(self):\n",
1261-
" print \"!! ERROR\"\n",
1261+
" print(\"!! ERROR\")\n",
12621262
" raise self.END()\n",
12631263
" @ATMT.state(final=1)\n",
12641264
" def END(self):\n",

doc/notebooks/graphs-ipids.ipynb

+2-2
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@
550550
"collapsed": false,
551551
"input": [
552552
"%matplotlib inline\n",
553-
"ans.multiplot(lambda (p, q): (q[IP].src, (q.time, q[IP].id)), plot_xy=True)"
553+
"ans.multiplot(lambda p_q: (p_q[1][IP].src, (p_q[1].time, p_q[1][IP].id)), plot_xy=True)"
554554
],
555555
"language": "python",
556556
"metadata": {},
@@ -579,7 +579,7 @@
579579
"cell_type": "code",
580580
"collapsed": false,
581581
"input": [
582-
"ans.multiplot(lambda (p, q): (q[IP].src, q[IP].id))"
582+
"ans.multiplot(lambda p_q: (p_q[1][IP].src, p_q[1][IP].id))"
583583
],
584584
"language": "python",
585585
"metadata": {},

doc/notebooks/tls/notebook1_x509.ipynb

+12-12
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"outputs": [],
2424
"source": [
2525
"# Use Shift+Enter to run the current cell\n",
26-
"print 'Hello!'"
26+
"print('Hello!')"
2727
]
2828
},
2929
{
@@ -36,7 +36,7 @@
3636
"source": [
3737
"# You may also use Alt+Enter to run the current cell, then create a new cell right below\n",
3838
"from datetime import datetime\n",
39-
"print 'This is the time right now: %s' % datetime.now()"
39+
"print('This is the time right now: %s' % datetime.now())"
4040
]
4141
},
4242
{
@@ -50,7 +50,7 @@
5050
"# If needed, pause the cell edition with Ctrl-M.\n",
5151
"# Then you can delete the current cell with D+D. You can also undo cell deletion with Z.\n",
5252
"# Finally, should Jupyter become stuck in execution, use Kernel/Interrupt from the menu bar.\n",
53-
"print 'Got it!'"
53+
"print('Got it!')"
5454
]
5555
},
5656
{
@@ -81,7 +81,7 @@
8181
"outputs": [],
8282
"source": [
8383
"keystr = open('raw_data/pki/ca_key.der', 'rb').read()\n",
84-
"print repr(keystr)\n",
84+
"print(repr(keystr))\n",
8585
"# (btw, you can hide the output of a cell by double-clicking on the left of the output)"
8686
]
8787
},
@@ -106,8 +106,8 @@
106106
"outputs": [],
107107
"source": [
108108
"v = privkey.version\n",
109-
"print 'The \\'version\\' stripped from any ASN.1 encoding is 0x%02x.' % v.val\n",
110-
"print 'The \\'version\\' field corresponds to bytes %r.' % raw(v)"
109+
"print('The \\'version\\' stripped from any ASN.1 encoding is 0x%02x.' % v.val)\n",
110+
"print('The \\'version\\' field corresponds to bytes %r.' % raw(v))"
111111
]
112112
},
113113
{
@@ -131,10 +131,10 @@
131131
},
132132
"outputs": [],
133133
"source": [
134-
"print 'Original data: %r...' % keystr[:13]\n",
135-
"print 'New version bytes: %r' % raw(privkey.version)\n",
136-
"print 'New modulus bytes: %r...' % raw(privkey.modulus)[:6]\n",
137-
"print 'Rebuilt data: %r...' % raw(privkey)[:13]"
134+
"print('Original data: %r...' % keystr[:13])\n",
135+
"print('New version bytes: %r' % raw(privkey.version))\n",
136+
"print('New modulus bytes: %r...' % raw(privkey.modulus)[:6])\n",
137+
"print('Rebuilt data: %r...' % raw(privkey)[:13])"
138138
]
139139
},
140140
{
@@ -244,8 +244,8 @@
244244
},
245245
"outputs": [],
246246
"source": [
247-
"print privkey.key\n",
248-
"print privkey.pubkey"
247+
"print(privkey.key)\n",
248+
"print(privkey.pubkey)"
249249
]
250250
},
251251
{

doc/notebooks/tls/notebook2_tls_protected.ipynb

+14-13
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"outputs": [],
2020
"source": [
2121
"# We're going to parse several successive records from the passive listening of a standard TLS handshake\n",
22-
"from scapy.all import *"
22+
"from scapy.all import *\n",
23+
"load_layer('tls')"
2324
]
2425
},
2526
{
@@ -37,7 +38,7 @@
3738
},
3839
"outputs": [],
3940
"source": [
40-
"record1 = TLS(open('raw_data/tls_session_protected/01_cli.raw').read())\n",
41+
"record1 = TLS(open('raw_data/tls_session_protected/01_cli.raw', 'rb').read())\n",
4142
"record1.show()"
4243
]
4344
},
@@ -50,7 +51,7 @@
5051
"outputs": [],
5152
"source": [
5253
"for extension in record1.msg[0].ext:\n",
53-
" print ''\n",
54+
" print('')\n",
5455
" extension.show()"
5556
]
5657
},
@@ -69,7 +70,7 @@
6970
},
7071
"outputs": [],
7172
"source": [
72-
"record2 = TLS(open('raw_data/tls_session_protected/02_srv.raw').read())\n",
73+
"record2 = TLS(open('raw_data/tls_session_protected/02_srv.raw', 'rb').read())\n",
7374
"record2.show()"
7475
]
7576
},
@@ -88,7 +89,7 @@
8889
},
8990
"outputs": [],
9091
"source": [
91-
"record3 = TLS(open('raw_data/tls_session_protected/03_srv.raw').read())\n",
92+
"record3 = TLS(open('raw_data/tls_session_protected/03_srv.raw', 'rb').read())\n",
9293
"record3.show()"
9394
]
9495
},
@@ -102,9 +103,9 @@
102103
"source": [
103104
"# The Certificate message actually contains a *chain* of certificates\n",
104105
"for cert in record3.msg[0].certs:\n",
105-
" print type(cert[1])\n",
106+
" print(type(cert[1]))\n",
106107
" cert[1].show()\n",
107-
" print ''"
108+
" print('')"
108109
]
109110
},
110111
{
@@ -120,7 +121,7 @@
120121
"\n",
121122
"# Indeed the certificate may be used with other domains than its CN 'www.github.com'\n",
122123
"x509c = record3.msg[0].certs[0][1].x509Cert\n",
123-
"print type(x509c)\n",
124+
"print(type(x509c))\n",
124125
"x509c.tbsCertificate.extensions[2].show()"
125126
]
126127
},
@@ -140,7 +141,7 @@
140141
"outputs": [],
141142
"source": [
142143
"# Here the server sent three TLS records in the same TCP segment\n",
143-
"record4 = TLS(open('raw_data/tls_session_protected/04_srv.raw').read())\n",
144+
"record4 = TLS(open('raw_data/tls_session_protected/04_srv.raw', 'rb').read())\n",
144145
"record4.show()"
145146
]
146147
},
@@ -156,7 +157,7 @@
156157
"# First, we need to assemble the whole data being signed\n",
157158
"cli_random = pkcs_i2osp(record1.msg[0].gmt_unix_time, 4) + record1.msg[0].random_bytes\n",
158159
"srv_random = pkcs_i2osp(record2.msg[0].gmt_unix_time, 4) + record2.msg[0].random_bytes\n",
159-
"ecdh_params = str(record4[TLSServerKeyExchange].params)\n",
160+
"ecdh_params = bytes(record4[TLSServerKeyExchange].params)\n",
160161
"\n",
161162
"# Then we retrieve the server's Cert and verify the signature\n",
162163
"cert_srv = record3.msg[0].certs[0][1]\n",
@@ -180,7 +181,7 @@
180181
},
181182
"outputs": [],
182183
"source": [
183-
"record5_str = open('raw_data/tls_session_protected/05_cli.raw').read()\n",
184+
"record5_str = open('raw_data/tls_session_protected/05_cli.raw', 'rb').read()\n",
184185
"record5 = TLS(record5_str)\n",
185186
"record5.show()"
186187
]
@@ -213,7 +214,7 @@
213214
},
214215
"outputs": [],
215216
"source": [
216-
"record6_str = open('raw_data/tls_session_protected/06_srv.raw').read()\n",
217+
"record6_str = open('raw_data/tls_session_protected/06_srv.raw', 'rb').read()\n",
217218
"record6 = TLS(record6_str, tls_session=record5.tls_session.mirror())\n",
218219
"record6.show()"
219220
]
@@ -233,7 +234,7 @@
233234
},
234235
"outputs": [],
235236
"source": [
236-
"record7_str = open('raw_data/tls_session_protected/07_cli.raw').read()\n",
237+
"record7_str = open('raw_data/tls_session_protected/07_cli.raw', 'rb').read()\n",
237238
"record7 = TLS(record7_str, tls_session=record6.tls_session.mirror())\n",
238239
"record7.show()"
239240
]

doc/notebooks/tls/notebook3_tls_compromised.ipynb

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
},
1616
"outputs": [],
1717
"source": [
18-
"from scapy.all import *"
18+
"from scapy.all import *\n",
19+
"load_layer('tls')"
1920
]
2021
},
2122
{
@@ -26,7 +27,7 @@
2627
},
2728
"outputs": [],
2829
"source": [
29-
"record1_str = open('raw_data/tls_session_compromised/01_cli.raw').read()\n",
30+
"record1_str = open('raw_data/tls_session_compromised/01_cli.raw', 'rb').read()\n",
3031
"record1 = TLS(record1_str)\n",
3132
"record1.msg[0].show()"
3233
]
@@ -40,7 +41,7 @@
4041
},
4142
"outputs": [],
4243
"source": [
43-
"record2_str = open('raw_data/tls_session_compromised/02_srv.raw').read()\n",
44+
"record2_str = open('raw_data/tls_session_compromised/02_srv.raw', 'rb').read()\n",
4445
"record2 = TLS(record2_str, tls_session=record1.tls_session.mirror())\n",
4546
"record2.msg[0].show()"
4647
]
@@ -67,7 +68,7 @@
6768
},
6869
"outputs": [],
6970
"source": [
70-
"record3_str = open('raw_data/tls_session_compromised/03_cli.raw').read()\n",
71+
"record3_str = open('raw_data/tls_session_compromised/03_cli.raw', 'rb').read()\n",
7172
"record3 = TLS(record3_str, tls_session=record2.tls_session.mirror())\n",
7273
"record3.show()"
7374
]
@@ -80,7 +81,7 @@
8081
},
8182
"outputs": [],
8283
"source": [
83-
"record4_str = open('raw_data/tls_session_compromised/04_srv.raw').read()\n",
84+
"record4_str = open('raw_data/tls_session_compromised/04_srv.raw', 'rb').read()\n",
8485
"record4 = TLS(record4_str, tls_session=record3.tls_session.mirror())\n",
8586
"record4.show()"
8687
]
@@ -93,7 +94,7 @@
9394
},
9495
"outputs": [],
9596
"source": [
96-
"record5_str = open('raw_data/tls_session_compromised/05_cli.raw').read()\n",
97+
"record5_str = open('raw_data/tls_session_compromised/05_cli.raw', 'rb').read()\n",
9798
"record5 = TLS(record5_str, tls_session=record4.tls_session.mirror())\n",
9899
"record5.show()"
99100
]

0 commit comments

Comments
 (0)