Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
lib_base
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sikang
lib_base
Commits
cf9231dc
Commit
cf9231dc
authored
Oct 22, 2019
by
sikang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update script
parent
efe8bd97
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
132 additions
and
116 deletions
+132
-116
python/tools/garbel/package_garble.py
+132
-116
No files found.
python/tools/garbel/package_garble.py
View file @
cf9231dc
# -*-coding:utf-8-*-
# -*-coding:utf-8-*-
import
glob
,
os
import
glob
import
string
import
os
import
random
import
string
import
random
class
ActivityGarble
:
target
=
[
"Activity.java"
,
class
PackageGarble
:
"Bean.java"
,
used_names
=
[]
"Fragment.java"
,
pkg_mapping
=
{}
"Receiver.java"
,
"CertifyActiviy.java"
,
# 随机串(全小写加下划线,避免和activity混淆重复)
"BaseApplication.java"
,
def
__gen_rand_str
(
self
):
"ContactEntity.java"
,
random_str1
=
''
.
join
(
random
.
sample
(
"SmsEntity.java"
,
"abcdefghijklmnopqrstuvwxyz"
,
random
.
randint
(
4
,
8
)))
"ActivityCenter.java"
,
random_str2
=
''
.
join
(
random
.
sample
(
"Service.java"
,
"abcdefghijklmnopqrstuvwxyz"
,
random
.
randint
(
4
,
8
)))
"Collector.java"
,
return
"
%
s_
%
s"
%
(
random_str1
,
random_str2
)
"BannerMessageDto.java"
,
"BasicAck.java"
,
# 替换文件内容
"InviteResult.java"
,
"LoanAppBeanFatherStatusLogs.java"
,
def
__process_file_content
(
self
,
path
,
file_name
):
"LoanRange.java"
,
# print("processing content: %s..."%path)
"PhotoInfo.java"
,
with
open
(
path
,
"rt"
)
as
fin
:
"IncomeMessageProto.java"
,
file_content
=
fin
.
read
()
"ResponseErrorBody.java"
,
# replace the file content based on mangle settings
"RecordFilesResponse.java"
,
for
(
src
,
dst
)
in
self
.
pkg_mapping
.
items
():
"TotalAmount.java"
,
separator
=
[
"UserBankInfo.java"
,
" "
,
"."
,
"
\"
"
,
"{"
,
"}"
,
"("
,
")"
,
"["
,
"]"
,
"<"
,
">"
,
":"
,
";"
,
","
]
"YWUser.java"
,
for
sep
in
separator
:
"TDChannel.java"
file_content
=
file_content
.
replace
(
src
+
sep
,
dst
+
sep
)
]
ext
=
os
.
path
.
splitext
(
file_name
)[
1
]
used_names
=
[]
if
ext
==
'.java'
:
activity_mapping
=
{}
comment
=
"// machine renamed:
%
s
\n
"
%
file_name
file_content
=
"
%
s
%
s"
%
(
comment
,
file_content
)
#随机串
with
open
(
path
,
"wt"
)
as
fout
:
def
__gen_rand_str
(
self
):
fout
.
write
(
file_content
)
rand_str
=
''
.
join
(
random
.
choices
(
string
.
ascii_uppercase
+
string
.
ascii_lowercase
,
k
=
8
))
return
while
rand_str
in
self
.
used_names
:
rand_str
=
''
.
join
(
random
.
choices
(
string
.
ascii_uppercase
+
string
.
ascii_lowercase
,
k
=
8
))
# 搜集混淆字典
def
__find_pkgs
(
self
,
path
):
self
.
used_names
.
append
(
rand_str
)
for
root
,
dirs
,
files
in
os
.
walk
(
path
):
return
rand_str
if
len
(
dirs
)
==
0
:
mp_path
=
root
.
split
(
"main/java/"
)[
1
]
.
replace
(
"/"
,
"."
)
if
mp_path
not
in
self
.
pkg_mapping
.
keys
():
#替换文件内容
random_str
=
""
def
__process_file_content
(
self
,
path
,
file_name
):
# 按长度排序
# print("processing content: %s..."%path)
pkg_path
=
mp_path
with
open
(
path
,
"rt"
)
as
fin
:
file_content
=
fin
.
read
()
keys
=
sorted
(
self
.
pkg_mapping
.
keys
(),
# replace the file content based on mangle settings
key
=
lambda
i
:
len
(
i
),
reverse
=
True
)
for
(
src
,
dst
)
in
self
.
activity_mapping
.
items
():
for
key
in
keys
:
separator
=
[
" "
,
"."
,
"
\"
"
,
"{"
,
"}"
,
"("
,
")"
,
"["
,
"]"
,
"<"
,
">"
,
":"
,
";"
,
","
]
if
pkg_path
.
startswith
(
key
):
for
sep
in
separator
:
random_str
+=
self
.
pkg_mapping
[
key
]
file_content
=
file_content
.
replace
(
src
+
sep
,
dst
+
sep
)
pkg_path
=
pkg_path
.
replace
(
key
,
""
)
ext
=
os
.
path
.
splitext
(
file_name
)[
1
]
# 没混淆过的包名补全混淆
if
ext
==
'.java'
:
for
pkgname
in
pkg_path
.
split
(
"."
):
comment
=
"// machine renamed:
%
s
\n
"
%
file_name
if
len
(
pkgname
)
>
0
:
file_content
=
"
%
s
%
s"
%
(
comment
,
file_content
)
if
len
(
random_str
)
>
0
:
with
open
(
path
,
"wt"
)
as
fout
:
random_str
+=
"."
fout
.
write
(
file_content
)
random_str
+=
self
.
__gen_rand_str
()
return
size
=
len
(
mp_path
.
split
(
"."
))
#查找所有待混淆类 并重命名
while
size
>
0
:
def
__find_pkgs
(
self
,
path
):
if
mp_path
not
in
self
.
pkg_mapping
.
keys
():
for
root
,
dirs
,
files
in
os
.
walk
(
path
):
self
.
pkg_mapping
[
mp_path
]
=
random_str
for
dir
in
dirs
:
mp_path
=
mp_path
.
replace
(
".
%
s"
%
mp_path
.
split
(
"."
)[
size
-
1
],
""
)
directory
=
os
.
path
.
join
(
os
.
getcwd
(),
root
)
random_str
=
random_str
.
replace
(
for
file
in
files
:
".
%
s"
%
random_str
.
split
(
"."
)[
size
-
1
],
""
)
is_activity
=
False
size
-=
1
for
file_name
in
self
.
target
:
if
file
.
endswith
(
file_name
):
#重命名所有包路径
is_activity
=
True
def
__rename_pkg
(
self
,
path
):
for
root
,
dirs
,
files
in
os
.
walk
(
path
):
if
is_activity
:
if
"main/java"
in
root
:
name
=
os
.
path
.
splitext
(
file
)[
0
]
root
+=
"/"
if
name
not
in
self
.
activity_mapping
.
keys
():
mp_path
=
root
.
split
(
"main/java/"
)[
1
]
.
replace
(
"/"
,
"."
)
self
.
activity_mapping
[
name
]
=
self
.
__gen_rand_str
()
for
dir
in
dirs
:
src
=
os
.
path
.
join
(
directory
,
file
)
pkg_path
=
"
%
s
%
s"
%
(
mp_path
,
dir
)
dst
=
os
.
path
.
join
(
directory
,
"
%
s.java"
%
self
.
activity_mapping
[
name
])
for
(
key
,
value
)
in
self
.
pkg_mapping
.
items
():
os
.
rename
(
src
,
dst
)
if
key
.
startswith
(
pkg_path
):
index
=
len
(
pkg_path
.
split
(
"."
))
-
1
#遍历混淆文件
dir_name
=
value
.
split
(
"."
)[
index
]
def
__process_files
(
self
,
path
):
old_name
=
os
.
path
.
join
(
os
.
path
.
join
(
os
.
getcwd
(),
root
),
dir
)
for
root
,
dirs
,
files
in
os
.
walk
(
path
):
new_name
=
os
.
path
.
join
(
os
.
path
.
join
(
os
.
getcwd
(),
root
),
dir_name
)
directory
=
os
.
path
.
join
(
os
.
getcwd
(),
root
)
os
.
rename
(
old_name
,
new_name
)
for
file
in
files
:
print
(
"rename
%
s ->
%
s"
%
(
dir
,
dir_name
))
if
file
.
endswith
(
".java"
):
break
self
.
__process_file_content
(
os
.
path
.
join
(
directory
,
file
),
file
)
def
__process_files
(
self
,
path
):
#四大组件混淆
for
root
,
dirs
,
files
in
os
.
walk
(
path
):
def
activity_garble
(
self
,
config
):
directory
=
os
.
path
.
join
(
os
.
getcwd
(),
root
)
#遍历包
for
file
in
files
:
print
(
"find activity..."
)
if
file
.
endswith
(
".java"
):
self
.
__find_pkgs
(
"
%
s/app/src/main/java"
%
config
[
'root'
])
self
.
__process_file_content
(
self
.
__find_pkgs
(
"
%
s/lib_base/src/main/java"
%
config
[
'root'
])
os
.
path
.
join
(
directory
,
file
),
file
)
#开始混淆
# 四大组件混淆
print
(
"start garble .java..."
)
def
pkg_garble
(
self
,
config
):
self
.
__process_files
(
"
%
s/app/src/main"
%
config
[
'root'
])
# 遍历包
self
.
__process_files
(
"
%
s/lib_base/src/main"
%
config
[
'root'
])
print
(
"find activity..."
)
self
.
__find_pkgs
(
"
%
s/app/src/main/java"
%
config
[
'root'
])
#混淆AndroidManifest.xml
self
.
__find_pkgs
(
"
%
s/lib_base/src/main/java"
%
config
[
'root'
])
print
(
"start garble 混淆AndroidManifest.xml ..."
)
manifest_path
=
os
.
path
.
join
(
"
%
s/app/src/main/"
%
config
[
'root'
],
"AndroidManifest.xml"
)
keys
=
sorted
(
self
.
pkg_mapping
.
keys
(),
self
.
__process_file_content
(
manifest_path
,
"AndroidManifest.xml"
)
key
=
lambda
i
:
len
(
i
.
split
(
"."
)),
reverse
=
True
)
for
key
in
keys
:
lib_manifest_path
=
os
.
path
.
join
(
"
%
s/lib_base/src/main/"
%
config
[
'root'
],
"AndroidManifest.xml"
)
print
(
key
+
" -> "
+
self
.
pkg_mapping
[
key
])
self
.
__process_file_content
(
lib_manifest_path
,
"AndroidManifest.xml"
)
self
.
__rename_pkg
(
"
%
s/app/src/main/java"
%
config
[
'root'
])
print
(
"done"
)
self
.
__rename_pkg
(
"
%
s/lib_base/src/main/java"
%
config
[
'root'
])
\ No newline at end of file
# self.__find_pkgs("%s/lib_base/src/main/java"% config['root'])
# #开始混淆
# print("start garble .java...")
# self.__process_files("%s/app/src/main"% config['root'])
# self.__process_files("%s/lib_base/src/main"% config['root'])
# #混淆AndroidManifest.xml
# print("start garble 混淆AndroidManifest.xml ...")
# manifest_path = os.path.join("%s/app/src/main/"% config['root'], "AndroidManifest.xml")
# self.__process_file_content(manifest_path,"AndroidManifest.xml")
# lib_manifest_path = os.path.join("%s/lib_base/src/main/"% config['root'], "AndroidManifest.xml")
# self.__process_file_content(lib_manifest_path,"AndroidManifest.xml")
print
(
"done"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment