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 — fix: 优化写法 · Corner514/SKRoot-linuxKernelRoot@94d8541

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • patch_kernel_root
    • analyze
      • ARM_asm.h
      • base_func.h
    • exe
      • patch_kernel_root.exe
    • patch_avc_denied.cpp
    • patch_do_execve.cpp
    • patch_filldir64.cpp
    • patch_freeze_task.cpp
    • patch_kernel_root.cpp
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎patch_kernel_root/analyze/ARM_asm.h‎

Copy file name to clipboardExpand all lines: patch_kernel_root/analyze/ARM_asm.h
+40-19Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <fstream>
66
#include <sstream>
77
#include <filesystem>
8+
#include "base_func.h"
89

910
static std::string AsmToBytes(const std::string& strArm64Asm) {
1011
//获取汇编文本
@@ -79,26 +80,46 @@ static std::string AsmToBytes(const std::string& strArm64Asm) {
7980

8081
}
8182

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;
10089
}
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;
102123
}
103124

104125
#endif /* ARM_ASM_HELPER_H_ */
Collapse file

‎patch_kernel_root/analyze/base_func.h‎

Copy file name to clipboardExpand all lines: patch_kernel_root/analyze/base_func.h
+36-1Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
#include <vector>
88
#include <math.h>
99

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+
1017
static std::vector<char> read_file_buf(const std::string& file_path) {
1118
std::ifstream file(file_path, std::ios::binary | std::ios::ate);
1219
if (file) {
@@ -39,14 +46,27 @@ static std::string generate_random_root_key() {
3946
return str_root_key;
4047
}
4148

42-
static auto hex2byte(uint8_t* hex, uint8_t* str) -> void {
49+
static auto hex2bytes(uint8_t* hex, uint8_t* str) -> void {
4350
char high, low;
4451
for (size_t i = 0, length = strlen((char*)hex); i < length; i += 2) {
4552
high = toupper(hex[i]) - '0';
4653
low = toupper(hex[i + 1]) - '0';
4754
str[i / 2] = ((high > 9 ? high - 7 : high) << 4) + (low > 9 ? low - 7 : low);
4855
}
4956
}
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+
}
5070

5171
static bool write_file_bytes(const char* file_path, size_t offset, const char* bytes, size_t len) {
5272
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) {
7393
}
7494
return addr;
7595
}
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

‎patch_kernel_root/exe/patch_kernel_root.exe‎

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

‎patch_kernel_root/patch_avc_denied.cpp‎

Copy file name to clipboardExpand all lines: patch_kernel_root/patch_avc_denied.cpp
+25-18Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "patch_avc_denied.h"
2+
#include "analyze/base_func.h"
23
#include "analyze/ARM_asm.h"
34
PatchAvcDenied::PatchAvcDenied(const std::vector<char>& file_buf, const KernelSymbolOffset& sym,
45
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::
2728
int cap_cnt = get_need_read_cap_cnt();
2829

2930
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;
3131
std::stringstream sstrAsm;
3232
sstrAsm
3333
<< "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::
3838
sstrAsm << "LDR X7, [X7, #" << task_struct_offset_cred[x] << "]" << std::endl;
3939
}
4040
}
41-
4241
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
4544
<< "MOV X8, #8" << std::endl
45+
<< "LABEL_CYCLE_UID:"
4646
<< "LDR W9, [X7], #4" << std::endl
47-
<< "CBNZ W9, #" << 72 << std::endl
47+
<< "CBNZ W9, #JUMP_END" << std::endl
4848
<< "SUBS X8, X8, #1" << std::endl
49-
<< "B.NE #-" << 12 << std::endl
49+
<< "B.NE #JUMP_CYCLE_UID" << std::endl
5050
<< "MOV W8, 0xC" << std::endl
51-
<< "LDR W9, [X7], #"<< 4 + securebits_padding << std::endl
51+
<< "LDR W9, [X7], #" << 4 + securebits_padding << std::endl
5252
<< "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:"
5657
<< "LDR X10, [X7], #8" << std::endl
5758
<< "CMP X10, X8" << std::endl
58-
<< "B.CC #" << 28 << std::endl
59+
<< "B.CC #JUMP_END" << std::endl
5960
<< "SUBS X9, X9, #1" << std::endl
60-
<< "B.NE #-16" << std::endl
61+
<< "B.NE #JUMP_CYCLE_CAP" << std::endl
6162
<< "LDP X9, X10, [sp], #16" << std::endl
6263
<< "LDP X7, X8, [sp], #16" << std::endl
6364
<< "MOV W0, WZR" << std::endl
6465
<< "RET" << std::endl
66+
<< "LABEL_END:"
6567
<< "LDP X9, X10, [sp], #16" << std::endl
6668
<< "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;
6972

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");
7176
std::cout << std::endl << strAsmCode << std::endl;
7277

7378
std::string strBytes = AsmToBytes(strAsmCode);
@@ -78,13 +83,15 @@ size_t PatchAvcDenied::patch_avc_denied(size_t hook_func_start_addr, const std::
7883

7984
char hookOrigCmd[4] = { 0 };
8085
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);
8390

8491
vec_out_patch_bytes_data.push_back({ strBytes, hook_func_start_addr });
8592
std::stringstream sstrAsm2;
8693
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;
8895
std::string strBytes2 = AsmToBytes(sstrAsm2.str());
8996
if (!strBytes2.length()) {
9097
return 0;
Collapse file

‎patch_kernel_root/patch_do_execve.cpp‎

Copy file name to clipboardExpand all lines: patch_kernel_root/patch_do_execve.cpp
+19-19Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22
#include "patch_do_execve.h"
3+
#include "analyze/base_func.h"
34
#include "analyze/ARM_asm.h"
45
PatchDoExecve::PatchDoExecve(const std::vector<char>& file_buf, const KernelSymbolOffset& sym,
56
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
5657

5758
size_t do_execve_entry_hook_jump_back_addr = do_execve_addr + 4;
5859

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());
6061
std::cout << "#生成的ROOT密匙字节集:" << str_show_root_key_mem_byte.c_str() << std::endl << std::endl;
6162

6263
vec_out_patch_bytes_data.push_back({ str_show_root_key_mem_byte, hook_func_start_addr });
6364

6465
size_t nHookFuncSize = str_root_key.length();
6566
hook_func_start_addr += nHookFuncSize;
6667

67-
auto next_asm_line_bytes_cnt = (task_struct_offset_cred.size() - 1) * 4;
6868
std::stringstream sstrAsm;
6969

7070
sstrAsm
@@ -74,20 +74,21 @@ size_t PatchDoExecve::patch_do_execve(const std::string& str_root_key, size_t ho
7474
<< "STP X11, X12, [sp, #-16]!" << std::endl
7575
<< "MOV X7, 0xFFFFFFFFFFFFF001" << std::endl
7676
<< "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
7878
<< "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
8183
<< "MOV X9, #0" << std::endl
84+
<< "LABEL_CYCLE_NAME:"
8285
<< "LDRB W10, [X7, X9]" << std::endl
83-
<< "CBZ W10, #" << 104 + next_asm_line_bytes_cnt << std::endl
8486
<< "LDRB W11, [X8, X9]" << std::endl
85-
<< "CBZ W11, #" << 96 + next_asm_line_bytes_cnt << std::endl
8687
<< "CMP W10, W11" << std::endl
87-
<< "B.NE #" << 88 + next_asm_line_bytes_cnt << std::endl
88+
<< "B.NE #JUMP_END" << std::endl
8889
<< "ADD X9, X9, 1" << std::endl
8990
<< "CMP X9, #" << str_root_key.length() << std::endl
90-
<< "BLT #-32" << std::endl;
91+
<< "BLT #JUMP_CYCLE_NAME" << std::endl;
9192
sstrAsm << "MRS X8, SP_EL0" << std::endl;
9293
for (auto x = 0; x < task_struct_offset_cred.size(); x++) {
9394
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
102103
<< "STR XZR, [X10], #8" << std::endl
103104
<< "MOV W9, 0xC" << std::endl
104105
<< "STR W9, [X10], #"<< 4 + securebits_padding << std::endl
105-
<< "MOV X9, "<< cap_ability_max << std::endl
106+
<< "MOV X9, "<< cap_ability_max << std::endl
106107
<< "STP X9, X9, [X10], #16" << std::endl
107108
<< "STP X9, X9, [X10], #16" << std::endl;
108109
if (cap_cnt == 5) {
109110
sstrAsm << "STR X9, [X10], #8" << std::endl;
110-
} else {
111-
sstrAsm << "MOV X1, X1" << std::endl;
112111
}
113112
sstrAsm << "LDXR W10, [X8]" << std::endl
114113
<< "BIC W10, W10,#0xFFF" << std::endl
115114
<< "STXR W11, W10, [X8]" << std::endl
116115
<< "STR WZR, [X8, #" << task_struct_offset_seccomp[task_struct_offset_seccomp.size() - 1] << "]" << std::endl
117116
<< "STR XZR, [X8, #" << task_struct_offset_seccomp[task_struct_offset_seccomp.size() - 1] + 8 << "]" << std::endl
117+
<< "LABEL_END:"
118118
<< "LDP X11, X12, [sp], #16" << std::endl
119119
<< "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;
122123

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");
126126
std::cout << std::endl << strAsmCode << std::endl;
127127

128128
std::string strBytes = AsmToBytes(strAsmCode);
@@ -134,14 +134,14 @@ size_t PatchDoExecve::patch_do_execve(const std::string& str_root_key, size_t ho
134134

135135
char hookOrigCmd[4] = { 0 };
136136
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));
138138
strBytes = strHookOrigCmd + strBytes.substr(0x4 * 2);
139139

140140
vec_out_patch_bytes_data.push_back({ strBytes, hook_func_start_addr });
141141

142142
std::stringstream sstrAsm2;
143143
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;
145145
std::string strBytes2 = AsmToBytes(sstrAsm2.str());
146146
if (!strBytes2.length()) {
147147
return 0;

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.