2019-04-25 11:02:11 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import hashlib
|
2019-04-21 21:38:23 +02:00
|
|
|
from random import shuffle
|
|
|
|
import struct
|
2019-04-25 11:02:11 +02:00
|
|
|
|
|
|
|
corpus_dirs = [
|
2019-04-28 13:46:23 +02:00
|
|
|
'torrent_info', 'upnp', 'gzip', 'base32decode', 'base32encode',
|
2019-04-25 11:02:11 +02:00
|
|
|
'base64encode', 'bdecode_node', 'convert_from_native', 'convert_to_native',
|
2019-04-28 13:46:23 +02:00
|
|
|
'dht_node', 'escape_path', 'escape_string', 'file_storage_add_file',
|
2019-04-25 11:02:11 +02:00
|
|
|
'http_parser', 'lazy_bdecode', 'parse_int', 'parse_magnet_uri', 'resume_data',
|
2019-04-28 13:46:23 +02:00
|
|
|
'sanitize_path', 'utf8_codepoint', 'utf8_wchar', 'utp',
|
2019-04-21 21:38:23 +02:00
|
|
|
'verify_encoding', 'wchar_utf8', 'peer_conn']
|
2019-04-25 11:02:11 +02:00
|
|
|
|
|
|
|
for p in corpus_dirs:
|
|
|
|
try:
|
|
|
|
os.makedirs(os.path.join('corpus', p))
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
torrent_dir = '../test/test_torrents'
|
|
|
|
for f in os.listdir(torrent_dir):
|
|
|
|
shutil.copy(os.path.join(torrent_dir, f), os.path.join('corpus', 'torrent_info'))
|
|
|
|
|
|
|
|
xml_tests = [
|
|
|
|
'<a blah="b"></a>', '<a b=c></a>', '<a b"c"></a>', '<a b="c></a>',
|
|
|
|
'<![CDATA[<sender>John Smith</sender>]]>', '<![CDATA[<sender>John S',
|
|
|
|
'<!-- comment -->', '<empty></empty>', '<tag',
|
|
|
|
'''<?xml version="1.0" encoding="ISO-8859-1" ?>
|
|
|
|
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"></xs:schema>''',
|
|
|
|
'<selfclosing />']
|
|
|
|
|
|
|
|
for x in xml_tests:
|
|
|
|
name = hashlib.sha1(x).hexdigest()
|
|
|
|
with open(os.path.join('corpus', 'upnp', name), 'w+') as f:
|
|
|
|
f.write(x)
|
|
|
|
|
|
|
|
gzip_dir = '../test'
|
|
|
|
for f in ['zeroes.gz', 'corrupt.gz', 'invalid1.gz']:
|
|
|
|
shutil.copy(os.path.join(gzip_dir, f), os.path.join('corpus', 'gzip'))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# generate peer protocol messages
|
|
|
|
messages = []
|
|
|
|
|
|
|
|
|
|
|
|
def add_length(msg):
|
2019-08-13 08:34:41 +02:00
|
|
|
return struct.pack('>I', len(msg)) + msg
|
|
|
|
|
|
|
|
|
|
|
|
def add_reserved(msg):
|
|
|
|
return '\0\0\0\0\0\x18\0\x05' + msg
|
|
|
|
|
|
|
|
|
|
|
|
# extended handshake
|
|
|
|
def add_extended_handshake(msg):
|
|
|
|
ext_handshake = 'd1:md11:ut_metadatai1e11:lt_donthavei2e12:ut_holepunch' + \
|
|
|
|
'i3e11:upload_onlyi4ee11:upload_onlyi1e10:share_modei1e4:reqqi1234e6:yourip4:0000e'
|
|
|
|
return add_length(struct.pack('BB', 20, 0) + ext_handshake) + msg
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
# request
|
|
|
|
for i in range(101):
|
|
|
|
for j in range(-1, 1):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_length(struct.pack('>Biii', 6, i, j, 0x4000)))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# cancel
|
|
|
|
for i in range(101):
|
|
|
|
for j in range(-1, 1):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_length(struct.pack('>Biii', 8, i, j, 0x4000)))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# piece
|
|
|
|
for i in range(101):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_length(struct.pack('>Bii', 7, i, 0) + ('a' * 0x4000)))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# single-byte
|
|
|
|
for i in range(256):
|
|
|
|
messages.append(add_length(struct.pack('B', i)))
|
|
|
|
|
|
|
|
# reject
|
|
|
|
for i in range(101):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_length(struct.pack('>Biii', 16, i, 0, 0x4000)))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# suggest
|
|
|
|
for i in range(101):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_length(struct.pack('>Bi', 13, i)))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# allow-fast
|
|
|
|
for i in range(101):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_length(struct.pack('>Bi', 17, i)))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# have
|
|
|
|
for i in range(101):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_length(struct.pack('>Bi', 4, i)))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# DHT-port
|
|
|
|
for i in range(101):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_length(struct.pack('>BH', 9, i * 10)))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# hash request
|
|
|
|
for i in range(-10, 200, 20):
|
|
|
|
for j in range(-1, 1):
|
|
|
|
for k in range(-1, 1):
|
|
|
|
for l in range(-1, 1):
|
|
|
|
for m in range(-1, 1):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_length(struct.pack('>Biiiii', 21, i, j, k, l, m)))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# hash reject
|
|
|
|
for i in range(-10, 200, 20):
|
|
|
|
for j in range(-1, 1):
|
|
|
|
for k in range(-1, 1):
|
|
|
|
for l in range(-1, 1):
|
|
|
|
for m in range(-1, 1):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_length(struct.pack('>Biiiii', 23, i, j, k, l, m)))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# hash
|
|
|
|
for i in range(-10, 200, 20):
|
|
|
|
for j in range(-1, 1):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_length(struct.pack('>Biiiii', 22, i, j, 0, 2, 0) + ('0' * 32 * 5)))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# lt_dont_have
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_extended_handshake(add_length(struct.pack('>BBi', 20, 7, -1))))
|
|
|
|
messages.append(add_extended_handshake(add_length(struct.pack('>BBi', 20, 7, 0))))
|
|
|
|
messages.append(add_extended_handshake(add_length(struct.pack('>BBi', 20, 7, 0x7fffffff))))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# share mode
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_extended_handshake(add_length(struct.pack('BBB', 20, 8, 255))))
|
|
|
|
messages.append(add_extended_handshake(add_length(struct.pack('BBB', 20, 8, 0))))
|
|
|
|
messages.append(add_extended_handshake(add_length(struct.pack('BBB', 20, 8, 1))))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# holepunch
|
|
|
|
for i in range(0, 2):
|
|
|
|
for j in range(0, 1):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_extended_handshake(add_length(struct.pack('>BBBBiH', 20, 4, i, j, 0, 0))))
|
|
|
|
messages.append(add_extended_handshake(add_length(struct.pack('>BBBBiiH', 20, 4, i, j, 0, 0, 0))))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# upload only
|
|
|
|
for i in range(0, 1):
|
2019-08-13 08:34:41 +02:00
|
|
|
messages.append(add_extended_handshake(add_length(struct.pack('BBB', 20, 3, i))))
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
# bitfields
|
2019-08-23 13:43:34 +02:00
|
|
|
bitfield_len = (100 + 7) // 8
|
2019-04-21 21:38:23 +02:00
|
|
|
|
|
|
|
for i in range(256):
|
|
|
|
messages.append(add_length(struct.pack('B', 5) + (chr(i) * bitfield_len)))
|
|
|
|
|
|
|
|
mixes = []
|
|
|
|
|
|
|
|
for i in range(200):
|
|
|
|
shuffle(messages)
|
|
|
|
mixes.append(''.join(messages[1:20]))
|
|
|
|
|
|
|
|
messages += mixes
|
|
|
|
|
|
|
|
for m in messages:
|
|
|
|
f = open('corpus/peer_conn/%s' % hashlib.sha1(m).hexdigest(), 'w+')
|
2019-08-13 08:34:41 +02:00
|
|
|
f.write(add_reserved(m))
|
2019-04-21 21:38:23 +02:00
|
|
|
f.close()
|