tonyenc php解密

# 解密

拿到文件很晕,都是乱码,应该不是常规的混淆,大概率是通过so来解密,先找到php目录

1
2
[root@acmepbx:/opt/bin]#find / -name php.ini
/opt/php/etc/php.ini

Shell

找到扩展配置目录

tonyenc.ini这个文件很可疑
内容为

1
extension=tonyenc.so

Shell


网上搜了一下这个so的文件名,直接搜到了加密项目
https://github.com/lihancong/tonyenc

这个密钥很关键,我们可以在ida进行查找

在解密之前我们需要确认github这个项目和目标so的逻辑是否相同,直接把正确的key修改,根据文档在Linux下编译替换原有的so,打开网页测试代码是否正常运行,或者可以调用enc方法加密同一个文件测试。
经过测试,我编译的so可以正常使用,这样的话直接用github的解密方法吧
解密逻辑比较简单,我们可以直接单独写个程序解密


完整代码

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
#include <iostream>
#include <stdio.h>
#include <string.h>
char tonyenc_header[] = {
0x16, 0x88, 0x6f, 0x4f,
0x68, 0x46, 0x00, 0x36,
0x91, 0x61, 0x76, 0x88,
};
char tonyenc_key[] = {
0x8F, 0x49, 0x52, 0x00,
0x58, 0x9f, 0x6f, 0x93,
0x3e, 0x2e, 0x7a, 0xfa,
0xa6, 0x33, 0xf3, 0xb6,
};
int write_change_policy(const char* filename, char* buf_policy)
{
FILE* fp = NULL;
fp = fopen(filename, "w+");
if (!fp) {
return -1;
}
if (fwrite(buf_policy, 1, strlen(buf_policy), fp) < 0) {
return -1;
fclose(fp);
}
fclose(fp);
}
void tonyenc_decode(char* data, size_t len)
{
size_t i, p = 0;
for (i = 0; i < len; ++i) {
if (i & 1) {
p += tonyenc_key[p] + i;
p %= sizeof(tonyenc_key);
char t = tonyenc_key[p];
data[i] = ~data[i] ^ t;
}
}
}
int tonyenc_ext_fopen(FILE* fp, struct stat* stat_buf,char* outname)
{
char* p_data;
size_t data_len;
size_t write_len;
data_len = stat_buf->st_size - sizeof(tonyenc_header);
p_data = (char*)malloc(data_len);
fseek(fp, sizeof(tonyenc_header), SEEK_SET);
fread(p_data, data_len, 1, fp);
fclose(fp);
tonyenc_decode(p_data, data_len);
write_change_policy(outname, p_data);
return 1;

}

int main(int argc,char* argv[])
{
struct stat stat_buf;
int data_len;
char* filename;
FILE* fp;
filename = argv[1];
//printf(argv[1]);
fp = fopen(filename, "rb");
fstat(fileno(fp), &stat_buf);
data_len = stat_buf.st_size;
printf("data_lenth:%d", data_len);
tonyenc_ext_fopen(fp, &stat_buf, strcat(filename , ".dec"));

}

C

成功解密:

写了个python批量调用

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
import codecs
import time
import frida
import os
def findfile(target_dir, target_suffix="php"):
find_res = []
target_suffix_dot = "." + target_suffix
walk_generator = os.walk(target_dir)
for root_path, dirs, files in walk_generator:
if len(files) < 1:
continue
for file in files:
file_name, suffix_name = os.path.splitext(file)
if target_suffix=="all":
find_res.append(os.path.join(root_path, file))
else:
if suffix_name == target_suffix_dot:
find_res.append(os.path.join(root_path, file))
return list(set(find_res))
def read_hex(filename):
with open(filename, 'rb') as f:
file_hex = f.read().hex()
f.close()
return file_hex

rlist=[]
def getfile():
for i in findfile(r"J:\phpstudy_pro\WWW"):
if read_hex(i)[0:6]=="16886f":
rlist.append(i)
getfile()
for i in rlist:
print(i)
os.system("dec.exe %s"%i)

Python

全解开后破解授权验证就很简单了

重新加密

https://windows.php.net/downloads/releases/archives/

转载https://www.leolei.cn/archives/20.html

说明:7.2使用VC15编译,本文以PHP7.0.26举例 使用Visual Studio 2017

需要从 官网 下载 PHP7.0.26 的源代码包和已编译的 PHP 程序,再分别解压,得到两个目录(假设在 C 盘):

C:\php-7.0.26-src

C:\php-7.0.26-nts-Win32-VC14-x64

如何编译

1)拿到要编译的代码,在 VS 2017 菜单中选择「文件」-「新建」-「从现有代码创建项目」,然后在弹出窗口中选择你要编译的代码,类型选择「动态库(dll)」。

2)此时 VS 2017 打开了项目,把工具栏中的「Debug」改为「Release」,「x86」改为「x64」,在菜单中选择「项目」-「属性」。然后在弹出窗口的左侧「常规」栏目下,「配置类型」选择为「动态库(.dll)」:

3)然后在左侧「C/C++」-「常规」栏目下,「附加包含目录」中,加入:

C:\php-7.0.26-src
C:\php-7.0.26-src\main
C:\php-7.0.26-src\TSRM
C:\php-7.0.26-src\Zend

4)接着在左侧「C/C++」-「预处理器」栏目下,「预处理器定义」中,加入:

ZEND_DEBUG=0
PHP_EXTENSION
PHP_WIN32
ZEND_WIN32
HAVE_XXX=1
COMPILE_DL_XXX
ZTS

注意,要把上面的 XXX 改为大写的扩展名 (如扩展叫 tonyenc 就把 XXX 改成 TONYENC),否则 PHP 将无法识别扩展。ZTS用于告诉编译器开启线程安全(如果去掉就是不开启)。注意,线程安全的开启与否,取决于前面下载到的 C:\php-7.0.26-nts-Win32-VC14-x64,它如果启用了线程安全编译,这里就开启线程安全。

5)在左侧「链接器」-「输入」栏目下,「附加依赖项」中,加入:C:\php-7.0.26-nts-Win32-VC14-x64\dev\php7.lib,即前面下载得到的已编译的 PHP 程序。

6)点确定,然后菜单中选择「生成」-「生成解决方案」,恭喜你,编译器报错:无法打开包括文件 ../main/config.w32.h,这时把 C:\php-7.0.26-src\win32\build\config.w32.h.in 复制到 C:\php-7.0.26-src\main\config.w32.h (注意没有了后面的 in),然后在 config.w32.h 中加入:

1
#define PHP_COMPILER_ID "VC14"

Dos

这将指明运行库是 VC14,与前面下载到的已编译 PHP 程序匹配,重新生成下解决方案,这样就能成功编译了!