|
1 | | -import dis |
2 | | -import os |
3 | | -import sys |
4 | | - |
5 | | -# Import function and class from obfuscated module |
6 | | -from queens import main, Queens |
7 | | - |
8 | | -# Call obfuscated function |
9 | | -main() |
10 | | - |
11 | | -# Check __file__ of obfuscated module "queens" is filename in target machine |
12 | | -import queens |
13 | | -if os.path.abspath(queens.__file__) == os.path.abspath(os.path.join(os.path.dirname(__file__), "queens.py")): |
14 | | - print("The value of __file__ is OK") |
15 | | - |
16 | | -# Check __wraparmor__ can't be called out of decorator |
17 | | -try: |
18 | | - from builtins import __wraparmor__ |
19 | | -except Exception: |
20 | | - from __builtin__ import __wraparmor__ |
21 | | -try: |
22 | | - __wraparmor__(main) |
23 | | -except Exception as e: |
24 | | - print('__wraparmor__ can not be called out of decorator') |
25 | | - |
26 | | -# Check filename in trackback |
27 | | -try: |
28 | | - queens.test_exception() |
29 | | -except Exception: |
30 | | - from traceback import print_exc |
31 | | - print_exc() |
32 | | - |
33 | | -# Check original func can not be got from exception frame |
34 | | -try: |
35 | | - queens.test_exception() |
36 | | -except Exception: |
37 | | - import inspect |
38 | | - for exc_tb in inspect.trace(): |
39 | | - frame = exc_tb[0] |
40 | | - print('Found frame of function %s' % exc_tb[3]) |
41 | | - if frame.f_locals.get('func') is None \ |
42 | | - and frame.f_locals.get('filename') is None \ |
43 | | - and frame.f_locals.get('n') is None: |
44 | | - print('Can not get data from frame.f_locals') |
45 | | - |
46 | | -# Check callback |
47 | | -def mycallback(): |
48 | | - frame = sys._getframe(1) |
49 | | - if len(frame.f_locals) == 0: |
50 | | - print('Got empty from callback') |
51 | | -queens.test_callback(mycallback) |
52 | | - |
53 | | -# Check generator |
54 | | -a = list(queens.simple_generator(10)) |
55 | | -if len(a) == 10: |
56 | | - print('Generator works well') |
57 | | - |
58 | | -# Check nested |
59 | | -func1 = queens.factory() |
60 | | -func2 = queens.factory() |
61 | | -func1(func2) |
62 | | -print('Shared code object works well') |
63 | | - |
64 | | -# Access original func_code will crash: Segmentation fault |
65 | | -# print(dis.dis(main.orig_func)) |
66 | | -# print(dis.dis(Queens.solve.orig_func)) |
| 1 | +import dis |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +# Import function and class from obfuscated module |
| 6 | +from queens import main, Queens |
| 7 | + |
| 8 | +# Call obfuscated function |
| 9 | +main() |
| 10 | + |
| 11 | +# Check __file__ of obfuscated module "queens" is filename in target machine |
| 12 | +import queens |
| 13 | +if os.path.abspath(queens.__file__) == os.path.abspath(os.path.join(os.path.dirname(__file__), "queens.py")): |
| 14 | + print("The value of __file__ is OK") |
| 15 | + |
| 16 | +# Check __wraparmor__ can't be called out of decorator |
| 17 | +try: |
| 18 | + from builtins import __wraparmor__ |
| 19 | +except Exception: |
| 20 | + from __builtin__ import __wraparmor__ |
| 21 | +try: |
| 22 | + __wraparmor__(main) |
| 23 | +except Exception as e: |
| 24 | + print('__wraparmor__ can not be called out of decorator') |
| 25 | + |
| 26 | +# Check filename in trackback |
| 27 | +try: |
| 28 | + queens.test_exception() |
| 29 | +except Exception: |
| 30 | + from traceback import print_exc |
| 31 | + print_exc() |
| 32 | + |
| 33 | +# Check original func can not be got from exception frame |
| 34 | +try: |
| 35 | + queens.test_exception() |
| 36 | +except Exception: |
| 37 | + import inspect |
| 38 | + for exc_tb in inspect.trace(): |
| 39 | + frame = exc_tb[0] |
| 40 | + print('Found frame of function %s' % exc_tb[3]) |
| 41 | + if frame.f_locals.get('func') is None \ |
| 42 | + and frame.f_locals.get('filename') is None \ |
| 43 | + and frame.f_locals.get('n') is None: |
| 44 | + print('Can not get data from frame.f_locals') |
| 45 | + |
| 46 | +# Check callback |
| 47 | +def mycallback(): |
| 48 | + frame = sys._getframe(1) |
| 49 | + if len(frame.f_locals) == 0: |
| 50 | + print('Got empty from callback') |
| 51 | +queens.test_callback(mycallback) |
| 52 | + |
| 53 | +# Check generator |
| 54 | +a = list(queens.simple_generator(10)) |
| 55 | +if len(a) == 10: |
| 56 | + print('Generator works well') |
| 57 | + |
| 58 | +# Check nested |
| 59 | +func1 = queens.factory() |
| 60 | +func2 = queens.factory() |
| 61 | +func1(func2) |
| 62 | +print('Shared code object works well') |
| 63 | + |
| 64 | +# Access original func_code will crash: Segmentation fault |
| 65 | +# print(dis.dis(main.orig_func)) |
| 66 | +# print(dis.dis(Queens.solve.orig_func)) |
0 commit comments