Linux intrusion Monitoring System LIDS principle-Linux Enterprise Application-Linux server application information. The following is a detailed description. I. Intrusion
With the increase of Linux Hosts on the Internet, more and more security vulnerabilities are discovered on the current GNU/Linux system. You may have heard of bugs found in Linux on the Internet, which may cause the system to be easily attacked by hackers.
Because Linux is an open code system, it is easy to discover vulnerabilities and patch them out quickly. However, when the vulnerability is not published, the Administrator is too lazy to patch the vulnerability. Hackers can easily attack the system and obtain root permissions. In the current GNU/Linux environment, hackers can do anything they want. Now you can ask, what can we do now?
1.1 where are the current GNU/Linux errors?
A super user will abuse his power and can do everything he wants to do. As root. It will change everything.
Many system files are easily changed. These files may be very important files, such as/bin/login. If a hacker enters, he can upload a login program to overwrite/bin/login, in this way, he can log on to the system without the login name and password. However, these files do not need to be changed frequently unless you want to upgrade the system.
Module modules is easily used to interrupt the kernel. The module is designed to make the Linux kernel more modular and more efficient. However, when a module is added to the kernel, it becomes a part of the kernel and can do the work that the original kernel can do. Therefore, some unfriendly code can be written as a module to be added to the kernel, and the code will redirect the system call and run as a virus.
Processes are unprotected. Some processes, such as the web server in the background, are always considered to be programs without strict protection. Therefore, they will be easily attacked by hackers.
1.2 What is LIDS's idea.
Protect important files. Because files are easily changed by the root user, why not perform strict file operations? Therefore, LIDS changes the security system calls of the file system in the kernel. If a person accesses a file at some time, he will access the system call and then we can check the file name and see if they are protected. If it has been protected, we can reject the visitor's request.
Protect important processes. This idea is not the same as that of the above process of protection. When a process runs in a system, it will have an entry in the/proc file system that uses pid as the path name. So, if you use "ps? Axf "you can display the currently running process. You can ask if you want to protect these processes. If you want to kill a process, first type "ps" to get the PID of the process, and then type "kill" pid "to kill it. However, if I don't let you see a process, how do you kill it? Therefore, LIDS uses hidden processes to protect it.
Another important method is to prevent anyone from killing the process, including the root user. LIDS can protect all processes whose parent process is init (pid = 1.
Encapsulate the kernel. Sometimes we need to add some necessary modules to the kernel for use. In addition, we also need to reject anyone, including the root user, from inserting modules into the kernel. How can we balance this contradiction? We can only insert modules when the system starts, and then encapsulate modules. After encapsulation, no one in the kernel can insert modules into the kernel. With this encapsulation function, we can use it to protect important files and processes. We can only allow necessary processes when the system is started and only change necessary files. After the kernel is encapsulated, we cannot modify the file.
Ii. File System Protection
2.1 protecting file systems is one of the important functions of LIDS. This function is implemented at the VFS (Virtual File System) layer of the kernel. We can protect all types of file systems, such as EXT2 and FAT.
In LIDS, protected files are classified into the following types:
Read-Only files or directories. Read-Only files mean they are not allowed to be rewritten, for example, in the directory/usr/bin,/sbin. Most of these types of files are binary system programs or system configuration files. We do not need to change these files when upgrading the system.
Only files or directories can be added. These files are those that can only increase the size. Most of them are system daily value files. For example, only files can be added to/var/log.
Additional files or directories are not protected. In general, you want to protect all files in the directory, but you still need some special files not to be protected. Therefore, we can define these files as additional read-only files.
Protects file systems from being mounted or detached. When you mount a file system at startup, You can prohibit everyone, even root, from detaching the file system. You can also prohibit anyone from attaching a file system to the current file system to overwrite it.
2.2 how does LIDS protect files in the kernel?
In this section, we will see some kernel code to understand how LIDS protects files.
Linux File System Data Structure Program
First, we must understand the Virtual File System of Linux.
Every file in Linux has a node inode no matter what it looks like. The file system provides the following data structure.
In/usr/src/Linux/include/Linux/fs. h
Struct inode {
Struct list_head I _hash;
Struct list_head I _list;
Struct list_head I _dentry;
Unsigned long I _ino; ----> inode number.
Unsigned int I _count;
Kdev_t I _dev; ----> device number.
Umode_t I _mode;
Nlink_t I _nlink;
Uid_t I _uid;
......
}
Note: It is used to identify a node inode. This means that you can use a pair to obtain the unique inode in a system.
In/ur/src/Linux/cinclude/Linux/dcache. h
Struct dentry {
Int d_count;
Unsigned int d_flags;
Struct inode * d_inode;/* Where the name belongs to-NULL is negative */
Struct dentry * d_parent;/* parent directory */
Struct dentry * d_mounts;/* mount information */
Struct dentry * d_covers;
Struct list_head d_hash;/* lookup hash list */
Struct list_head d_lru;/* d_count = 0 LRU list */
Struct list_head d_child;/* child of parent list */
Struct list_head d_subdirs;/* our
......
}
Dentry is the entry to a directory file. Through this portal, we can easily move under the parent directory of the file.
For example, if the inode of a file is (struct inode *) file_inode, if you can use file_inode-> d_entry to get its directory entry, and use file_inode-> d_entry-> d_parent to get the directory entry of the parent directory.
LIDS protects Data Structures
After analyzing the Linux File System, let's take a look at how LIDS supports VFS to protect files and directories.
In/usr/src/Linux/fs/lids. c
Struct secure_ino {
Unsigned long int ino;/* the inode number */
Kdev_t dev;/* the dev number */
Int type;/* the file type */
};
The above structure uses a pair of nodes to store and protect files or directories. "Type" indicates the file type of the protected node.
LIDS has four types
In/usr/src/Linux/include/Linux/fs. h
# Define LIDS_APPEND 1/* append only file */
# Define LIDS_READONLY 2/* Read Only File */
# Define LIDS_DEVICE 3/* Protect MBR Writing to device */
# Define LIDS_IGNORE 4/* Ignore the protection */
Through the secure_ino structure, we can easily implement the files that are protected or execute the following functions in the kernel.
In/usr/src/Linux/fs/lids. c
Int lids_add_inode (unsigned long int inode, kdev_t dev, int type)
{
If (last_secure = (LIDS_MAX_INODE-1 ))
Return 0;
Secure [last_secure]. ino = inode;
Secure [last_secure]. dev = dev;
Secure [last_secure]. type = type;
As you can see in the code above, it is very easy to add secure_ino to a node. Protected nodes will be first activated when the system starts. The initialization program is in init_vfs_security () of/usr/src/Linux/fs/lids. c.
Now, let's see how LIDS checks whether a node is protected.
In/usr/src/Linux/fs/open. c
Int do_truncate (struct dentry * dentry, unsigned long length)
{
Struct inode * inode = dentry-> d_inode;
Int error;
Struct iattr newattrs;
/* Not pretty: "inode-> I _size" shouldnt really be "off_t". But it is .*/
If (off_t) length <0)
Return-EINVAL;
# Ifdef CONFIG_LIDS
If (lids_load & lids_local_load ){
Error = lids_check_base (dentry, LIDS_READONLY );
If (error ){
Lids_security_alert ("Try to truncate a protected file (dev % d, inode % ld )",
MAJOR (dentry-> d_inode-> I _dev ),
MINOR (dentry-> d_inode-> I _dev ),
Dentry-> d_inode-> I _ino );
.....................
This is an example of how to add LIDS to the kernel for detection. You will see that lids_check_base () is a core function of the LIDS protection method.
You can see that many LIDS protection methods use the lids_check_base () function, especially under the subdirectory of the Linux kernel, where LIDS is to be protected.
In/usr/src/Linux/fs/lids. c
Int lids_check_base (struct dentry * base, int flag)
{
..................
Inode = base-> d_inode;/* get the inode number */
Parent = base-> d_parent;/* get the parent diretory */
.................
----> Do {
If (inode = parent-> d_inode)
Break;
If (retval = lids_search_inode (inode ))){
If (retval = LIDS_IGNORE |
(Retval = LIDS_DEVICE & flag! = LIDS_DEVICE ))
Break;
If (flag = LIDS_READONLY |
(Flag = LIDS_APPEND & retval> flag) |
(Flag = LIDS_DEVICE & flag = retval )){
Return-EROFS;
}
Break;
}
Inode = parent-> d_inode;
} While (parent = parent-> d_parent )! = NULL ));
Return 0;
}
Lids_check_base () checks whether the dentry of a given file and its parent directory are protected.
Note: if its parent directory is protected, the files under it will also be protected.
For example, if "/etc/" is protected, "/etc/passwd" is also protected.
In kernel Protection System Call
To protect the system, LIDS checks critical system calls. Therefore, we can protect system calls and limit user calls of file systems.
These are examples,
Open () is to prohibit some rights to protect the opening of files. You can check open_namei () when calling open_namei.
Mknod () and mknod are used to protect mknod under a specified directory.
Unlink (), check do_unlink () in the kernel code ().
Iii. Protection Equipment
Linux devices are listed as files in the/dev/directory. We can use the above file protection method to protect the devices. However, in some cases, you can use IO operations to bypass the file system to read and write devices. We must pay attention to this problem.
3.1 device, kernel I/O
Devices in GNU/Linux systems are expressed as files, so we can protect the devices by protecting the file system.
The user's I/O access is implemented by calling sys_operm and sys_iopl. You can check/usr/src/Linux/arch/i386/kernel/ioport .. This is based on the system structure. If you are on another platform, you need to pay attention to their changes.
3.2 How to Use LIDS to protect
In most cases, the program does not need to access the device by the/dev device file name. However, some special programs need direct access, such as X Server, which will be written to/dev/mem and even I/O devices. We need something extra to protect the device. LIDS will define this function when configuring the kernel.
CONFIG_LIDS_ALLOW_DEV_MEM. If you choose to enable this function, you can allow special programs to access critical kernel devices such as/dev/men and/dev/kmen. If you want to use the X Server of the kernel, selecting this function will provide the entire path and file name during Kernel configuration.
CONFIG_LIDS_ALLOW_RAW_DISKS. If this feature is enabled, you can allow special programs to access the physical disk.
CONFIG_LIDS_ALLOW_IO_PORTS. If you choose to enable this function, you can allow some special programs to access the I/O port.
It is called when the system runs init_vfs_security () in fs/lids. c.
If a process or program needs to directly access the ip port or disk device, LIDS checks it in the array allow_raw_disk, last_io_ports, and so on ). This check is implemented by calling lids_search_inode (inode) in lids_check_base.
For example, let's look at CONFIG_LIDS_ALLOW_DEV_MEM.
/* In lids_search_inode ()*/
# Ifdef CONFIG_LIDS_ALLOW_DEV_MEM
For (I = 0; I <last_dev_mem; I ++ ){
If (allow_dev_mem . Ino = ino & allow_dev_mem. Dev = dev ){
Return LIDS_READONLY;
}
}
# Endif
# Ifdef CONFIG_LIDS_ALLOW_RAW_DISKS
Which program node is included in allow_dev_mem is initially modified in init_vfs_security () when the system is started. In the same way, apart from some special programs, we can protect devices, I/O access, and so on.
Iv. Protection of important processes
A process is a dynamic entry to the operating system. There are two special processes in the kernel: process ID 0 (swapd) and process ID 1 (init ). The Init process is the parent process of all processes at system startup.
4.1 processes that cannot be killed.
Just as you can see if someone wants to win the root privilege, we can easily kill processes that send special signals to the kernel. To kill a process, you must get the process ID and run the kill command to kill it.
The kill command is used to call the system kill process. It is used in the sys_kill () command in the kernel.
Let's take a look at the LIDS protection code.
In/usr/src/Linux/kernel/signal. c
Asmlinkage int
Sys_kill (int pid, int sig)
{
Struct siginfo info;
# Ifdef CONFIG_LIDS_INIT_CHILDREN_LOCK pid_t this_pid;
Int I;
# Ifdef CONFIG_LIDS_ALLOW_KILL_INIT_CHILDREN
If (! (Current-> flags & PF_KILLINITC ))
# Endif
If (lids_load & lids_local_load & LIDS_FISSET (lids_flags, LIDS_FLAGS_LOCK_INIT_CHILDREN )){
This_pid = pid> 0? Pid:-pid;
For (I = 0; I if (this_pid = lids_protected_pid){
Lids_security_alert ("Try to kill pid = % d, sig = % d
", Pid, sig );
Return-EPERM;
}
}
}
# Endif
...
}
You can see two labels in the kernel: CONFIG_LIDS_INIT_CHILDREN_LOCK and CONFIG_LIDS_ALLOW_KILL_INIT_CHILDREN.
When CONFIG_LIDS_INIT_CHILDREN_LOCK is enabled, LIDS can protect the first running program. For example, if you run the inetd program in the system, you can run the program before hiding the kernel, and then you can kill it. However, if some people telnet to your machine, inetd will create a sub-process to serve the user. This sub-process will not be protected by LIDS because the user exits and kills the program at any time.
4.2 hide a process
Another way to protect the process is to hide the process. When a hacker crisis your system. He will log in and then check whether there are any known processes monitoring it. Then he kills it. If you hide the process of this function, the hacker will not know all the conditions of the process and you can record anything that he does on your system.
How to hide a process
To hide a process, you must provide a full path name When configuring the kernel.
When the kernel is started, LIDS accesses the file node to a structure named proc_to_hide.
/* In fs/lids. c, init_vfs_security (),
Fill up the hidden process in proc_to_hide []
*/
# Ifdef CONFIG_LIDS_HIDE_PROC
Lids_fill_table (proc_to_hide, & last_hide, LIDS_MAX_ALLOWED, CONFIG_LIDS_HIDDEN_PROC_PATH );
# Endif
PF_HIDDEN can be used to display process commands (such as "ps? A) to display and check the process. If a process is hidden by LIDS, when it is executed, the process will get a PF_HIDDEN attribute. Then, when the system outputs the system process information to the user, it can check whether the current output process has the PF_HIDDEN flag. If it finds out, it will not output information about the process.
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