LINUX.ORG.RU

История изменений

Исправление LINUX-ORG-RU, (текущая версия) :

Сам попытался придумать, но не смог, нашёл это

Чуть подправил

/*
 * Hook main() using LD_PRELOAD, because why not?
 * Obviously, this code is not portable. Use at your own risk.
 *
 * Compile using 'gcc hax.c -o hax.so -fPIC -shared -ldl'
 * Then run your program as 'LD_PRELOAD=$PWD/hax.so ./a.out'
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
#include <time.h>

/* Trampoline for the real main() */
static int (*main_orig)(int, char **, char **);

/* Our fake main() that gets called by __libc_start_main() */
int main_hook(int argc, char **argv, char **envp)
{
    clock_t c_run = clock();
    printf("--- Start Clock: %ld ---\n",c_run);
    int ret = main_orig(argc, argv, envp);
    clock_t c_end = clock();
    printf("----- End Clock: %ld----\n",c_end);

    printf("----- Clocks per external main: %ld----\n",c_end-c_run);
    return ret;
}

/*
 * Wrapper for __libc_start_main() that replaces the real main
 * function with our hooked version.
 */
int __libc_start_main(
    int (*main)(int, char **, char **),
    int argc,
    char **argv,
    int (*init)(int, char **, char **),
    void (*fini)(void),
    void (*rtld_fini)(void),
    void *stack_end)
{
    /* Save the real main function address */
    main_orig = main;

    /* Find the real __libc_start_main()... */
    typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");

    /* ... and call it with our custom main function */
    return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
}

Собираем и дёргаем любые программы

gcc hook.c -o hook.so -fPIC -shared -ldl
dron@gnu:~/Рабочий-стол$ export LD_PRELOAD=`pwd`/hook.so 
dron@gnu:~/Рабочий-стол$ ls
--- Start Clock: 1690 ---
app  applib.out  ext.c  ext.so  hook.c  hook.so  main.c
----- End Clock: 2135----
----- Clocks per external main: 445----
dron@gnu:~/Рабочий-стол$ lua -e "('aaa'):rep(100):rep(1000)"
--- Start Clock: 4178 ---
----- End Clock: 6252----
----- Clocks per external main: 2074----
dron@gnu:~/Рабочий-стол$ uname 
--- Start Clock: 2873 ---
Linux
----- End Clock: 3209----
----- Clocks per external main: 336----
dron@gnu:~/Рабочий-стол$

Тока оно это, я не уверен что тут всё адекватно :D

Исправление LINUX-ORG-RU, :

Сам попытался придумать, но не смог, нашёл это

Чуть подправил

/*
 * Hook main() using LD_PRELOAD, because why not?
 * Obviously, this code is not portable. Use at your own risk.
 *
 * Compile using 'gcc hax.c -o hax.so -fPIC -shared -ldl'
 * Then run your program as 'LD_PRELOAD=$PWD/hax.so ./a.out'
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
#include <time.h>

/* Trampoline for the real main() */
static int (*main_orig)(int, char **, char **);

/* Our fake main() that gets called by __libc_start_main() */
int main_hook(int argc, char **argv, char **envp)
{
    clock_t c_run = clock();
    printf("--- Start Clock: %ld ---\n",c_run);
    int ret = main_orig(argc, argv, envp);
    clock_t c_end = clock();
    printf("----- End Clock: %ld----\n",c_end);

    printf("----- Clocks per external main: %ld----\n",c_end-c_run);
    return ret;
}

/*
 * Wrapper for __libc_start_main() that replaces the real main
 * function with our hooked version.
 */
int __libc_start_main(
    int (*main)(int, char **, char **),
    int argc,
    char **argv,
    int (*init)(int, char **, char **),
    void (*fini)(void),
    void (*rtld_fini)(void),
    void *stack_end)
{
    /* Save the real main function address */
    main_orig = main;

    /* Find the real __libc_start_main()... */
    typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");

    /* ... and call it with our custom main function */
    return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
}

Собираем и дёргаем любые программы

gcc hook.c -o hook.so -fPIC -shared -ldl
dron@gnu:~/Рабочий-стол$ export LD_PRELOAD=`pwd`/hook.so 
dron@gnu:~/Рабочий-стол$ ls
--- Start Clock: 1690 ---
app  applib.out  ext.c  ext.so  hook.c  hook.so  main.c
----- End Clock: 2135----
----- Clocks per external main: 445----
dron@gnu:~/Рабочий-стол$ lua -e "('aaa'):rep(100):rep(1000)"
--- Start Clock: 4178 ---
----- End Clock: 6252----
----- Clocks per external main: 2074----
dron@gnu:~/Рабочий-стол$ uname 
--- Start Clock: 2873 ---
Linux
----- End Clock: 3209----
----- Clocks per external main: 336----
dron@gnu:~/Рабочий-стол$

Тока оно это, я не уверен что тут всё адекватно :D

Исходная версия LINUX-ORG-RU, :

Сам попытался придумать, но не смог, нашёл это

Чуть подправил

/*
 * Hook main() using LD_PRELOAD, because why not?
 * Obviously, this code is not portable. Use at your own risk.
 *
 * Compile using 'gcc hax.c -o hax.so -fPIC -shared -ldl'
 * Then run your program as 'LD_PRELOAD=$PWD/hax.so ./a.out'
 */

#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
#include <time.h>

/* Trampoline for the real main() */
static int (*main_orig)(int, char **, char **);

/* Our fake main() that gets called by __libc_start_main() */
int main_hook(int argc, char **argv, char **envp)
{
    clock_t c_run = clock();
    printf("--- Start Clock: %ld ---\n",c_run);
    int ret = main_orig(argc, argv, envp);
    clock_t c_end = clock();
    printf("----- End Clock: %ld----\n",c_end);

    printf("----- Clocks per external main: %ld----\n",c_end-c_run);
    return ret;
}

/*
 * Wrapper for __libc_start_main() that replaces the real main
 * function with our hooked version.
 */
int __libc_start_main(
    int (*main)(int, char **, char **),
    int argc,
    char **argv,
    int (*init)(int, char **, char **),
    void (*fini)(void),
    void (*rtld_fini)(void),
    void *stack_end)
{
    /* Save the real main function address */
    main_orig = main;

    /* Find the real __libc_start_main()... */
    typeof(&__libc_start_main) orig = dlsym(RTLD_NEXT, "__libc_start_main");

    /* ... and call it with our custom main function */
    return orig(main_hook, argc, argv, init, fini, rtld_fini, stack_end);
}

Собираем и дёргаем любые программы

gcc hook.c -o hook.so -fPIC -shared -ldl
dron@gnu:~/Рабочий-стол$ export LD_PRELOAD=`pwd`/hook.so 
dron@gnu:~/Рабочий-стол$ ls
--- Start Clock: 1690 ---
app  applib.out  ext.c  ext.so  hook.c  hook.so  main.c
----- End Clock: 2135----
----- Clocks per external main: 445----
dron@gnu:~/Рабочий-стол$ lua -e "('aaa'):rep(100):rep(1000)"
--- Start Clock: 4178 ---
----- End Clock: 6252----
----- Clocks per external main: 2074----
dron@gnu:~/Рабочий-стол$ uname 
--- Start Clock: 2873 ---
Linux
----- End Clock: 3209----
----- Clocks per external main: 336----
dron@gnu:~/Рабочий-стол$

Тока оно это, я не уверен что тут всё адекватно :D