Skip to content
This repository was archived by the owner on Dec 4, 2024. It is now read-only.

Commit 6ed271c

Browse files
committed
commit
0 parents  commit 6ed271c

38 files changed

+1648
-0
lines changed

.gitignore

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store
39+
40+
### Others ###
41+
.idea/
42+
cmake-build-debug/
43+
cmake-build-release/
44+
*.exe

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# code-encryptor-plus
2+
3+
## 介绍
4+
5+
使用`JNI`加密字节码,通过`JVMTI`解密字节码以保护代码
6+
7+
提供两份`DLL`文件,一份加密一份解密,实际运行只需使用解密`DLL`文件
8+
9+
加密和解密的过程可以指定具体的包名,只加密核心关键部分
10+
11+
详细文章参考:[JVMTI 加密字节码详解](http://127.0.0.1:8080)
12+
13+
加密后的`Class`文件变成无法解析的畸形文件
14+
15+
![jd-gui](img/002.png)
16+
17+
除了开头保持了`Magic`部分,后续是无法解析的字节
18+
19+
![hex](img/003.png)
20+
21+
## 快速开始
22+
23+
加密解密部分使用`C`做一层加密,使用`汇编`二层加密,已提供编译好的`Release`版本`DLL`文件嵌入`Jar`包中
24+
25+
仅支持`Windows 64位`/`JDK-8`环境,其他版本的`JDK`只需要更换`JNI.h`头文件重新编译,其他操作系统可能需要重写
26+
27+
加密你的`Jar`包:(指定`Jar`包和`package`加密包名)
28+
29+
```shell
30+
java -jar code-encryptor-plus.jar patch --jar your-jar.jar --package com.your.pack
31+
```
32+
33+
导出解密`DLL`文件:(默认导出到`code-encryptor-plus-temp`目录)
34+
35+
```shell
36+
java -jar code-encryptor-plus.jar export
37+
```
38+
39+
使用解密`DLL`启动`Jar`包:(使用`-agentlib`参数)
40+
41+
```shell
42+
java -agentlib:D:\abs-path\decrypter=PACKAGE_NAME=com.your.pack --jar your-jar.jar
43+
```
44+
45+
另外支持了简易的`GUI`版本,选择需要加密的`Jar`文件即可一键加密
46+
47+
![screenshot](img/001.png)
48+
49+
## 其他
50+
51+
不适用于`SpringBoot`场景,存在两个问题:
52+
- `SpringBoot`不允许压缩`lib`依赖(这个有解决办法)
53+
- `SpringBoot`启动扫描会分析`class`由于加密报错
54+
55+
网上提供了两种办法,可以参考
56+
57+
参考:https://zhuanlan.zhihu.com/p/545268749
58+
59+
类似地,启动扫描`class`的代码是无法使用这种加密的
60+
61+
## 致谢
62+
63+
感谢以下项目或文章提供的思路:
64+
- https://juejin.cn/post/6844903487784894477
65+
- https://github.com/sea-boat/ByteCodeEncrypt

img/001.png

5.53 KB
Loading

img/002.png

9.25 KB
Loading

img/003.png

58.5 KB
Loading

native/CMakeLists.txt

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
3+
################ JNI CONFIG START ################
4+
5+
project(native C)
6+
7+
set(CMAKE_C_STANDARD 11)
8+
9+
# INCLUDE JNI
10+
11+
find_package(JNI REQUIRED)
12+
13+
include_directories(${JNI_INCLUDE_DIRS})
14+
15+
################ JNI CONFIG END ##################
16+
17+
enable_language(ASM_MASM)
18+
19+
add_custom_target(
20+
native_encrypt_asm ALL
21+
COMMAND ml64 /c /Fo${CMAKE_CURRENT_BINARY_DIR}/native_encrypt_asm.obj
22+
${CMAKE_CURRENT_SOURCE_DIR}/encrypt.asm
23+
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/native_encrypt_asm.obj
24+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
25+
)
26+
27+
add_custom_target(
28+
native_decrypt_asm ALL
29+
COMMAND ml64 /c /Fo${CMAKE_CURRENT_BINARY_DIR}/native_decrypt_asm.obj
30+
${CMAKE_CURRENT_SOURCE_DIR}/decrypt.asm
31+
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/native_decrypt_asm.obj
32+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
33+
)
34+
35+
add_library(
36+
encryptor SHARED
37+
core_en.h
38+
encryptor.h
39+
encryptor.c
40+
41+
xxtea_common.c
42+
xxtea_common.h
43+
xxtea_en.h
44+
xxtea_de.h
45+
xxtea.c
46+
)
47+
48+
add_library(
49+
decrypter SHARED
50+
core_de.h
51+
start.c
52+
53+
xxtea_common.c
54+
xxtea_common.h
55+
xxtea_en.h
56+
xxtea_de.h
57+
xxtea.c
58+
)
59+
60+
set_property(TARGET native_encrypt_asm PROPERTY
61+
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
62+
)
63+
64+
set_property(TARGET native_decrypt_asm PROPERTY
65+
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
66+
)
67+
68+
add_dependencies(encryptor native_encrypt_asm)
69+
70+
add_dependencies(decrypter native_decrypt_asm)
71+
72+
target_link_libraries(encryptor
73+
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/native_encrypt_asm.obj
74+
)
75+
76+
target_link_libraries(decrypter
77+
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/native_decrypt_asm.obj
78+
)
79+
80+
add_executable(
81+
xxtea_test
82+
xxtea_common.h
83+
xxtea_common.c
84+
xxtea_en.h
85+
xxtea_de.h
86+
xxtea.c
87+
xxtea_test.c
88+
)
89+
90+
add_executable(
91+
decrypt_test
92+
core_de.h
93+
decrypt_test.c
94+
)
95+
96+
target_link_libraries(decrypt_test
97+
PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/native_decrypt_asm.obj
98+
)

native/core_de.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef NATIVE_CORE_DE_H
2+
#define NATIVE_CORE_DE_H
3+
4+
#endif //NATIVE_CORE_DE_H
5+
6+
#define LOG(msg) printf("[JVMTI-LOG] %s\n", msg)
7+
8+
// SEE decrypt.asm
9+
extern void decrypt(unsigned char *, long);

native/core_en.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef NATIVE_CORE_EN_H
2+
#define NATIVE_CORE_EN_H
3+
4+
#endif //NATIVE_CORE_EN_H
5+
6+
#define LOG(msg) printf("[ENCRYPT] %s\n", msg)
7+
8+
// SEE encrypt.asm
9+
extern void encrypt(unsigned char *, long);

native/decrypt.asm

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.code
2+
; FUNCTION decrypt
3+
decrypt PROC
4+
; init
5+
push rbp
6+
mov rbp, rsp
7+
; save
8+
push rbx
9+
push rsi
10+
push rdi
11+
; char* str
12+
mov rdi, rcx
13+
; long length
14+
mov rcx, rdx
15+
; rbx = 0
16+
xor rbx, rbx
17+
; rbx = rbx + 4
18+
add rbx, 004h
19+
; signature
20+
mov rsi, rcx
21+
sub rsi, 001h
22+
mov al, byte ptr [rdi+rsi]
23+
mov ah, byte ptr [rdi+004h]
24+
mov byte ptr [rdi+004h], al
25+
mov byte ptr [rdi+rsi], ah
26+
; reset
27+
xor ah, ah
28+
xor al, al
29+
xor rsi, rsi
30+
link_start:
31+
; if ebx >= ecx goto end
32+
cmp rbx, rcx
33+
jge magic
34+
; al = str[rdi+rbx]
35+
mov al, byte ptr [rdi+rbx]
36+
; al = al ^ 22
37+
xor al, 022h
38+
; al = al -1
39+
sub al, 001h
40+
; al = ~al
41+
not al
42+
; al = al ^ 11h
43+
xor al, 011h
44+
; al = al + 2
45+
add al, 002h
46+
; str[rdi+rbx] = al
47+
mov byte ptr [rdi+rbx], al
48+
; ebx ++
49+
inc ebx
50+
; loop
51+
jmp link_start
52+
magic:
53+
; recover
54+
pop rdi
55+
pop rsi
56+
pop rbx
57+
; recover rbp
58+
pop rbp
59+
ret
60+
decrypt ENDP
61+
62+
end

native/decrypt_test.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "core_de.h"
2+
#include "stdio.h"
3+
4+
void printHex(const unsigned char *arr, int length) {
5+
for (int i = 0; i < length; i++) {
6+
printf("%02X", arr[i]);
7+
}
8+
printf("\n");
9+
}
10+
11+
int main() {
12+
unsigned char code[12] = {
13+
0xca, 0xfe, 0xba, 0xbe,
14+
0x00, 0x00, 0x00, 0x05,
15+
0x01, 0x02, 0x03, 0x04,
16+
};
17+
decrypt(code,12);
18+
printHex(code,12);
19+
}

0 commit comments

Comments
 (0)