Skip to content

Instantly share code, notes, and snippets.

@jbosboom
Created May 9, 2021 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbosboom/1438dcbc304b7325802c36257f5dede9 to your computer and use it in GitHub Desktop.
Save jbosboom/1438dcbc304b7325802c36257f5dede9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import pickle, io, collections, sys
x = collections.deque()
x.append(10)
x.append(3.14)
x.append(10000000000000)
x.append((1, 2, 3))
x.append([1, 2, 3])
x.append({1, 2, 3})
x.append({'1': 1, '2': 2, '3': 3})
x.append('ПАСЬЯНС')
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
b = io.BytesIO()
pickle.dump(x, b, protocol)
b = b.getvalue()
with open('{}.pickle'.format(protocol), 'wb') as f:
f.write(b)
if '--mangle' in sys.argv:
with open('{}-garbage.pickle'.format(protocol), 'wb') as f:
f.write(b)
f.write(b'GARBAGE')
if protocol >= 3:
with open('{}-noproto.pickle'.format(protocol), 'wb') as f:
f.write(b[2:])
with open('{}-noproto-garbage.pickle'.format(protocol), 'wb') as f:
f.write(b[2:])
f.write(b'GARBAGE')
if protocol >= 4:
if b[2] != 0x95: # FRAME opcode
raise AssertionError('expected protocol {} to use framing'.format(protocol))
with open('{}-noframe.pickle'.format(protocol), 'wb') as f:
f.write(b[:2])
f.write(b[11:])
with open('{}-noframe-garbage.pickle'.format(protocol), 'wb') as f:
f.write(b[:2])
f.write(b[11:])
f.write(b'GARBAGE')
with open('{}-noproto-noframe.pickle'.format(protocol), 'wb') as f:
f.write(b[11:])
with open('{}-noproto-noframe-garbage.pickle'.format(protocol), 'wb') as f:
f.write(b[11:])
f.write(b'GARBAGE')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment