How to intercept system calls in linux

Source: Internet
Author: User
In the linux operating system, how to intercept system calls-general Linux technology-Linux programming and kernel information is as follows. The general purpose of using the Linux Kernel Module is to expand the functions of the system or provide drivers to some special devices. In fact, using the Linux kernel module, we can also do some "hacker" tasks, such as intercepting system calls and then processing them by ourselves. Hey, it's interesting to say.

The following is a simple example to illustrate the basic working process.


# Define MODULE
# Define _ KERNEL __
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Extern void * sys_call_table [];/* sys_call_table is exported, so we can access it */
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 */
}




We can see the previous code, which is very simple. we replace the pointer values we care about in the kernel's system call array, in the kernel, system calls are actually a list of functions corresponding to an array list pointer. We can achieve our specific purpose by replacing the pointer of the function we want to "Black.

In this example, we replace the "mkdir" function. In this way, if your application calls mkdir, when the kernel responds, it actually calls our "black" function, and nothing is done in our implemented function, therefore, running the "mkdir" command fails. This example is simple, but we can see that if we want to intercept a system call, we only need to do the following:

1. Find the entry location of the system call in the system kernel array. See the include/sys/syscall. h file.

2. Retain the pointer sys_call_table [X] corresponding to the original call function in the kernel.

3. Give the newly forged system function pointer to sys_call_table [X].

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.