GeezerVison is showing github.com — fix: 优化写法 · Corner514/SKRoot-linuxKernelRoot@94d8541
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: patch_kernel_root/analyze/ARM_asm.h +40-19Lines changed: 40 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 5 | 5 | #include <fstream> | 6 | 6 | #include <sstream> | 7 | 7 | #include <filesystem> | | 8 | +#include "base_func.h" | 8 | 9 |
| 9 | 10 | static std::string AsmToBytes(const std::string& strArm64Asm) { | 10 | 11 | //获取汇编文本 | @@ -79,26 +80,46 @@ static std::string AsmToBytes(const std::string& strArm64Asm) { | 79 | 80 |
| 80 | 81 | } | 81 | 82 |
| 82 | | -static const char HEX[16] = { | 83 | | -'0', '1', '2', '3', | 84 | | -'4', '5', '6', '7', | 85 | | -'8', '9', 'a', 'b', | 86 | | -'c', 'd', 'e', 'f' | 87 | | -}; | 88 | | - | 89 | | -/* Convert byte array to hex string. */ | 90 | | -static std::string bytes_2_hex_str(const unsigned char* input, size_t length) { | 91 | | - | 92 | | - std::string str; | 93 | | - str.reserve(length << 1); | 94 | | - for (size_t i = 0; i < length; ++i) { | 95 | | - int t = input[i]; | 96 | | - int a = t / 16; | 97 | | - int b = t % 16; | 98 | | - str.append(1, HEX[a]); | 99 | | - str.append(1, HEX[b]); | | 83 | +static std::string AsmLabelToOffset(const std::string& asm_code, const char* end_label_name, const char* jump_label_name) { | | 84 | + // 得到结尾位置 | | 85 | + std::string s = asm_code; | | 86 | + size_t n = s.find(end_label_name); | | 87 | + if (n == -1) { | | 88 | + return s; | 100 | 89 | } | 101 | | - return str; | | 90 | + std::string before = s.substr(0, n); | | 91 | + size_t end_back_idx_line = count_endl(before); | | 92 | + replace_all_distinct(s, end_label_name, ""); | | 93 | + | | 94 | + // 逐行切割 | | 95 | + std::vector<std::string> lines; | | 96 | + { | | 97 | + std::istringstream iss(s); | | 98 | + std::string line; | | 99 | + while (std::getline(iss, line)) { | | 100 | + if (!line.empty() && line.back() == '\r') | | 101 | + line.pop_back(); | | 102 | + lines.push_back(line); | | 103 | + } | | 104 | + } | | 105 | + | | 106 | + // 替换每一行中的 #JUMP_END | | 107 | + const std::string placeholder = jump_label_name; | | 108 | + for (size_t idx = 0; idx < lines.size(); ++idx) { | | 109 | + auto p = lines[idx].find(placeholder); | | 110 | + if (p != std::string::npos) { | | 111 | + int imm = (end_back_idx_line - idx) * 4; | | 112 | + lines[idx].replace(p, placeholder.size(), std::to_string(imm)); | | 113 | + } | | 114 | + } | | 115 | + | | 116 | + // 拼回去 | | 117 | + std::string out; | | 118 | + for (size_t i = 0; i < lines.size(); ++i) { | | 119 | + out += lines[i]; | | 120 | + out += "\n"; | | 121 | + } | | 122 | + return out; | 102 | 123 | } | 103 | 124 |
| 104 | 125 | #endif /* ARM_ASM_HELPER_H_ */ |
Collapse file Copy file name to clipboardExpand all lines: patch_kernel_root/analyze/base_func.h +36-1Lines changed: 36 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | #include <vector> | 8 | 8 | #include <math.h> | 9 | 9 |
| | 10 | +static const char HEX[16] = { | | 11 | +'0', '1', '2', '3', | | 12 | +'4', '5', '6', '7', | | 13 | +'8', '9', 'a', 'b', | | 14 | +'c', 'd', 'e', 'f' | | 15 | +}; | | 16 | + | 10 | 17 | static std::vector<char> read_file_buf(const std::string& file_path) { | 11 | 18 | std::ifstream file(file_path, std::ios::binary | std::ios::ate); | 12 | 19 | if (file) { | @@ -39,14 +46,27 @@ static std::string generate_random_root_key() { | 39 | 46 | return str_root_key; | 40 | 47 | } | 41 | 48 |
| 42 | | -static auto hex2byte(uint8_t* hex, uint8_t* str) -> void { | | 49 | +static auto hex2bytes(uint8_t* hex, uint8_t* str) -> void { | 43 | 50 | char high, low; | 44 | 51 | for (size_t i = 0, length = strlen((char*)hex); i < length; i += 2) { | 45 | 52 | high = toupper(hex[i]) - '0'; | 46 | 53 | low = toupper(hex[i + 1]) - '0'; | 47 | 54 | str[i / 2] = ((high > 9 ? high - 7 : high) << 4) + (low > 9 ? low - 7 : low); | 48 | 55 | } | 49 | 56 | } | | 57 | +static std::string bytes2hex(const unsigned char* input, size_t length) { | | 58 | + | | 59 | + std::string str; | | 60 | + str.reserve(length << 1); | | 61 | + for (size_t i = 0; i < length; ++i) { | | 62 | + int t = input[i]; | | 63 | + int a = t / 16; | | 64 | + int b = t % 16; | | 65 | + str.append(1, HEX[a]); | | 66 | + str.append(1, HEX[b]); | | 67 | + } | | 68 | + return str; | | 69 | +} | 50 | 70 |
| 51 | 71 | static bool write_file_bytes(const char* file_path, size_t offset, const char* bytes, size_t len) { | 52 | 72 | std::fstream file_stream(file_path, std::ios::in | std::ios::out | std::ios::binary); | @@ -73,3 +93,18 @@ static size_t align8(size_t addr) { | 73 | 93 | } | 74 | 94 | return addr; | 75 | 95 | } | | 96 | + | | 97 | +static size_t count_endl(const std::string& s) { | | 98 | + return std::count(s.begin(), s.end(), '\n'); | | 99 | +} | | 100 | + | | 101 | +static void replace_all_distinct(std::string& str, const std::string& old_value, const std::string& new_value) { | | 102 | + for (std::string::size_type pos(0); pos != std::string::npos; pos += new_value.length()) { | | 103 | + if ((pos = str.find(old_value, pos)) != std::string::npos) { | | 104 | + str.replace(pos, old_value.length(), new_value); | | 105 | + } | | 106 | + else { | | 107 | + break; | | 108 | + } | | 109 | + } | | 110 | +} |
Collapse file Copy file name to clipboard Collapse file Copy file name to clipboardExpand all lines: patch_kernel_root/patch_avc_denied.cpp +25-18Lines changed: 25 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #include "patch_avc_denied.h" | | 2 | +#include "analyze/base_func.h" | 2 | 3 | #include "analyze/ARM_asm.h" | 3 | 4 | PatchAvcDenied::PatchAvcDenied(const std::vector<char>& file_buf, const KernelSymbolOffset& sym, | 4 | 5 | const SymbolAnalyze& symbol_analyze) : PatchBase(file_buf, sym, symbol_analyze) { | @@ -27,7 +28,6 @@ size_t PatchAvcDenied::patch_avc_denied(size_t hook_func_start_addr, const std:: | 27 | 28 | int cap_cnt = get_need_read_cap_cnt(); | 28 | 29 |
| 29 | 30 | size_t avc_denied_entry_hook_jump_back_addr = avc_denied_addr + 4; | 30 | | - auto next_asm_line_bytes_cnt = (task_struct_offset_cred.size() - 1) * 4; | 31 | 31 | std::stringstream sstrAsm; | 32 | 32 | sstrAsm | 33 | 33 | << "STP X7, X8, [sp, #-16]!" << std::endl | @@ -38,36 +38,41 @@ size_t PatchAvcDenied::patch_avc_denied(size_t hook_func_start_addr, const std:: | 38 | 38 | sstrAsm << "LDR X7, [X7, #" << task_struct_offset_cred[x] << "]" << std::endl; | 39 | 39 | } | 40 | 40 | } | 41 | | - | 42 | 41 | sstrAsm << "LDR X7, [X7, #" << task_struct_offset_cred[task_struct_offset_cred.size() - 1] << "]" << std::endl | 43 | | - << "CBZ X7, #" << 88 << std::endl | 44 | | - << "ADD X7, X7, #"<< atomic_usage_len << std::endl | | 42 | + << "CBZ X7, #JUMP_END" << std::endl | | 43 | + << "ADD X7, X7, #" << atomic_usage_len << std::endl | 45 | 44 | << "MOV X8, #8" << std::endl | | 45 | + << "LABEL_CYCLE_UID:" | 46 | 46 | << "LDR W9, [X7], #4" << std::endl | 47 | | - << "CBNZ W9, #" << 72 << std::endl | | 47 | + << "CBNZ W9, #JUMP_END" << std::endl | 48 | 48 | << "SUBS X8, X8, #1" << std::endl | 49 | | - << "B.NE #-" << 12 << std::endl | | 49 | + << "B.NE #JUMP_CYCLE_UID" << std::endl | 50 | 50 | << "MOV W8, 0xC" << std::endl | 51 | | - << "LDR W9, [X7], #"<< 4 + securebits_padding << std::endl | | 51 | + << "LDR W9, [X7], #" << 4 + securebits_padding << std::endl | 52 | 52 | << "CMP W8, W9" << std::endl | 53 | | - << "B.NE #" << 48 << std::endl | 54 | | - << "MOV X8, "<< cap_ability_max << std::endl | 55 | | - << "MOV X9, #"<< cap_cnt << std::endl | | 53 | + << "B.NE #JUMP_END" << std::endl | | 54 | + << "MOV X8, " << cap_ability_max << std::endl | | 55 | + << "MOV X9, #" << cap_cnt << std::endl | | 56 | + << "LABEL_CYCLE_CAP:" | 56 | 57 | << "LDR X10, [X7], #8" << std::endl | 57 | 58 | << "CMP X10, X8" << std::endl | 58 | | - << "B.CC #" << 28 << std::endl | | 59 | + << "B.CC #JUMP_END" << std::endl | 59 | 60 | << "SUBS X9, X9, #1" << std::endl | 60 | | - << "B.NE #-16" << std::endl | | 61 | + << "B.NE #JUMP_CYCLE_CAP" << std::endl | 61 | 62 | << "LDP X9, X10, [sp], #16" << std::endl | 62 | 63 | << "LDP X7, X8, [sp], #16" << std::endl | 63 | 64 | << "MOV W0, WZR" << std::endl | 64 | 65 | << "RET" << std::endl | | 66 | + << "LABEL_END:" | 65 | 67 | << "LDP X9, X10, [sp], #16" << std::endl | 66 | 68 | << "LDP X7, X8, [sp], #16" << std::endl | 67 | | - << "MOV X0, X0" << std::endl | 68 | | - << "B #" << avc_denied_entry_hook_jump_back_addr - (hook_func_start_addr + 0x74 + next_asm_line_bytes_cnt) << std::endl; | | 69 | + << "MOV X0, X0" << std::endl; | | 70 | + size_t end_order_len = count_endl(sstrAsm.str()) * 4; | | 71 | + sstrAsm<< "B #" << (int64_t)(avc_denied_entry_hook_jump_back_addr - (hook_func_start_addr + end_order_len)) << std::endl; | 69 | 72 |
| 70 | | - std::string strAsmCode = sstrAsm.str(); | | 73 | + std::string strAsmCode = AsmLabelToOffset(sstrAsm.str(), "LABEL_END:", "JUMP_END"); | | 74 | + strAsmCode = AsmLabelToOffset(strAsmCode, "LABEL_CYCLE_UID:", "JUMP_CYCLE_UID"); | | 75 | + strAsmCode = AsmLabelToOffset(strAsmCode, "LABEL_CYCLE_CAP:", "JUMP_CYCLE_CAP"); | 71 | 76 | std::cout << std::endl << strAsmCode << std::endl; | 72 | 77 |
| 73 | 78 | std::string strBytes = AsmToBytes(strAsmCode); | @@ -78,13 +83,15 @@ size_t PatchAvcDenied::patch_avc_denied(size_t hook_func_start_addr, const std:: | 78 | 83 |
| 79 | 84 | char hookOrigCmd[4] = { 0 }; | 80 | 85 | memcpy(&hookOrigCmd, (void*)((size_t)&m_file_buf[0] + avc_denied_addr), sizeof(hookOrigCmd)); | 81 | | - std::string strHookOrigCmd = bytes_2_hex_str((const unsigned char*)hookOrigCmd, sizeof(hookOrigCmd)); | 82 | | - strBytes = strBytes.substr(0, (0x70 + next_asm_line_bytes_cnt) * 2) + strHookOrigCmd + strBytes.substr((0x70 + next_asm_line_bytes_cnt + 4) * 2); | | 86 | + std::string strHookOrigCmd = bytes2hex((const unsigned char*)hookOrigCmd, sizeof(hookOrigCmd)); | | 87 | + | | 88 | + end_order_len = (count_endl(sstrAsm.str()) - 2) * 4; | | 89 | + strBytes = strBytes.substr(0, (end_order_len) * 2) + strHookOrigCmd + strBytes.substr((end_order_len + 4) * 2); | 83 | 90 |
| 84 | 91 | vec_out_patch_bytes_data.push_back({ strBytes, hook_func_start_addr }); | 85 | 92 | std::stringstream sstrAsm2; | 86 | 93 | sstrAsm2 | 87 | | - << "B #" << hook_func_start_addr - avc_denied_addr << std::endl; | | 94 | + << "B #" << (int64_t)(hook_func_start_addr - avc_denied_addr) << std::endl; | 88 | 95 | std::string strBytes2 = AsmToBytes(sstrAsm2.str()); | 89 | 96 | if (!strBytes2.length()) { | 90 | 97 | return 0; | |
Collapse file Copy file name to clipboardExpand all lines: patch_kernel_root/patch_do_execve.cpp +19-19Lines changed: 19 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #pragma once | 2 | 2 | #include "patch_do_execve.h" | | 3 | +#include "analyze/base_func.h" | 3 | 4 | #include "analyze/ARM_asm.h" | 4 | 5 | PatchDoExecve::PatchDoExecve(const std::vector<char>& file_buf, const KernelSymbolOffset& sym, | 5 | 6 | const SymbolAnalyze& symbol_analyze) : PatchBase(file_buf, sym, symbol_analyze) { | @@ -56,15 +57,14 @@ size_t PatchDoExecve::patch_do_execve(const std::string& str_root_key, size_t ho | 56 | 57 |
| 57 | 58 | size_t do_execve_entry_hook_jump_back_addr = do_execve_addr + 4; | 58 | 59 |
| 59 | | - std::string str_show_root_key_mem_byte = bytes_2_hex_str((const unsigned char*)str_root_key.c_str(), str_root_key.length()); | | 60 | + std::string str_show_root_key_mem_byte = bytes2hex((const unsigned char*)str_root_key.c_str(), str_root_key.length()); | 60 | 61 | std::cout << "#生成的ROOT密匙字节集:" << str_show_root_key_mem_byte.c_str() << std::endl << std::endl; | 61 | 62 |
| 62 | 63 | vec_out_patch_bytes_data.push_back({ str_show_root_key_mem_byte, hook_func_start_addr }); | 63 | 64 |
| 64 | 65 | size_t nHookFuncSize = str_root_key.length(); | 65 | 66 | hook_func_start_addr += nHookFuncSize; | 66 | 67 |
| 67 | | - auto next_asm_line_bytes_cnt = (task_struct_offset_cred.size() - 1) * 4; | 68 | 68 | std::stringstream sstrAsm; | 69 | 69 |
| 70 | 70 | sstrAsm | @@ -74,20 +74,21 @@ size_t PatchDoExecve::patch_do_execve(const std::string& str_root_key, size_t ho | 74 | 74 | << "STP X11, X12, [sp, #-16]!" << std::endl | 75 | 75 | << "MOV X7, 0xFFFFFFFFFFFFF001" << std::endl | 76 | 76 | << "CMP X" << do_execve_key_reg << ", X7" << std::endl | 77 | | - << "BCS #" << 128 + next_asm_line_bytes_cnt << std::endl | | 77 | + << "BCS #JUMP_END" << std::endl | 78 | 78 | << "LDR X7, [X" << do_execve_key_reg << "]" << std::endl | 79 | | - << "CBZ X7, #" << 120 + next_asm_line_bytes_cnt << std::endl | 80 | | - << "ADR X8, #-84" << std::endl | | 79 | + << "CBZ X7, #JUMP_END" << std::endl; | | 80 | + size_t end_order_cnt = count_endl(sstrAsm.str()); | | 81 | + int key_offset = (hook_func_start_addr - 48) - (hook_func_start_addr + end_order_cnt * 4); | | 82 | + sstrAsm << "ADR X8, #"<< key_offset << std::endl | 81 | 83 | << "MOV X9, #0" << std::endl | | 84 | + << "LABEL_CYCLE_NAME:" | 82 | 85 | << "LDRB W10, [X7, X9]" << std::endl | 83 | | - << "CBZ W10, #" << 104 + next_asm_line_bytes_cnt << std::endl | 84 | 86 | << "LDRB W11, [X8, X9]" << std::endl | 85 | | - << "CBZ W11, #" << 96 + next_asm_line_bytes_cnt << std::endl | 86 | 87 | << "CMP W10, W11" << std::endl | 87 | | - << "B.NE #" << 88 + next_asm_line_bytes_cnt << std::endl | | 88 | + << "B.NE #JUMP_END" << std::endl | 88 | 89 | << "ADD X9, X9, 1" << std::endl | 89 | 90 | << "CMP X9, #" << str_root_key.length() << std::endl | 90 | | - << "BLT #-32" << std::endl; | | 91 | + << "BLT #JUMP_CYCLE_NAME" << std::endl; | 91 | 92 | sstrAsm << "MRS X8, SP_EL0" << std::endl; | 92 | 93 | for (auto x = 0; x < task_struct_offset_cred.size(); x++) { | 93 | 94 | if (x != task_struct_offset_cred.size() - 1) { | @@ -102,27 +103,26 @@ size_t PatchDoExecve::patch_do_execve(const std::string& str_root_key, size_t ho | 102 | 103 | << "STR XZR, [X10], #8" << std::endl | 103 | 104 | << "MOV W9, 0xC" << std::endl | 104 | 105 | << "STR W9, [X10], #"<< 4 + securebits_padding << std::endl | 105 | | - << "MOV X9, "<< cap_ability_max << std::endl | | 106 | + << "MOV X9, "<< cap_ability_max << std::endl | 106 | 107 | << "STP X9, X9, [X10], #16" << std::endl | 107 | 108 | << "STP X9, X9, [X10], #16" << std::endl; | 108 | 109 | if (cap_cnt == 5) { | 109 | 110 | sstrAsm << "STR X9, [X10], #8" << std::endl; | 110 | | - } else { | 111 | | - sstrAsm << "MOV X1, X1" << std::endl; | 112 | 111 | } | 113 | 112 | sstrAsm << "LDXR W10, [X8]" << std::endl | 114 | 113 | << "BIC W10, W10,#0xFFF" << std::endl | 115 | 114 | << "STXR W11, W10, [X8]" << std::endl | 116 | 115 | << "STR WZR, [X8, #" << task_struct_offset_seccomp[task_struct_offset_seccomp.size() - 1] << "]" << std::endl | 117 | 116 | << "STR XZR, [X8, #" << task_struct_offset_seccomp[task_struct_offset_seccomp.size() - 1] + 8 << "]" << std::endl | | 117 | + << "LABEL_END:" | 118 | 118 | << "LDP X11, X12, [sp], #16" << std::endl | 119 | 119 | << "LDP X9, X10, [sp], #16" << std::endl | 120 | | - << "LDP X7, X8, [sp], #16" << std::endl | 121 | | - << "B #" << do_execve_entry_hook_jump_back_addr - (hook_func_start_addr + 0xA4 + next_asm_line_bytes_cnt) << std::endl; | | 120 | + << "LDP X7, X8, [sp], #16" << std::endl; | | 121 | + size_t end_order_len = count_endl(sstrAsm.str()) * 4; | | 122 | + sstrAsm << "B #" << (int64_t)(do_execve_entry_hook_jump_back_addr - (hook_func_start_addr + end_order_len)) << std::endl; | 122 | 123 |
| 123 | | - | 124 | | - | 125 | | - std::string strAsmCode = sstrAsm.str(); | | 124 | + std::string strAsmCode = AsmLabelToOffset(sstrAsm.str(), "LABEL_END:", "JUMP_END"); | | 125 | + strAsmCode = AsmLabelToOffset(strAsmCode, "LABEL_CYCLE_NAME:", "JUMP_CYCLE_NAME"); | 126 | 126 | std::cout << std::endl << strAsmCode << std::endl; | 127 | 127 |
| 128 | 128 | std::string strBytes = AsmToBytes(strAsmCode); | @@ -134,14 +134,14 @@ size_t PatchDoExecve::patch_do_execve(const std::string& str_root_key, size_t ho | 134 | 134 |
| 135 | 135 | char hookOrigCmd[4] = { 0 }; | 136 | 136 | memcpy(&hookOrigCmd, (void*)((size_t)&m_file_buf[0] + do_execve_addr), sizeof(hookOrigCmd)); | 137 | | - std::string strHookOrigCmd = bytes_2_hex_str((const unsigned char*)hookOrigCmd, sizeof(hookOrigCmd)); | | 137 | + std::string strHookOrigCmd = bytes2hex((const unsigned char*)hookOrigCmd, sizeof(hookOrigCmd)); | 138 | 138 | strBytes = strHookOrigCmd + strBytes.substr(0x4 * 2); | 139 | 139 |
| 140 | 140 | vec_out_patch_bytes_data.push_back({ strBytes, hook_func_start_addr }); | 141 | 141 |
| 142 | 142 | std::stringstream sstrAsm2; | 143 | 143 | sstrAsm2 | 144 | | - << "B #" << hook_func_start_addr - do_execve_addr << std::endl; | | 144 | + << "B #" << (int64_t)(hook_func_start_addr - do_execve_addr) << std::endl; | 145 | 145 | std::string strBytes2 = AsmToBytes(sstrAsm2.str()); | 146 | 146 | if (!strBytes2.length()) { | 147 | 147 | return 0; | |
|