The linux-- function hijacking -- Based on LD_PRELOAD

原文地址::http://www.programering.com/a/MzN3YzMwATY.html


Recently i am facing a problem, how to differentiate a problem of library-function from application problems.for solving this problem, we need to know some knowledge about share-library and basics in Linux.For dynamic libraries, they are loaded to memory at program running. There are many benefits for this.On the other hand, we have a chance to do something. For example: replace the function, which will be loaded,as our function.Usually,this is maybe a very professional problem, at least for me.But thanks for some masters, they have been provide a mature way to our goal.
1). How to replace a function of shared libraries?
the answer is LD_PRELOAD, this is a environment variable for GUN-Linker.It is used to indicated some pre-load shared libraries.This meaning that functions in

this libraries will get a higher priority than normal libraries.

Normally, we use this technique just want to intercept some functions.So we can do some other thing( evil thing?)
above the original function.But 
1). How can i get the original function?
some functions: dlopen(), dlsym(), dlclose() and dlerror().There have been many articles to explain them.

In the next section, I will try to translate a nice article. The original article is there



This article list as follows:
A shared database.
Two. Simple LD_PRELOAD (relative to the back of the processed LD_PRELOAD)
Production and use of shared library 2.1.
2.2. dlsym()
2.3. use restrictions

Three. Relevant hiding technology

3.1 Jynx-Kit

3.2 checking methods


Below is the text:

A shared database.
As we know, dynamic library linking is implemented in the program loading. On my computer this function is realized by the ld-linux-x86-64.so.X, but it is also possible for other models of ld-linux.so.X. If you have interest, can be verified

fluxiux@handgrep:~$ readelf -l /bin/ls 
[...]
  INTERP         0x0000000000000248 0x00000000004purposes00248 0x0000000000400248 
                 0x000000000000001c 0x000000000000001c  R      1 
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2] 
[...]

(PS: where readelf is an assistant tool for reading elf files, dynamic loading procedures will generally specify a dynamic loading tool. This involves parsing the elf format, will try to make some simple analysis of ELF format in the next article. )

Compared with static compilation huge volume, dynamic compilation on a small number of. For some library functions which retain only a pointer to the related database, and contain no function entity. If you want to view aprogram included those of library calls, you can use the " command to check, for example; ldd":

<a target=_blank href="mailto:fluxiux@handgrep:~$">fluxiux@handgrep:~$</a> ldd /bin/ls 
    linux-vdso.so.1 =>  (0x00007fff0bb9a000) 
    libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f7842edc000) 
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f7842cd4000) 
    libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f7842acb000) 
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7842737000) 
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7842533000) 
    /lib64/ld-linux-x86-64.so.2 (0x00007f7843121000) 
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7842314000) 
    libattr.so.1 => /lib/x86_64-linux-gnu/libattr.so.1 (0x00007f784210f000)

We look at an example (example, below)

#include <stdio.h> 
main() 
{ 
        printf("huhu la charrue"); 
}

Are compiled into a dynamic and static two

fluxiux@handgrep:~$ gcc toto.c -o toto-dyn 
fluxiux@handgrep:~$ gcc -static toto.c -o toto-stat 
fluxiux@handgrep:~$ ls -l | grep "toto-" 
-rwxr-xr-x  1 fluxiux fluxiux     8426 2011-10-28 23:21 toto-dyn 
-rwxr-xr-x  1 fluxiux fluxiux   804327 2011-10-28 23:21 toto-stat

We see " toto-stat" toto-dyn" is almost " 96 times, why is this?

fluxiux@handgrep:~$ ldd toto-stat 
    is not a dynamic executable

