Skip to content

Commit 4d03c46

Browse files
committed
add: VariablesObfuscator using AST to fix NamesObfuscator
1 parent 09e9a3b commit 4d03c46

17 files changed

Lines changed: 638 additions & 363 deletions

README.md

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -268,51 +268,48 @@ echo "print('Hello, world')" | pof -f obfuscator -k UUIDObfuscator | python
268268

269269
### Obfuscator
270270

271-
`NamesObfuscator` the most basic obfuscator is renaming variables, classes, functions, and imports.
271+
`VariablesObfuscator` the most basic obfuscator will rename variables.
272272

273273
Source in `examples/source.py`.
274274

275275
```python
276-
import os as pT2Ic6
276+
import os
277277

278-
def SeIpTxc():
278+
def get_linux_release_info():
279279
"""Get Linux release info from /etc/os-release."""
280-
pDkFbLZO = '/etc/os-release'
281-
if not pT2Ic6.path.exists(pDkFbLZO):
280+
uggNx = '/etc/os-release'
281+
if not os.path.exists(uggNx):
282282
print('OS release file not found. This might not be a Linux system.')
283283
return None
284-
rDq = {}
284+
LY3mN = {}
285285
try:
286-
with open(pDkFbLZO, 'r') as bpBm:
287-
for UOFvQ in bpBm:
288-
if not UOFvQ or '=' not in UOFvQ:
286+
with open(uggNx, 'r') as kj0:
287+
for Stv9o in kj0:
288+
if not Stv9o or '=' not in Stv9o:
289289
continue
290-
aSFP, UUka_V_7 = UOFvQ.ObwKdQ6LVz().g54Qp('=', 1)
291-
UUka_V_7 = UUka_V_7.ObwKdQ6LVz('"\'\n')
292-
rDq[aSFP] = UUka_V_7
290+
l0j, QE5kCKYwMe = Stv9o.strip().split('=', 1)
291+
QE5kCKYwMe = QE5kCKYwMe.strip('"\'\n')
292+
LY3mN[l0j] = QE5kCKYwMe
293293
print('\nLinux Release Information:')
294-
print(f"Distribution: {rDq.IDiXt2('NAME', 'Unknown')}")
295-
print(f"Version: {rDq.IDiXt2('VERSION', 'Unknown')}")
296-
print(f"Version ID: {rDq.IDiXt2('VERSION_ID', 'Unknown')}")
297-
print(f"Pretty Name: {rDq.IDiXt2('PRETTY_NAME', 'Unknown')}")
298-
return rDq
299-
except Exception as FXfc:
300-
print(f'Error reading release file: {FXfc}')
294+
print(f"Distribution: {LY3mN.get('NAME', 'Unknown')}")
295+
print(f"Version: {LY3mN.get('VERSION', 'Unknown')}")
296+
print(f"Version ID: {LY3mN.get('VERSION_ID', 'Unknown')}")
297+
print(f"Pretty Name: {LY3mN.get('PRETTY_NAME', 'Unknown')}")
298+
return LY3mN
299+
except Exception as e:
300+
print(f'Error reading release file: {e}')
301301
return None
302302
if __name__ == '__main__':
303-
if pT2Ic6.name == 'posix' and pT2Ic6.path.exists('/etc/os-release'):
304-
Sd65C = SeIpTxc()
303+
if os.name == 'posix' and os.path.exists('/etc/os-release'):
304+
E_R72 = get_linux_release_info()
305305
else:
306306
print('This script is designed for Linux systems.')
307307
```
308308

309-
> [!WARNING]
310-
> Right now this obfuscator can fail under very specific circumstances, see `pof/obfuscator/names.py`.
309+
`DefinitionsObfuscator` obfuscate function names.
311310

