温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么劫持printf函数的Demo

发布时间:2021-12-20 10:04:26 来源:亿速云 阅读:131 作者:iii 栏目:云计算

本篇内容主要讲解“怎么劫持printf函数的Demo”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么劫持printf函数的Demo”吧!

##劫持printf函数的Demo

[root@garnett-vm-1-3nskg test_ld]# ls hijack_printf.c printf_hello.c

root@garnett-vm-1-3nskg test_ld]# cat printf_hello.c

#include <stdio.h>
	main()
	{
	        printf("hello garnett.wang!");
	}

[root@garnett-vm-1-3nskg test_ld]# cat hijack_printf.c

#define _GNU_SOURCE

#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)("I have hijacked printf : %s", parg); // and we call the function with previous arguments

        free(parg);
}

注意:在 #include <dlfcn.h>前加 #define _GNU_SOURCE,因为RTLD_NEXTposix标准中包含的。

[root@garnett-vm-1-3nskg test_ld]# gcc printf_hello.c -o printf_hello [root@garnett-vm-1-3nskg test_ld]# ls hijack_printf.c printf_hello printf_hello.c

###劫持之前: [root@garnett-vm-1-3nskg test_ld]# ./printf_hello hello garnett.wang!

编译源代码生成劫持so文件: [root@garnett-vm-1-3nskg test_ld]# gcc -shared -fPIC hijack_printf.c -o libhijack_printf.so -ldl [root@garnett-vm-1-3nskg test_ld]# ls hijack_printf.c libhijack_printf.so printf_hello printf_hello.c

###配置LD_PRELOAD: [root@garnett-vm-1-3nskg test_ld]# export LD_PRELOAD=pwd/libhijack_printf.so [root@garnett-vm-1-3nskg test_ld]# echo $LD_PRELOAD /root/test_ld/libhijack_printf.so

###劫持之后: [root@garnett-vm-1-3nskg test_ld]# ./printf_hello I have hijacked printf : hello garnett.wang!

###查看ld dependency: [root@garnett-vm-1-3nskg test_ld]# ldd libhijack_printf.so linux-vdso.so.1 => (0x00007fff0fcfe000) libdl.so.2 => /lib64/libdl.so.2 (0x00007fb30bd30000) libc.so.6 => /lib64/libc.so.6 (0x00007fb30b99b000) /lib64/ld-linux-x86-64.so.2 (0x00007fb30c13c000)

[root@garnett-vm-1-3nskg test_ld]# ldd printf_hello linux-vdso.so.1 => (0x00007fff0c5ff000) /root/test_ld/libhijack_printf.so (0x00007fc6329a8000) libc.so.6 => /lib64/libc.so.6 (0x00007fc63260d000) libdl.so.2 => /lib64/libdl.so.2 (0x00007fc632409000) /lib64/ld-linux-x86-64.so.2 (0x00007fc632baa000)

到此,相信大家对“怎么劫持printf函数的Demo”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI