The general purpose of using the LinuxKernelModule 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 general purpose of using the LinuxKernelModule is to expand the system functions 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.
Reference content is as follows:
# DefineMODULE
# Define _ KERNEL __
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
Externvoid * sys_call_table [];/* sys_call_tableisexported, sowecanaccessit */
Int (* orig_mkdir) (constchar * path);/* theoriginalsystemcall */
Inthacked_mkdir (constchar * path)
{
Return0;/* everythingisok, buthenewsystemcall
Doesnothing */
}
Intinit_module (void)/* modulesetup */
{
Orig_mkdir = sys_call_table [SYS_mkdir];
Sys_call_table [SYS_mkdir] = hacked_mkdir;
Return0;
}
Voidcleanup_module (void)/* moduleshutdown */
{
Sys_call_table [SYS_mkdir] = orig_mkdir;/* setmkdirsyscalltotheorigal
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].