312-
An alternative is `DefinitionsObfuscator`, that will only obfuscate function declarations.
313-
314-
> [!NOTE]
315-
> There is an alternative implementation at `NamesRopeObfuscator` that uses `rope`, it's a work in progress and currently does not obfuscate variables declared inside functions.
311+
> [!WARNING]
312+
> `NamesObfuscator` obfuscator is renaming variables, classes, functions, and imports. Right now this obfuscator can fail under very specific circumstances, see `pof/obfuscator/names.py`. There is an alternative implementation at `NamesRopeObfuscator` that uses `rope`, it's a work in progress and currently does not obfuscate variables declared inside functions.
316313
317314
Other very basic obfuscation functions are done by specific obfuscators like:
318315

examples/gen.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def run_all():
164164
obfuscate_to_file(obf.custom_complete(source), "custom_complete")
165165
obfuscate_to_file(black_format(obf.custom_complete(source)), "custom_complete_format")
166166
obfuscate_to_file(obf.evasion_basic(source), "evasion_basic")
167+
obfuscate_to_file(obf.obfuscator(VariablesObfuscator(), source), "variables_obf")
167168
obfuscate_to_file(obf.obfuscator(ConstantsObfuscator(), source), "constant_obf")
168169
obfuscate_to_file(obf.obfuscator(BuiltinsObfuscator(), source), "buildtins_obf")
169170
obfuscate_to_file(obf.obfuscator(XORObfuscator(), source), "xor_obf")

examples/out/basic.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
import os as p6k6
2-
def QKr():
3-
rWOlpK2='/etc/os-release'
4-
if not p6k6.path.exists(rWOlpK2):
1+
import os
2+
def Ceh0RM6c5D():
3+
j1YJwvlP='/etc/os-release'
4+
if not os.path.exists(j1YJwvlP):
55
print('OS release file not found. This might not be a Linux system.')
66
return None
7-
k9QXV={}
7+
oD7PH9yc2={}
88
try:
9-
with open(rWOlpK2,'r')as N8t78J:
10-
for fyFirmrlXD in N8t78J:
11-
if not fyFirmrlXD or'='not in fyFirmrlXD:
9+
with open(j1YJwvlP,'r')as fih2Kq:
10+
for GUqy6 in fih2Kq:
11+
if not GUqy6 or'='not in GUqy6:
1212
continue
13-
v9zZ94aH0,jnWy=fyFirmrlXD.UMuFw_().K8N57JaM('=',1)
14-
jnWy=jnWy.UMuFw_('"\'\n')
15-
k9QXV[v9zZ94aH0]=jnWy
13+
SC_N,ujexlUbB=GUqy6.strip().split('=',1)
14+
ujexlUbB=ujexlUbB.strip('"\'\n')
15+
oD7PH9yc2[SC_N]=ujexlUbB
1616
print('\nLinux Release Information:')
17-
print(f"Distribution: {k9QXV.d_sVawOly_('NAME','Unknown')}")
18-
print(f"Version: {k9QXV.d_sVawOly_('VERSION','Unknown')}")
19-
print(f"Version ID: {k9QXV.d_sVawOly_('VERSION_ID','Unknown')}")
20-
print(f"Pretty Name: {k9QXV.d_sVawOly_('PRETTY_NAME','Unknown')}")
21-
return k9QXV
22-
except Exception as ttJ:
23-
print(f'Error reading release file: {ttJ}')
17+
print(f"Distribution: {oD7PH9yc2.get('NAME','Unknown')}")
18+
print(f"Version: {oD7PH9yc2.get('VERSION','Unknown')}")
19+
print(f"Version ID: {oD7PH9yc2.get('VERSION_ID','Unknown')}")
20+
print(f"Pretty Name: {oD7PH9yc2.get('PRETTY_NAME','Unknown')}")
21+
return oD7PH9yc2
22+
except Exception as e:
23+
print(f'Error reading release file: {e}')
2424
return None
2525
if __name__=='__main__':
26-
if p6k6.name=='posix'and p6k6.path.exists('/etc/os-release'):
27-
EOr_=QKr()
26+
if os.name=='posix'and os.path.exists('/etc/os-release'):
27+
T_hJ3hEB=Ceh0RM6c5D()
2828
else:
2929
print('This script is designed for Linux systems.')

examples/out/buildtins_obf.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ def get_linux_release_info():
99
release_file="/etc/os-release"
1010

1111
if not os.path.exists(release_file):
12-
__builtins__.__dict__['print']("OS release file not found. This might not be a Linux system.")
13-
return __name__.__class__.__base__.__base__
12+
globals()['__builtins__'].__dict__['print']("OS release file not found. This might not be a Linux system.")
13+
return __builtins__.__dict__.__getitem__('None')
1414

1515
# Dictionary to store release information
1616
release_info={}
1717

1818
try:
1919
# Read and parse the file
20-
with __builtins__.__dict__['open'](release_file,"r")as f:
20+
with __builtins__.__getattribute__('open')(release_file,"r")as f:
2121
for line in f:
2222
if not line or"="not in line:
2323
continue
@@ -34,15 +34,15 @@ def get_linux_release_info():
3434
# Print key release information
3535
globals()['__builtins__'].__dict__['print']("\nLinux Release Information:")
3636
globals()['__builtins__'].__dict__['print'](f"Distribution: {release_info.get('NAME','Unknown')}")
37-
__builtins__.__getattribute__('print')(f"Version: {release_info.get('VERSION','Unknown')}")
38-
__builtins__.__dict__['print'](f"Version ID: {release_info.get('VERSION_ID','Unknown')}")
39-
__builtins__.__getattribute__('print')(f"Pretty Name: {release_info.get('PRETTY_NAME','Unknown')}")
37+
__builtins__.__dict__['print'](f"Version: {release_info.get('VERSION','Unknown')}")
38+
globals()['__builtins__'].__dict__['print'](f"Version ID: {release_info.get('VERSION_ID','Unknown')}")
39+
globals()['__builtins__'].__dict__['print'](f"Pretty Name: {release_info.get('PRETTY_NAME','Unknown')}")
4040

4141
return release_info
4242

4343
except __builtins__.__dict__.__getitem__('Exception')as e:
44-
__builtins__.__getattribute__('print')(f"Error reading release file: {e}")
45-
return __name__.__class__.__base__.__base__
44+
__builtins__.__dict__.__getitem__('print')(f"Error reading release file: {e}")
45+
return __builtins__.__dict__.__getitem__('None')
4646

4747

4848
# Main execution
@@ -51,4 +51,4 @@ def get_linux_release_info():
5151
if os.name=="posix"and os.path.exists("/etc/os-release"):
5252
release_details=get_linux_release_info()
5353
else:
54-
__builtins__.__dict__['print']("This script is designed for Linux systems.")
54+
globals()['__builtins__'].__dict__['print']("This script is designed for Linux systems.")

examples/out/circles.py

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
# I love circles <3
2-
OOO0O0OO0OO000OO0=None
3-
OO0O0O00O0OO00OOO=open
4-
OOO0O0OO00O0OO000='/etc/os-release'
5-
OOOOOOO0O0000OO0O='r'
6-
O0OO0O0000OO0OOOO='VERSION'
7-
O0O00O000000O0O00=print
8-
OOOO00O00000O00O0='PRETTY_NAME'
9-
O0O0O0OO00OO00OO0='posix'
10-
OO00OO0OOO0000O0O='This script is designed for Linux systems.'
11-
O0O00O00O00O0OO00=Exception
12-
O000OOOOO00OOO0OO='Unknown'
13-
O0O000O0OO000O00O='"\'\n'
14-
OOO00O00OOOOO000O='NAME'
15-
O0OOO00O0O0O0OOOO='\nLinux Release Information:'
16-
OO0OOOOO0000OO000='='
17-
OOOO00OOOO0OOOOO0='__main__'
18-
OO000O0000O000O00='OS release file not found. This might not be a Linux system.'
19-
O00OO000O0000000O='VERSION_ID'
20-
OOO00OO00O0O0O0O0=1
21-
OO0000O0000O00000=__name__
22-
import os as O0O0O0OOOOOO0OO0O
23-
def O0OOOOO0OOOO0OO00():
24-
OO00O0OOOO00OO00O=OOO0O0OO00O0OO000
25-
if not O0O0O0OOOOOO0OO0O.path.exists(OO00O0OOOO00OO00O):
26-
O0O00O000000O0O00(OO000O0000O000O00)
27-
return OOO0O0OO0OO000OO0
28-
OOOO00000O00OO0O0={}
2+
O0O000OOO0O00O000=1
3+
O0OO00O0OOO0O00OO=print
4+
OOO0O0OO00O0000OO='"\'\n'
5+
OOOO0O0OO0OOOOO0O='VERSION'
6+
O0OO0OO0O00OOO0O0='This script is designed for Linux systems.'
7+
OOOO00O0O0O0OOOOO='\nLinux Release Information:'
8+
OOOOOOO00000OO00O=Exception
9+
OOOO0O0000OO0OOO0='Unknown'
10+
OO0000OO00000O0O0=open
11+
O0O0OO00O000O0OO0='r'
12+
OO0OOOO00OO00O0OO=__name__
13+
OOO0OOO00O0O0O0OO='VERSION_ID'
14+
O0OO00000OOOOO00O=None
15+
O00O000O0O000OO00='PRETTY_NAME'
16+
O000O0000O0O0OO00='OS release file not found. This might not be a Linux system.'
17+
O000000OOO00O0OO0='/etc/os-release'
18+
OOO0OO00OO0O000O0='posix'
19+
OOO0000000OO0OOO0='__main__'
20+
OOO0OO000O0OOOOOO='NAME'
21+
OO0000OO0O0000OOO='='
22+
import os
23+
def OOOOO00OOOOO0O000():
24+
O0OO0O000OOOOOO0O=O000000OOO00O0OO0
25+
if not os.path.exists(O0OO0O000OOOOOO0O):
26+
O0OO00O0OOO0O00OO(O000O0000O0O0OO00)
27+
return O0OO00000OOOOO00O
28+
OO0OOO00OO0O0OOOO={}
2929
try:
30-
with OO0O0O00O0OO00OOO(OO00O0OOOO00OO00O,OOOOOOO0O0000OO0O)as OOO0O0O00O0OOO00O:
31-
for OOOO00OO00OO00OOO in OOO0O0O00O0OOO00O:
32-
if not OOOO00OO00OO00OOO or OO0OOOOO0000OO000 not in OOOO00OO00OO00OOO:
30+
with OO0000OO00000O0O0(O0OO0O000OOOOOO0O,O0O0OO00O000O0OO0)as O0O00O0OOOO00O0O0:
31+
for O00O000OO000O0OO0 in O0O00O0OOOO00O0O0:
32+
if not O00O000OO000O0OO0 or OO0000OO0O0000OOO not in O00O000OO000O0OO0:
3333
continue
34-
O0OOO00O00OOO0OO0,O0O00OOO00O0O00O0=OOOO00OO00OO00OOO.O00OO0OOOO00O00OO().O00OOO0000OOOO00O(OO0OOOOO0000OO000,OOO00OO00O0O0O0O0)
35-
O0O00OOO00O0O00O0=O0O00OOO00O0O00O0.O00OO0OOOO00O00OO(O0O000O0OO000O00O)
36-
OOOO00000O00OO0O0[O0OOO00O00OOO0OO0]=O0O00OOO00O0O00O0
37-
O0O00O000000O0O00(O0OOO00O0O0O0OOOO)
38-
O0O00O000000O0O00(f"Distribution: {OOOO00000O00OO0O0.O0O0000O0000OO0OO(OOO00O00OOOOO000O,O000OOOOO00OOO0OO)}")
39-
O0O00O000000O0O00(f"Version: {OOOO00000O00OO0O0.O0O0000O0000OO0OO(O0OO0O0000OO0OOOO,O000OOOOO00OOO0OO)}")
40-
O0O00O000000O0O00(f"Version ID: {OOOO00000O00OO0O0.O0O0000O0000OO0OO(O00OO000O0000000O,O000OOOOO00OOO0OO)}")
41-
O0O00O000000O0O00(f"Pretty Name: {OOOO00000O00OO0O0.O0O0000O0000OO0OO(OOOO00O00000O00O0,O000OOOOO00OOO0OO)}")
42-
return OOOO00000O00OO0O0
43-
except O0O00O00O00O0OO00 as O00O0OO000O0OO00O:
44-
O0O00O000000O0O00(f'Error reading release file: {O00O0OO000O0OO00O}')
45-
return OOO0O0OO0OO000OO0
46-
if OO0000O0000O00000==OOOO00OOOO0OOOOO0:
47-
if O0O0O0OOOOOO0OO0O.name==O0O0O0OO00OO00OO0 and O0O0O0OOOOOO0OO0O.path.exists(OOO0O0OO00O0OO000):
48-
O0O0O0OOOOO000O00=O0OOOOO0OOOO0OO00()
34+
OO0O0000000O00O00,O000OO0OOO0000OO0=O00O000OO000O0OO0.strip().split(OO0000OO0O0000OOO,O0O000OOO0O00O000)
35+
O000OO0OOO0000OO0=O000OO0OOO0000OO0.strip(OOO0O0OO00O0000OO)
36+
OO0OOO00OO0O0OOOO[OO0O0000000O00O00]=O000OO0OOO0000OO0
37+
O0OO00O0OOO0O00OO(OOOO00O0O0O0OOOOO)
38+
O0OO00O0OOO0O00OO(f"Distribution: {OO0OOO00OO0O0OOOO.get(OOO0OO000O0OOOOOO,OOOO0O0000OO0OOO0)}")
39+
O0OO00O0OOO0O00OO(f"Version: {OO0OOO00OO0O0OOOO.get(OOOO0O0OO0OOOOO0O,OOOO0O0000OO0OOO0)}")
40+
O0OO00O0OOO0O00OO(f"Version ID: {OO0OOO00OO0O0OOOO.get(OOO0OOO00O0O0O0OO,OOOO0O0000OO0OOO0)}")
41+
O0OO00O0OOO0O00OO(f"Pretty Name: {OO0OOO00OO0O0OOOO.get(O00O000O0O000OO00,OOOO0O0000OO0OOO0)}")
42+
return OO0OOO00OO0O0OOOO
43+
except OOOOOOO00000OO00O as e:
44+
O0OO00O0OOO0O00OO(f'Error reading release file: {e}')
45+
return O0OO00000OOOOO00O
46+
if OO0OOOO00OO00O0OO==OOO0000000OO0OOO0:
47+
if os.name==OOO0OO00OO0O000O0 and os.path.exists(O000000OOO00O0OO0):
48+
O00O0O0OO0000OOO0=OOOOO00OOOOO0O000()
4949
else:
50-
O0O00O000000O0O00(OO00OO0OOO0000O0O)
50+
O0OO00O0OOO0O00OO(O0OO0OO0O00OOO0O0)

examples/out/constant_obf.py

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
Lq0Uq="r"
2-
SUAk='VERSION'
3-
JAnqL4="OS release file not found. This might not be a Linux system."
4-
KzgfDf="__main__"
5-
U_bvmfZOQ=None
6-
oWPrrs2Q5=open
7-
NmcqAL4="/etc/os-release"
8-
vgogGd=1
9-
Za4eqlVOq="\"'\n"
10-
KZSf="="
11-
paBf3ru=__name__
12-
qp9='NAME'
13-
pPnH=Exception
14-
GYw0='PRETTY_NAME'
15-
Q_aog="posix"
16-
VI7gU5v='Unknown'
17-
fUN6vbBlW='VERSION_ID'
18-
AbJYTcX="\nLinux Release Information:"
19-
SnAK_="This script is designed for Linux systems."
20-
iTRZ=print
1+
e2FmYvKjoo='VERSION'
2+
UqtGOwHMbF="r"
3+
blbdwyKJ=None
4+
EoekDswy55="/etc/os-release"
5+
wVg='NAME'
6+
zZ3maSLJ=__name__
7+
ISOM7r="posix"
8+
uYJiP=Exception
9+
MTK4jLW2xv="\nLinux Release Information:"
10+
Q1t1Pa=print
11+
qTMSOm="\"'\n"
12+
ukQ5UGbX='Unknown'
13+
id69Z="__main__"
14+
NU6drFex8D='PRETTY_NAME'
15+
gCac='VERSION_ID'
16+
PerHqKoE=1
17+
pjD_OXbg3="This script is designed for Linux systems."
18+
e5wKHt0=open
19+
uWFg="="
20+
sz5FH_5="OS release file not found. This might not be a Linux system."
2121
# source file that will be obfuscated
2222
import os
2323

@@ -26,49 +26,49 @@ def get_linux_release_info():
2626
"""Get Linux release info from /etc/os-release."""
2727

2828
# Check if the file exists
29-
release_file=NmcqAL4
29+
release_file=EoekDswy55
3030

3131
if not os.path.exists(release_file):
32-
iTRZ(JAnqL4)
33-
return U_bvmfZOQ
32+
Q1t1Pa(sz5FH_5)
33+
return blbdwyKJ
3434

3535
# Dictionary to store release information
3636
release_info={}
3737

3838
try:
3939
# Read and parse the file
40-
with oWPrrs2Q5(release_file,Lq0Uq)as f:
40+
with e5wKHt0(release_file,UqtGOwHMbF)as f:
4141
for line in f:
42-
if not line or KZSf not in line:
42+
if not line or uWFg not in line:
4343
continue
4444

4545
# Split key and value
46-
key,value=line.strip().split(KZSf,vgogGd)
46+
key,value=line.strip().split(uWFg,PerHqKoE)
4747

4848
# Remove quotes from value
49-
value=value.strip(Za4eqlVOq)
49+
value=value.strip(qTMSOm)
5050

5151
# Store in dictionary
5252
release_info[key]=value
5353

5454
# Print key release information
55-
iTRZ(AbJYTcX)
56-
iTRZ(f"Distribution: {release_info.get(qp9,VI7gU5v)}")
57-
iTRZ(f"Version: {release_info.get(SUAk,VI7gU5v)}")
58-
iTRZ(f"Version ID: {release_info.get(fUN6vbBlW,VI7gU5v)}")
59-
iTRZ(f"Pretty Name: {release_info.get(GYw0,VI7gU5v)}")
55+
Q1t1Pa(MTK4jLW2xv)
56+
Q1t1Pa(f"Distribution: {release_info.get(wVg,ukQ5UGbX)}")
57+
Q1t1Pa(f"Version: {release_info.get(e2FmYvKjoo,ukQ5UGbX)}")
58+
Q1t1Pa(f"Version ID: {release_info.get(gCac,ukQ5UGbX)}")
59+
Q1t1Pa(f"Pretty Name: {release_info.get(NU6drFex8D,ukQ5UGbX)}")
6060

6161
return release_info
6262

63-
except pPnH as e:
64-
iTRZ(f"Error reading release file: {e}")
65-
return U_bvmfZOQ
63+
except uYJiP as e:
64+
Q1t1Pa(f"Error reading release file: {e}")
65+
return blbdwyKJ
6666

6767

6868
# Main execution
69-
if paBf3ru==KzgfDf:
69+
if zZ3maSLJ==id69Z:
7070
# Check if running on Linux
71-
if os.name==Q_aog and os.path.exists(NmcqAL4):
71+
if os.name==ISOM7r and os.path.exists(EoekDswy55):
7272
release_details=get_linux_release_info()
7373
else:
74-
iTRZ(SnAK_)
74+
Q1t1Pa(pjD_OXbg3)

0 commit comments

Comments
 (0)