Skip to content

Instantly share code, notes, and snippets.

@endofline
Created February 15, 2017 14:37
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 endofline/cb8b4437c8db25bb3b0cd70399ee4940 to your computer and use it in GitHub Desktop.
Save endofline/cb8b4437c8db25bb3b0cd70399ee4940 to your computer and use it in GitHub Desktop.
subverting imported libraries
# test_a.py
# in the bot, this represents the new hangups library
this_is_a_constant = 'ABCD'
def print_constant():
print(repr(this_is_a_constant))
# test_b.py
# in the bot, this represents the plugin
import test_a
# test_c.py
# in the bot, this file is the plugin loader
import test_b # plugin is loaded
import test_d # we replace the test_a module inside the plugin with this in EXAMPLE 2
# EXAMPLE 1: replace constant
# setattr(test_b.test_a, 'this_is_a_constant', 'no longer constant');
# EXAMPLE 2: replace module
# setattr(test_b, 'test_a', test_d) # replaces module with another one
test_b.test_a.print_constant()
# in example 1, this prints 'no longer constant'
# in example 2, this prints '3.142'
# test_d.py
# in the bot, this replaces new hangups (test_a.py) with the compatibility layer
def print_constant():
print('3.142');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment