Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
patchwork/patchwork/core.py at main · GoZippy/patchwork · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
GoZippy
/
patchwork
Public
forked from
bikini/patchwork
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
patchwork
/
patchwork
/
core.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
146 lines (139 loc) · 6.55 KB
main
Breadcrumbs
patchwork
/
patchwork
/
core.py
Copy path
Top
File metadata and controls
Code
Blame
146 lines (139 loc) · 6.55 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
145
146
from
__future__
import
annotations
import
ast
from
pathlib
import
Path
from
.
abyss
import
ASSETS_NAME
as
_ABYSS_ASSETS_NAME
,
DISPATCH_NAME
as
_ABYSS_DISPATCH_NAME
,
AbyssTransformer
,
build_runtime_stmts
as
build_abyss_runtime
,
encode_assets
as
encode_abyss_assets
from
.
lazy
import
encrypt_user_functions
from
.
loader
import
build_loader
from
.
packer
import
pack
from
.
transforms
import
FStringLowerer
,
IdentifierRenamer
,
MatchLowerer
,
NumberObfuscator
,
OpaquePredicateInjector
,
StringEncryptor
,
build_decrypt_helper
,
collect_skip_names
from
.
transforms
.
junk
import
JunkBranchInjector
from
.
transforms
.
mba
import
MBATransformer
from
.
transforms
.
opaque
import
build_seed_stmt
from
.
util
import
make_rng
_SEED_NAME
=
'_pw_seed_value'
_DEC_S_NAME
=
'_pw_dec_str'
_DEC_B_NAME
=
'_pw_dec_bytes'
_CONST_POOL_NAME
=
'_pw_literal_pool'
_RESOLVE_NAME
=
'__pw_resolve_lazy__'
def
_insert_runtime_stmts
(
tree
:
ast
.
Module
,
stmts
:
list
[
ast
.
stmt
])
->
None
:
if
not
stmts
:
return
insert_at
=
0
if
(
tree
.
body
and
isinstance
(
tree
.
body
[
0
],
ast
.
Expr
)
and
isinstance
(
tree
.
body
[
0
].
value
,
ast
.
Constant
)
and
isinstance
(
tree
.
body
[
0
].
value
.
value
,
str
)
):
insert_at
=
1
while
(
insert_at
<
len
(
tree
.
body
)
and
isinstance
(
tree
.
body
[
insert_at
],
ast
.
ImportFrom
)
and
tree
.
body
[
insert_at
].
module
==
'__future__'
):
insert_at
+=
1
tree
.
body
[
insert_at
:
insert_at
]
=
stmts
def
_normalize_future_imports
(
tree
:
ast
.
Module
)
->
None
:
if
not
tree
.
body
:
return
doc
:
list
[
ast
.
stmt
]
=
[]
rest
=
tree
.
body
if
(
rest
and
isinstance
(
rest
[
0
],
ast
.
Expr
)
and
isinstance
(
rest
[
0
].
value
,
ast
.
Constant
)
and
isinstance
(
rest
[
0
].
value
.
value
,
str
)
):
doc
=
[
rest
[
0
]]
rest
=
rest
[
1
:]
future
=
[
stmt
for
stmt
in
rest
if
isinstance
(
stmt
,
ast
.
ImportFrom
)
and
stmt
.
module
==
'__future__'
]
other
=
[
stmt
for
stmt
in
rest
if
not
(
isinstance
(
stmt
,
ast
.
ImportFrom
)
and
stmt
.
module
==
'__future__'
)]
tree
.
body
=
[
*
doc
,
*
future
,
*
other
]
class
Obfuscator
:
def
__init__
(
self
,
*
,
seed
:
int
|
None
=
None
,
rename
:
bool
=
True
,
encrypt_strings
:
bool
=
True
,
obfuscate_numbers
:
bool
=
True
,
opaque_predicates
:
bool
=
True
,
mba
:
bool
=
True
,
junk_branches
:
bool
=
True
,
lazy_funcs
:
bool
=
True
,
anti_debug
:
bool
=
True
,
layers
:
int
=
3
,
stage2_layers
:
int
=
3
,
keep
:
set
[
str
]
|
None
=
None
,
abyss
:
bool
=
False
,
abyss_functions
:
list
[
str
]
|
set
[
str
]
|
tuple
[
str
, ...]
|
None
=
None
,
lower_fstrings
:
bool
=
True
,
lower_match
:
bool
=
True
):
self
.
rng
,
self
.
seed
=
make_rng
(
seed
)
self
.
rename
=
rename
self
.
encrypt_strings
=
encrypt_strings
self
.
obfuscate_numbers
=
obfuscate_numbers
self
.
opaque_predicates
=
opaque_predicates
self
.
mba
=
mba
self
.
junk_branches
=
junk_branches
self
.
lazy_funcs
=
lazy_funcs
self
.
anti_debug
=
anti_debug
self
.
layers
=
max
(
1
,
int
(
layers
))
self
.
stage2_layers
=
max
(
1
,
int
(
stage2_layers
))
self
.
keep_extra
=
set
(
keep
or
())
self
.
abyss
=
bool
(
abyss
)
self
.
abyss_functions
=
set
(
abyss_functions
or
())
self
.
lower_fstrings
=
bool
(
lower_fstrings
)
self
.
lower_match
=
bool
(
lower_match
)
self
.
abyss_stats
:
dict
[
str
,
object
]
=
{
"protected"
: [],
"skipped"
: []}
def
obfuscate
(
self
,
source
:
str
)
->
str
:
tree
=
ast
.
parse
(
source
)
skip
=
collect_skip_names
(
tree
)
if
self
.
lower_fstrings
:
tree
=
FStringLowerer
().
visit
(
tree
)
if
self
.
lower_match
:
tree
=
MatchLowerer
().
visit
(
tree
)
abyss_runtime_stmts
:
list
[
ast
.
stmt
]
=
[]
abyss_keep
:
set
[
str
]
=
set
()
if
self
.
abyss
or
self
.
abyss_functions
:
abyss_transformer
=
AbyssTransformer
(
self
.
rng
,
targets
=
self
.
abyss_functions
or
None
,
auto
=
self
.
abyss
)
tree
=
abyss_transformer
.
protect
(
tree
)
abyss_keep
=
abyss_transformer
.
keep_names
|
{
_ABYSS_DISPATCH_NAME
,
_ABYSS_ASSETS_NAME
}
self
.
abyss_stats
=
{
"protected"
:
list
(
abyss_transformer
.
protected
),
"skipped"
:
list
(
abyss_transformer
.
skipped
),
}
if
abyss_transformer
.
assets
:
abyss_runtime_stmts
=
build_abyss_runtime
(
encode_abyss_assets
(
abyss_transformer
.
assets
,
self
.
rng
),
self
.
rng
)
seed_used
=
False
if
self
.
opaque_predicates
:
opi
=
OpaquePredicateInjector
(
self
.
rng
,
_SEED_NAME
)
tree
=
opi
.
visit
(
tree
)
seed_used
=
seed_used
or
opi
.
injected
if
self
.
junk_branches
:
jbi
=
JunkBranchInjector
(
self
.
rng
,
_SEED_NAME
)
tree
=
jbi
.
visit
(
tree
)
seed_used
=
seed_used
or
jbi
.
injected
if
seed_used
:
_insert_runtime_stmts
(
tree
, [
build_seed_stmt
(
_SEED_NAME
)])
if
self
.
mba
:
tree
=
MBATransformer
(
self
.
rng
).
visit
(
tree
)
if
self
.
obfuscate_numbers
:
tree
=
NumberObfuscator
(
self
.
rng
).
visit
(
tree
)
if
self
.
encrypt_strings
:
enc
=
StringEncryptor
(
self
.
rng
,
_DEC_S_NAME
,
_DEC_B_NAME
,
_CONST_POOL_NAME
)
tree
=
enc
.
visit
(
tree
)
runtime_stmts
=
[]
pool_stmt
=
enc
.
build_pool_stmt
()
if
pool_stmt
is
not
None
:
runtime_stmts
.
append
(
pool_stmt
)
runtime_stmts
.
extend
(
build_decrypt_helper
(
_DEC_S_NAME
,
_DEC_B_NAME
))
_insert_runtime_stmts
(
tree
,
runtime_stmts
)
if
self
.
rename
:
renamer
=
IdentifierRenamer
(
self
.
rng
,
keep
=
skip
|
self
.
keep_extra
|
abyss_keep
|
{
_RESOLVE_NAME
})
tree
=
renamer
.
visit
(
tree
)
if
abyss_runtime_stmts
:
_insert_runtime_stmts
(
tree
,
abyss_runtime_stmts
)
_normalize_future_imports
(
tree
)
ast
.
fix_missing_locations
(
tree
)
user_code
=
compile
(
tree
,
'<patchwork>'
,
'exec'
)
if
self
.
lazy_funcs
:
user_code
,
lazy_blobs
=
encrypt_user_functions
(
user_code
,
self
.
rng
,
_RESOLVE_NAME
)
else
:
lazy_blobs
=
[]
user_payload
,
user_keys
=
pack
(
user_code
,
self
.
rng
,
layers
=
self
.
layers
)
return
build_loader
(
user_payload
,
user_keys
,
lazy_blobs
,
resolver_name
=
_RESOLVE_NAME
,
rng
=
self
.
rng
,
anti_debug
=
self
.
anti_debug
,
stage2_layers
=
self
.
stage2_layers
)
def
obfuscate
(
source
:
str
,
**
kwargs
)
->
str
:
return
Obfuscator
(
**
kwargs
).
obfuscate
(
source
)
def
obfuscate_file
(
input_path
:
str
|
Path
,
output_path
:
str
|
Path
|
None
=
None
,
**
kwargs
)
->
Path
:
inp
=
Path
(
input_path
)
src
=
inp
.
read_text
(
encoding
=
'utf-8'
)
out
=
obfuscate
(
src
,
**
kwargs
)
if
output_path
is
None
:
outp
=
inp
.
with_name
(
inp
.
stem
+
'_obf.py'
)
else
:
outp
=
Path
(
output_path
)
outp
.
write_text
(
out
,
encoding
=
'utf-8'
)
return
outp
You can’t perform that action at this time.