GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 正式发布 · dogkill/SKRoot-linuxKernelRoot@ef2c38f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • find_kernel_func
    • empty
    • find_cred_has_capability.exe
    • find_do_execve.exe
    • find_setresuid.exe
  • patch_kernel_root
    • ArmAsmHelper.h
    • empty
    • patch_kernel_root.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎find_kernel_func/empty‎

Copy file name to clipboardExpand all lines: find_kernel_func/empty
Whitespace-only changes.
Collapse file

‎find_kernel_func/find_cred_has_capability.exe‎

Copy file name to clipboard
25.5 KB
Binary file not shown.
Collapse file

‎find_kernel_func/find_do_execve.exe‎

Copy file name to clipboard
17.5 KB
Binary file not shown.
Collapse file

‎find_kernel_func/find_setresuid.exe‎

Copy file name to clipboard
121 KB
Binary file not shown.
Collapse file

‎patch_kernel_root/ArmAsmHelper.h‎

Copy file name to clipboard
+93Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#ifndef ARM_ASM_HELPER_H_
2+
#define ARM_ASM_HELPER_H_
3+
#include <string>
4+
#include <windows.h>
5+
#include <fstream>
6+
#include <sstream>
7+
8+
std::string AsmToBytes(const std::string& strArm64Asm) {
9+
//获取汇编文本
10+
11+
//获取自身运行目录
12+
char szFileName[MAX_PATH] = { 0 };
13+
::GetModuleFileNameA(NULL, szFileName, MAX_PATH);
14+
std::string strMyPath = szFileName;
15+
strMyPath = strMyPath.substr(0, strMyPath.find_last_of('\\') + 1);
16+
17+
//写出input.txt
18+
std::ofstream inputFile;
19+
inputFile.open(strMyPath + "input.txt", std::ios_base::out | std::ios_base::trunc);
20+
inputFile << strArm64Asm;
21+
inputFile.close();
22+
23+
//ARM64
24+
DeleteFileA(std::string(strMyPath + "output.txt").c_str());
25+
26+
std::string cmd = strMyPath + "aarch64-linux-android-as.exe -ahlm " + strMyPath + "input.txt >> " + strMyPath + "output.txt";
27+
system(cmd.c_str());
28+
29+
//未开发的
30+
//ARM:arm-linux-as.exe -ahlm -k -mthumb-interwork -march=armv7-a %s >> %s
31+
//Thumb:arm-linux-as.exe -ahlm -k -mthumb-interwork -march=armv7 %s >> %s
32+
33+
//读取output.txt
34+
std::ifstream in(strMyPath + "output.txt");
35+
std::stringstream ssOutput;
36+
std::string line;
37+
bool bIsFirstLine = true;
38+
if (in) // 有该文件
39+
{
40+
while (getline(in, line)) // line中不包括每行的换行符
41+
{
42+
if (bIsFirstLine) {
43+
bIsFirstLine = false;
44+
continue;
45+
}
46+
if (!line.length()) { continue; }
47+
if (line.length() == 1 && line == "\n") { continue; }
48+
if (line.find("AARCH64 GAS") != -1) { continue; }
49+
50+
std::stringstream ssGetMidBuf;
51+
std::string word;
52+
ssGetMidBuf << line;
53+
int n = 0;
54+
while (ssGetMidBuf >> word) {
55+
n++;
56+
if (n == 3) {
57+
ssOutput << word;
58+
}
59+
word.empty();
60+
}
61+
62+
63+
}
64+
in.close();
65+
}
66+
67+
return ssOutput.str();
68+
69+
}
70+
71+
const char HEX[16] = {
72+
'0', '1', '2', '3',
73+
'4', '5', '6', '7',
74+
'8', '9', 'a', 'b',
75+
'c', 'd', 'e', 'f'
76+
};
77+
78+
/* Convert byte array to hex string. */
79+
std::string bytesToHexString(const byte* input, size_t length) {
80+
81+
std::string str;
82+
str.reserve(length << 1);
83+
for (size_t i = 0; i < length; ++i) {
84+
int t = input[i];
85+
int a = t / 16;
86+
int b = t % 16;
87+
str.append(1, HEX[a]);
88+
str.append(1, HEX[b]);
89+
}
90+
return str;
91+
}
92+
93+
#endif /* ARM_ASM_HELPER_H_ */
Collapse file

‎patch_kernel_root/empty‎

Copy file name to clipboardExpand all lines: patch_kernel_root/empty
Whitespace-only changes.
Collapse file

‎patch_kernel_root/patch_kernel_root.cpp‎

