-
Notifications
You must be signed in to change notification settings - Fork 5
/
pyfuck.py
144 lines (104 loc) · 3.62 KB
/
pyfuck.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#/usr/bin/python3
import re
base = set("+travels[]'()")
digits = {
0: '+all([[]])',
1: '+all([])',
2: 'all([])+all([])'
}
for i in range(3, 10):
digits[i] = digits[i-1] + digits[1]
# use paranthesis in case. Not sure if needed
for i in range(0, 10):
digits[i] = '(' + digits[i] + ')'
# print(digits)
DICT = {
# 'a': 'str(str)[' + digits[3] + ']',
'c': 'str(str)[+all([])]', # 1
# 'e': 'str(eval)[eval(str(' + digits[1] + ')+str(' + digits[9] + '))]',
'f': 'str(eval)[eval(str(' + digits[1] + ')+str(' + digits[0] + '))]', # 10
'n': 'str(eval)[' + digits[8] + ']', # 8
'o': 'str(eval)[eval(str(' + digits[1] + ')+str(' + digits[6] + '))]',
'u': 'str(eval)[' + digits[2] + ']',
'\'': 'str(str)[' + digits[7] + ']',
}
# have 'float'
DICT['.'] = "+".join(["eval('str('", DICT['f'], "'l'", DICT['o'],
"'at(" + digits[1] + "))[" + digits[1] + "]')"]) #'str(float(1))[1]'
# have str.count
DICT['h'] = "+".join(["eval('str(str'", DICT['.'],
DICT['c'], DICT['o'], DICT['u'], DICT['n'], "'t)[" + digits[4] + "]')"]) # str(str.count)[4]
## Now we have chr(), we can get every alphanumeric var
# print(DICT)
for i in DICT:
# print(i, eval(DICT[i]))
assert i == eval(DICT[i]), (i, DICT[i])
## convery every program into a string, then wrap eval(...) around it
TMP = '\x01' # substitute out single quotes
def encode(s, with_eval=False):
new_str = ''
for c in s:
# substitute for arbituary characters using chr()
if not c.isdigit() and c not in base and c not in DICT:
new_str += "'+eval('chr(" + str(ord(c)) + ")')+'"
else:
if c == '\'':
new_str += "'+" + TMP + "+'" # fix triple-quote issue
else:
new_str += c
s = "'" + new_str + "'"
new_str = ''
for c in s:
if c in base or c.isdigit() or c == TMP:
new_str += c
else:
# substitute characters in DICT with base characters
if c in ['.', 'h']:
new_str += "'+" + DICT[c] + "+'"
else:
new_str += "'+eval('" + DICT[c] + "')+'"
s = new_str
new_str = ''
# deal with numbers
def repl(matchobj):
i = matchobj.group(0)
if i == '0':
return '(+all([[]]))'
if i == '1':
return '(+all([]))'
else:
return "'+" + "+".join(['str(' + digits[int(j)] + ')' for j in i]) + "+'"
s = re.sub('\d+', repl, s)
# sub back in quote
s = s.replace(TMP, DICT['\''])
# clear out leading/trailing empty strings
s = s.replace("+''+", "+")
if s[:3] == "''+":
s = s[3:]
if s[-3:] == "+''":
s = s[:-3]
#print(str(eval(s)))
#print("\n")
return ("eval(" + s + ")") if with_eval else s
def test(s, verbose=False, with_eval=False):
print(s)
s_encoded = encode(s)
assert set(s_encoded) <= base, set(s)
assert eval("str("+s_encoded+")") == s, eval("str("+s_encoded+")")
if verbose:
print(s_encoded)
if with_eval:
print(eval("eval(" + s_encoded + ")"))
print("Encoding len: ", len(s_encoded), "\n\n")
return s_encoded
test('print("Hello, world!")', True)
test("'hi'a", True)
test('(lambda x: x**2)(5+3)', with_eval=True)
test('''
def f(x):
return x * 4
y = f(5)
print(y)
''')
test("@", True)
#test('''(lambda __builtin__: (lambda __print, __y, d: [[__print(d.x<d.y<5) for d.y in [(4)]][0] for d.x in [(3)]][0])(__builtin__.__dict__['print'],(lambda f: (lambda x: x(x))(lambda y: f(lambda *args: y(y)(*args)))),type('StateDict',(),__builtin__.__dict__)()))(__import__('__builtin__'))''')