|
| 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 | +} |