Copy file name to clipboard
+273Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
// patch_kernel_root.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
2+
//
3+
4+
#include <iostream>
5+
#include <string>
6+
#include <sstream>
7+
#include <vector>
8+
#include "ArmAsmHelper.h"
9+
10+
11+
using namespace std;
12+
13+
struct patch_bytes_data {
14+
string str_bytes;
15+
size_t write_addr = 0;
16+
};
17+
18+
char* GetFileBuf(const char* lpszFilePath, int& nSize) {
19+
FILE* pFile = fopen(lpszFilePath, "rb");
20+
if (!pFile) {
21+
return NULL;
22+
}
23+
fseek(pFile, 0, SEEK_END);
24+
nSize = ftell(pFile);
25+
rewind(pFile);
26+
char* buffer = (char*)malloc(sizeof(char) * nSize);
27+
if (!buffer) {
28+
return NULL;
29+
}
30+
size_t result = fread(buffer, 1, nSize, pFile);
31+
if ((int)result != nSize) {
32+
free(buffer);
33+
return NULL;
34+
}
35+
fclose(pFile);
36+
//free(buffer);
37+
return buffer;
38+
}
39+
40+
void get_rand_str(char* dest, int n) {
41+
int i, randno;
42+
char stardstring[63] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
43+
srand((unsigned)time(NULL));
44+
for (i = 0; i < n; i++) {
45+
randno = rand() % 62;
46+
*dest = stardstring[randno];
47+
dest++;
48+
}
49+
}
50+
51+
string generate_random_root_key() {
52+
//生成随机密匙
53+
const int key_len = 48;
54+
char root_key_data[key_len] = { 0 };
55+
get_rand_str(root_key_data, sizeof(root_key_data));
56+
string str_root_key(root_key_data, sizeof(root_key_data));
57+
return str_root_key;
58+
}
59+
60+
size_t path_do_execve(const char* file_buf, const string& str_root_key, size_t hook_func_start_addr,
61+
size_t do_execve_entry_addr,
62+
size_t task_struct_offset_cred,
63+
vector<patch_bytes_data>& vec_out_patch_bytes_data) {
64+
65+
size_t do_execve_entry_hook_jump_back_addr = do_execve_entry_addr + 4;
66+
67+
string str_show_root_key_mem_byte = bytesToHexString((const byte*)str_root_key.c_str(), str_root_key.length());
68+
cout << "#生成的ROOT密匙字节集:" << str_show_root_key_mem_byte.c_str() << endl << endl;
69+
vec_out_patch_bytes_data.push_back({ str_show_root_key_mem_byte, hook_func_start_addr });
70+
71+
size_t nHookFuncSize = str_root_key.length();
72+
hook_func_start_addr += nHookFuncSize;
73+
74+
stringstream sstrAsm;
75+
sstrAsm
76+
<< "MOV X0, X0" << endl
77+
<< "STP X7, X8, [sp, #-16]!" << endl
78+
<< "STP X9, X10, [sp, #-16]!" << endl
79+
<< "STP X11, X12, [sp, #-16]!" << endl
80+
<< "MOV X7, 0xFFFFFFFFFFFFF001" << endl
81+
<< "CMP X1, X7" << endl
82+
<< "BCS #112" << endl
83+
<< "LDR X7, [X1]" << endl
84+
<< "CBZ X7, #104" << endl
85+
<< "ADR X8, #-84" << endl
86+
<< "MOV X9, #0" << endl
87+
<< "LDRB W10, [X7, X9]" << endl
88+
<< "CBZ W10, #88" << endl
89+
<< "LDRB W11, [X8, X9]" << endl
90+
<< "CBZ W11, #80" << endl
91+
<< "CMP W10, W11" << endl
92+
<< "B.NE #72" << endl
93+
<< "ADD X9, X9, 1" << endl
94+
<< "CMP X9, #" << str_root_key.length() << endl
95+
<< "BLT #-32" << endl
96+
<< "MRS X8, SP_EL0" << endl
97+
<< "LDR X8, [X8, #" << task_struct_offset_cred << "]" << endl
98+
<< "MOV X7, #4" << endl
99+
<< "MOV W9, WZR" << endl
100+
<< "STR W9, [X8, X7]" << endl
101+
<< "ADD X7, X7, 4" << endl
102+
<< "CMP X7, #36" << endl
103+
<< "BLT #-12" << endl
104+
<< "ADD X7, X7, 4" << endl
105+
<< "MOV X9, 0xFFFFFFFFFFFFFFFF" << endl
106+
<< "STR X9, [X8, X7]" << endl
107+
<< "ADD X7, X7, 8" << endl
108+
<< "CMP X7, #80" << endl
109+
<< "BLT #-12" << endl
110+
<< "LDP X11, X12, [sp], #16" << endl
111+
<< "LDP X9, X10, [sp], #16" << endl
112+
<< "LDP X7, X8, [sp], #16" << endl
113+
<< "B #" << do_execve_entry_hook_jump_back_addr - (hook_func_start_addr + 0x94) << endl;
114+
string strAsmCode = sstrAsm.str();
115+
cout << endl << strAsmCode << endl;
116+
string strBytes = AsmToBytes(strAsmCode);
117+
nHookFuncSize = strBytes.length() / 2;
118+
char hookOrigCmd[4] = { 0 };
119+
memcpy(&hookOrigCmd, (void*)((size_t)file_buf + do_execve_entry_addr), sizeof(hookOrigCmd));
120+
string strHookOrigCmd = bytesToHexString((const byte*)hookOrigCmd, sizeof(hookOrigCmd));
121+
strBytes = strHookOrigCmd + strBytes.substr(0x4 * 2);
122+
vec_out_patch_bytes_data.push_back({ strBytes, hook_func_start_addr });
123+
stringstream sstrAsm2;
124+
sstrAsm2 << "B #" << hook_func_start_addr - do_execve_entry_addr << endl;
125+
string strBytes2 = AsmToBytes(sstrAsm2.str());
126+
vec_out_patch_bytes_data.push_back({ strBytes2, do_execve_entry_addr });
127+
hook_func_start_addr += nHookFuncSize;
128+
cout << "#下一段HOOK函数起始可写位置:" << hex << hook_func_start_addr << endl << endl;
129+
return hook_func_start_addr;
130+
}
131+
132+
size_t path_avc_denied(const char* file_buf, size_t hook_func_start_addr, size_t avc_denied_entry_addr,
133+
size_t task_struct_offset_cred, vector<patch_bytes_data> & vec_out_patch_bytes_data) {
134+
size_t avc_denied_entry_hook_jump_back_addr = avc_denied_entry_addr + 4;
135+
stringstream sstrAsm;
136+
sstrAsm
137+
<< "STP X7, X8, [sp, #-16]!" << endl
138+
<< "STP X9, X10, [sp, #-16]!" << endl
139+
<< "MRS X7, SP_EL0" << endl
140+
<< "LDR X7, [X7, #" << task_struct_offset_cred << "]" << endl
141+
<< "CBZ X7, #84" << endl
142+
<< "MOV X8, #4" << endl
143+
<< "MOV W9, WZR" << endl
144+
<< "LDR W10, [X7, X8]" << endl
145+
<< "CMP W10, W9" << endl
146+
<< "B.NE #64" << endl
147+
<< "ADD X8, X8, 4" << endl
148+
<< "CMP X8, #36" << endl
149+
<< "BLT #-20" << endl
150+
<< "ADD X8, X8, 12" << endl
151+
<< "MOV X9, 0x3FFFFFFFFF" << endl
152+
<< "LDR X10, [X7, X8]" << endl
153+
<< "ADD X8, X8, 8" << endl
154+
<< "CMP X10, X9" << endl
155+
<< "B.CC #28" << endl
156+
<< "CMP X8, #72" << endl
157+
<< "BLT #-20" << endl
158+
<< "LDP X9, X10, [sp], #16" << endl
159+
<< "LDP X7, X8, [sp], #16" << endl
160+
<< "MOV W0, WZR" << endl
161+
<< "RET" << endl
162+
<< "LDP X9, X10, [sp], #16" << endl
163+
<< "LDP X7, X8, [sp], #16" << endl
164+
<< "MOV X0, X0" << endl
165+
<< "B #" << avc_denied_entry_hook_jump_back_addr - (hook_func_start_addr + 0x70) << endl;
166+
string strAsmCode = sstrAsm.str();
167+
cout << endl << strAsmCode << endl;
168+
string strBytes = AsmToBytes(strAsmCode);
169+
size_t nHookFuncSize = strBytes.length() / 2;
170+
char hookOrigCmd[4] = { 0 };
171+
memcpy(&hookOrigCmd, (void*)((size_t)file_buf + avc_denied_entry_addr), sizeof(hookOrigCmd));
172+
string strHookOrigCmd = bytesToHexString((const byte*)hookOrigCmd, sizeof(hookOrigCmd));
173+
strBytes = strBytes.substr(0, (0x6C) * 2) + strHookOrigCmd + strBytes.substr((0x6C + 4) * 2);
174+
vec_out_patch_bytes_data.push_back({ strBytes, hook_func_start_addr });
175+
stringstream sstrAsm2;
176+
sstrAsm2 << "B #" << hook_func_start_addr - avc_denied_entry_addr << endl;
177+
string strBytes2 = AsmToBytes(sstrAsm2.str());
178+
vec_out_patch_bytes_data.push_back({ strBytes2, avc_denied_entry_addr });
179+
return hook_func_start_addr + nHookFuncSize;
180+
}
181+
182+
static auto hex2byte(uint8_t* hex, uint8_t* str) -> void {
183+
char high, low;
184+
for (int i = 0, length = strlen((char*)hex); i < length; i += 2) {
185+
high = toupper(hex[i]) - '0';
186+
low = toupper(hex[i + 1]) - '0';
187+
str[i / 2] = ((high > 9 ? high - 7 : high) << 4) + (low > 9 ? low - 7 : low);
188+
}
189+
}
190+
191+
bool write_file_bytes(const char* file_path, long offset, const char* bytes, int len) {
192+
FILE* pFile = fopen(file_path, "rb+");
193+
if (!pFile) {
194+
return false;
195+
}
196+
fseek(pFile, offset, SEEK_SET);
197+
fwrite(bytes, len, 1, pFile);
198+
fclose(pFile);
199+
return true;
200+
}
201+
202+
int main(int argc, char* argv[]) {
203+
++argv;
204+
--argc;
205+
206+
cout << "本工具用于生成aarch64 Linux内核ROOT提权HOOK代码" << endl << endl;
207+
208+
if (argc < 1) {
209+
cout << "无输入文件" << endl;
210+
system("pause");
211+
return 0;
212+
}
213+
214+
int nFileSize = 0;
215+
const char* file_path = argv[0];
216+
char* image = GetFileBuf(file_path, nFileSize);
217+
if (!image) {
218+
cout << "打开文件失败:" << file_path << endl;
219+
system("pause");
220+
return 0;
221+
}
222+
223+
//cout << "请输入空闲代码的位置:(即存放执行HOOK代码的位置):" << endl;
224+
size_t hook_func_start_addr = 0x300;
225+
//cin >> hex >> hook_func_start_addr;
226+
227+
size_t do_execve_entry_addr = 0;
228+
cout << "请输入do_execve函数的入口位置:" << endl;
229+
cin >> hex >> do_execve_entry_addr;
230+
231+
size_t avc_denied_entry_addr = 0;
232+
cout << "请输入avc_denied函数的入口位置:" << endl;
233+
cin >> hex >> avc_denied_entry_addr;
234+
235+
size_t task_struct_offset_cred = -1;
236+
while (task_struct_offset_cred % 8) {
237+
cout << "请输入task_struct结构体里cred的十六进制偏移值(从setresuid的头部能看到):" << endl;
238+
cin >> hex >> task_struct_offset_cred;
239+
if (task_struct_offset_cred % 8) {
240+
cout << "信息输入有误" << endl;
241+
}
242+
}
243+
244+
string str_root_key;
245+
size_t create_new_root_key = 0;
246+
cout << "是否需要自动随机生成ROOT密匙(1需要;2不需要):" << endl;
247+
cin >> hex >> create_new_root_key;
248+
if (create_new_root_key == 1) {
249+
str_root_key = generate_random_root_key();
250+
} else {
251+
cout << "请输入ROOT密匙(48个字符的字符串,包含大小写和数字):" << endl;
252+
cin >> str_root_key;
253+
}
254+
vector<patch_bytes_data> vec_patch_bytes_data;
255+
hook_func_start_addr = path_do_execve(image, str_root_key, hook_func_start_addr, do_execve_entry_addr, task_struct_offset_cred, vec_patch_bytes_data);
256+
path_avc_denied(image, hook_func_start_addr, avc_denied_entry_addr, task_struct_offset_cred, vec_patch_bytes_data);
257+
258+
cout << "#获取ROOT权限的密匙:" << str_root_key.c_str() << endl << endl;
259+
260+
size_t need_write_modify_in_file = 0;
261+
cout << "#是否需要立即写入修改到文件?(1需要;2不需要):" << endl;
262+
cin >> need_write_modify_in_file;
263+
if (need_write_modify_in_file == 1) {
264+
for (auto& item : vec_patch_bytes_data) {
265+
shared_ptr<char> spData(new (std::nothrow) char[item.str_bytes.length() / 2], std::default_delete<char[]>());
266+
hex2byte((uint8_t*)item.str_bytes.c_str(), (uint8_t*)spData.get());
267+
write_file_bytes(file_path, item.write_addr, spData.get(), item.str_bytes.length() / 2);
268+
}
269+
}
270+
free(image);
271+
system("pause");
272+
return 0;
273+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.