(because the " toto-stat" is static.)
Dynamic linking is a kind of very good method, gave us a lot of benefits, for example:
■update libraries and still support programs that want to use older, non-backward-compatible versions of those libraries,
■override specific libraries or even specific functions in a library when executing a particular program,
■do all this while programs are running using existing libraries.
For the shared library name, we have some habits. If a database name is " soname", then usually prefixed with a " lib", a suffix ".So", and a version number. 
(PS: although only agreed: but you don't do that, your library cannot be correctly identified)
Now we can look at the LD_PRELOAD., 

Two. A simple LD_PRELOAD application

As we know, the library files generally stored in the " directory; /lib". So if you want to modify a database, the most easy to think of the way is to find the library source code, modify the re compiled again. But in addition to this project, we have another cool method, that is using Linux to provide an external interface our: LD_PRELOAD. (have a look first below this.)

Production and use of shared library 2.1.
If you want to override the " printf" behavior, can do to write one of your " printf" function

        #define _GNU_SOURCE
        #include <stdio.h>
	
        int printf(const char *format, ...)
        {
              exit(153);
        }

Then compile compile it into a shared library. Like this, 

        fluxiux@handgrep:~$ gcc -Wall -fPIC -c -o my_printf.o my_printf.c 
        my_printf.c: In function 'printf':
        my_printf.c:6:2: warning: implicit declaration of function 'exit'
        my_printf.c:6:2: warning: incompatible implicit declaration of built-in function 'exit'
        fluxiux@handgrep:~$ gcc -shared -fPIC -Wl,-soname -Wl,libmy_printf.so -o libmy_printf.so  my_printf.o

Then we modify an environment variable before running its own testing procedures

        fluxiux@handgrep:~$ export LD_PRELOAD=$PWD/libmy_printf.so
        fluxiux@handgrep:~$ ./toto-dyn

You will see the " printf" behavior has changed, it does not print the " Huhu La charrue". Okay, let us have a look " ltrace" how to say

        fluxiux@handgrep:~$ ltrace ./toto-dyn 
        __libc_start_main(0x4015f4, 1, 0x7fffa88d0908, 0x402530, 0x4025c0 <unfinished ...>
        printf("huhu la charrue" <unfinished ...>
        +++ exited (status 153) +++

Fun, our " printf" on the system " printf" be called before the. Now a new problem, if our goal was simply to change the " printf" behavior, but does not destroy the original function. How do you do, rewriting the entire function?!! That is obviously not suitable, in order to deal with this problem, several functions can see below. 

2.2. dlsym()
In the library of " libdl" there are several interesting function
dlopen() : The loading of a library
dlsym() : Pointer access a specific symbol
dlclose() : Unloading a database


Here, because the library has been in the loader is loaded, we only need to call the " dlsym" can. We give " dlsym" " RTLD_NEXT" parameter, used to get to the original " printf" function pointer. Like this

	[...]
			typeof(printf) *old_printf;
	[...]		 
			//DO HERE SOMETHING VERY EVIL
			old_printf = dlsym(RTLD_NEXT, "printf");
	[...]

Then we need to make a special treatment to the format string (parameters corresponding to the general need not be so troublesome), after treatment can be directly used

        #include <stdio.h>
        #include <dlfcn.h>
        #include <stdlib.h>
        #include <stdarg.h>
	
        int printf(const char *format, ...)
        {
                  va_list list;
                  char *parg;
                  typeof(printf) *old_printf;

                  // format variable arguments
                  va_start(list, format);
                  vasprintf(&parg, format, list);
                  va_end(list);
	
                  //DO HERE SOMETHING VERY EVIL
	
                  // get a pointer to the function "printf"
                  old_printf = dlsym(RTLD_NEXT, "printf");
                  (*old_printf)("%s", parg); // and we call the function with previous arguments
	
                  free(parg);
        }

Re compile

        fluxiux@handgrep:~$ gcc -Wall -fPIC -c -o my_printf.o my_printf.c 
        my_printf.c: In function 'printf':
        my_printf.c:21:1: warning: control reaches end of non-void function
        fluxiux@handgrep:~$ gcc -shared -fPIC -Wl,-soname -Wl libmy_printf.so -ldl -o libmy_printf.so my_printf.o 

Try it again

        fluxiux@handgrep:~$ export LD_PRELOAD=$PWD/libmy_printf.so
        fluxiux@handgrep:~$ ./toto-dyn 
        huhu la charrue

On such a call to the " printf" users do not want things quietly changed. But this mechanism has some defects

2.3. use restrictions
Although this method is very cool, but there are some limitations. For example, is not valid for static compilation procedures. Because of the function of the static compiled program does not need to connect the dynamic library. But, if the file SUID or the SGID bit is set to 1, the loading time will ignore the LD_PRELOAD (this is the developer of LD for security reasons do). 

Three. Relevant hiding technology

3.1 Jynx-Kit

About two weeks ago, we introduce a new hiding technology. It uses an automatic operation of the shell script, and has not been rkhunter and chkrootkit check. Let us have a look the actual code, in the " ld_poison.c", has 14 functions is being held hostage.

[...]
    old_fxstat = dlsym(RTLD_NEXT, "__fxstat");
    old_fxstat64 = dlsym(RTLD_NEXT, "__fxstat64");
    old_lxstat = dlsym(RTLD_NEXT, "__lxstat");
    old_lxstat64 = dlsym(RTLD_NEXT, "__lxstat64");
    old_open = dlsym(RTLD_NEXT,"open");
    old_rmdir = dlsym(RTLD_NEXT,"rmdir");
    old_unlink = dlsym(RTLD_NEXT,"unlink"); 
    old_unlinkat = dlsym(RTLD_NEXT,"unlinkat");
    old_xstat = dlsym(RTLD_NEXT, "__xstat");
    old_xstat64 = dlsym(RTLD_NEXT, "__xstat64");
    old_fdopendir = dlsym(RTLD_NEXT, "fdopendir");
    old_opendir = dlsym(RTLD_NEXT, "opendir");
    old_readdir = dlsym(RTLD_NEXT, "readdir");
    old_readdir64 = dlsym(RTLD_NEXT, "readdir64");
[...]

We can tune a " open" function to have a look, see " in the interior; __xstat" is called

[...]
    struct stat s_fstat;
[...]
    old_xstat(_STAT_VER, pathname, &s_fstat);
[...]

The following is a check operation, check the file group ID, path, and file name. (we want to make sure that the file is not a " ld.so.preload", because we want to hide it). If we want to hide files, will not return results to the user

[...]
    if(s_fstat.st_gid == MAGIC_GID || (strstr(pathname, MAGIC_DIR) != NULL) || (strstr(pathname, CONFIG_FILE) != NULL)) {
        errno = ENOENT;
        return -1;
    }
[...]


According to this idea will be above the function again after processing, we can to the user (attacker) to hide some files and behavior. But we have what method can be used to check to see here?

3.2 check the occult
Whether the hiding method Is it right? Enough let you shine at the moment, but it does take rkhunter and chkrootkit. Because the two kinds of tool use is symbolic of the inspection way based on the method, this really is not the best.

See the example below: 
First of all, if we are in the clear LD_PRELOAD variables, generates a checksum files to a specified file. Like this

        fluxiux@handgrep:~$ sha1sum toto-dyn 
        a659c72ea5d29c9a6406f88f0ad2c1a5729b4cfa  toto-dyn
        fluxiux@handgrep:~$ sha1sum toto-dyn > toto-dyn.sha1

Then check that the file checksum in the setting of LD_PRELOAD, like this

        fluxiux@handgrep:~$ export LD_PRELOAD=$PWD/libmy_printf.so
        fluxiux@handgrep:~$ sha1sum -c toto-dyn.sha1 
        toto-dyn: OK

(looks through the authentication of the document)
But is this really the right?

Obviously not, because we actually have not changed the program files, so check what time the file and will be the same. If the tool is based on latent checksum, then no doubt this is not. Other inspection techniques are: check suspicious files, symbol, port binding detection, but failed, because the concealment method is too flexible, and in Jynx we have a sort of port knocking to open the remote shell for our host.


Well, for those things we can do what? All the library, check the LD_PRELOAD direction or " /etc/ld.so.preload". We know that " dlsym" often used to find the original function, all can be

$ strace ./bin/ls
[...]
open("/home/fluxiux/blabla/Jynx-Kit/ld_poison.so", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\240\n\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=17641, ...}) = 0
mmap(NULL, 2109656, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f5e1a586000
mprotect(0x7f5e1a589000, 2093056, PROT_NONE) = 0
mmap(0x7f5e1a788000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2000) = 0x7f5e1a788000
close(3) 
[...]
open("/lib/x86_64-linux-gnu/libdl.so.2", O_RDONLY) = 3
[...]

Analysis of the " ld_poison.so" file, where we saw a lot of replacement, they are likely to shelter evil people and countenance evil practices. Have a look string information of these binaries, are likely to provide us with some interesting implications. Of course, if they are skillfully handled, it can't be helped. But from another perspective, the normal procedures need to hide their string information. 

fluxiux@handgrep:~/blabla/Jynx-Kit$ strings ld_poison.so
[...]
libdl.so.2
[...]
dlsym
fstat
[...]
lstat hooked.
ld.so.preload
xochi <-- sounds familiar
[...]
/proc/%s <-- hmmm... strange!
[...]

This is called proof hiding method Jynx-kit, think through the hidden symbol based checking to cope with the above is not too realistic. While the heuristic test results will be good. 

The original here


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园的建设目标是通过数据整合、全面共享,实现校园内教学、科研、管理、服务流程的数字化、信息化、智能化和多媒体化,以提高资源利用率和管理效率,确保校园安全。 智慧校园的建设思路包括构建统一支撑平台、建立完善管理体系、大数据辅助决策和建设校园智慧环境。通过云架构的数据中心与智慧的学习、办公环境,实现日常教学活动、资源建设情况、学业水平情况的全面统计和分析,为决策提供辅助。此外,智慧校园还涵盖了多媒体教学、智慧录播、电子图书馆、VR教室等多种教学模式,以及校园网络、智慧班牌、校园广播等教务管理功能,旨在提升教学品质和管理水平。 智慧校园的详细方案设计进一步细化了教学、教务、安防和运维等多个方面的应用。例如,在智慧教学领域,通过多媒体教学、智慧录播、电子图书馆等技术,实现教学资源的共享和教学模式的创新。在智慧教务方面,校园网络、考场监控、智慧班牌等系统为校园管理提供了便捷和高效。智慧安防系统包括视频监控、一键报警、阳光厨房等,确保校园安全。智慧运维则通过综合管理平台、设备管理、能效管理和资产管理,实现校园设施的智能化管理。 智慧校园的优势和价值体现在个性化互动的智慧教学、协同高效的校园管理、无处不在的校园学习、全面感知的校园环境和轻松便捷的校园生活等方面。通过智慧校园的建设,可以促进教育资源的均衡化,提高教育质量和管理效率,同时保障校园安全和提升师生的学习体验。 总之,智慧校园解决方案通过整合现代信息技术,如云计算、大数据、物联网和人工智能,为教育行业带来了革命性的变革。它不仅提高了教育的质量和效率,还为师生创造了一个更加安全、便捷和富有智慧的学习与生活环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值