Changeset 1607

Show
Ignore:
Timestamp:
03/24/08 19:22:30 (7 months ago)
Author:
common
Message:

libemu

  • restructured the process environment
    now we have emu_env as the holder for linux and win32 as well as emu_env_hook for the api hooks.
    env functions take emu_env and emu_env_hook as args, and access the required *thing* themselves
    furthermore I introduced a different kind of hooking api calls using variadic callbackfunctions
    the benefit is easy, the interactive hooks do not have to be part of libemu itself any longer
    and hooking calls does not require access to the emu/cpu/memory
    all you have to do is retrieve the arguments for the function to be hooked from the va_list using va_arg

- sctest got -i --interactive, using the interactive hooks from sctest itself

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libemu/trunk/configure.ac

    r1509 r1607  
    7777# library soname 
    7878# check http://sources.redhat.com/autobook/autobook/autobook_91.html#SEC91 
    79 libemu_current=1 
     79libemu_current=2 
    8080libemu_revision=0 
    8181libemu_age=0 
  • libemu/trunk/include/emu/environment/linux/emu_env_linux.h

    r1526 r1607  
    4040 
    4141struct emu_profile; 
     42struct emu_env; 
    4243 
    4344struct emu_env_linux 
     
    4546        struct emu *emu; 
    4647        struct emu_hashtable *syscall_hooks_by_name; 
    47         struct emu_env_linux_syscall *syscall_hooks; 
    48         struct emu_profile *profile; 
     48        struct emu_env_linux_syscall *syscall_hookx; 
     49        struct emu_env_hook *hooks; 
     50//      struct emu_profile *profile; 
    4951}; 
    5052 
     
    5254void emu_env_linux_free(struct emu_env_linux *eel); 
    5355 
    54 struct emu_env_linux_syscall *emu_env_linux_syscall_check(struct emu_env_linux *env); 
     56struct emu_env_hook *emu_env_linux_syscall_check(struct emu_env *env); 
    5557 
    5658 
     
    6163}; 
    6264 
     65 
     66typedef uint32_t (*userhook)(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall, ...); 
     67 
    6368struct emu_env_linux_syscall 
    6469{ 
    6570        const char *name; 
    66         int32_t         (*fnhook)(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall); 
     71        int32_t         (*fnhook)(struct emu_env *env, struct emu_env_hook *hook); 
    6772        void *userdata; 
     73        uint32_t (*userhook)(struct emu_env *env, struct emu_env_hook *hook, ...); 
    6874}; 
    6975 
    70 int32_t emu_env_linux_syscall_hook(struct emu_env_linux *env, const char *syscallname, 
    71                                                                    int32_t             (*fnhook)(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall), 
     76int32_t emu_env_linux_syscall_hook(struct emu_env *env, const char *syscallname, 
     77                                                                   uint32_t (*userhook)(struct emu_env *env, struct emu_env_hook *hook, ...), 
    7278                                                                   void *userdata); 
    7379 
  • libemu/trunk/include/emu/environment/linux/env_linux_syscall_hooks.h

    r1476 r1607  
    2727 
    2828/* 1 exit */ 
    29 int32_t env_linux_hook_exit(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall); 
     29int32_t env_linux_hook_exit(struct emu_env *env, struct emu_env_hook *hook); 
    3030 
    3131/* 2 fork */ 
    32 int32_t env_linux_hook_fork(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall); 
     32int32_t env_linux_hook_fork(struct emu_env *env, struct emu_env_hook *hook); 
    3333 
    3434/* 11 execve */ 
    35 int32_t env_linux_hook_execve(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall); 
     35int32_t env_linux_hook_execve(struct emu_env *env, struct emu_env_hook *hook); 
    3636 
    3737/* 63 dup2 */ 
    38 int32_t env_linux_hook_dup2(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall); 
     38int32_t env_linux_hook_dup2(struct emu_env *env, struct emu_env_hook *hook); 
    3939 
    4040/* 102 socketcall */ 
     
    5858        socketpair - missing 
    5959*/ 
    60 int32_t env_linux_hook_socketcall(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall); 
     60int32_t env_linux_hook_socketcall(struct emu_env *env, struct emu_env_hook *hook); 
    6161 
    6262 
  • libemu/trunk/include/emu/environment/win32/emu_env_w32.h

    r1505 r1607  
    2626 *******************************************************************************/ 
    2727 
     28#ifndef HAVE_EMU_ENV_W32 
     29#define HAVE_EMU_ENV_W32 
     30 
    2831#include <stdint.h> 
    2932 
     
    3235struct emu_env_w32_dll_export; 
    3336struct emu_profile; 
     37struct emu_env; 
     38struct emu_env_hook; 
    3439 
    3540/** 
     
    5257         */ 
    5358        uint32_t        baseaddr; 
    54  
    55         struct emu_profile *profile; 
    5659}; 
    5760 
     
    7982 *  
    8083 * @param env        the env 
    81  * @param dllname    the dllname, if NULL the export is searched within all loaded dlls 
    8284 * @param exportname the exportname, f.e. "socket" 
    8385 * @param fnhook     pointer to the hook function 
     
    8688 *         on failure: -1 
    8789 */ 
    88 int32_t emu_env_w32_export_hook(struct emu_env_w32 *env, 
    89                                                                 const char *dllname, 
     90int32_t emu_env_w32_export_hook(struct emu_env *env, 
    9091                                                                const char *exportname,  
    91                                                                 int32_t                (*fnhook)(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex), 
     92                                                                uint32_t               (*fnhook)(struct emu_env *env, struct emu_env_hook *hook, ...), 
    9293                                                                void *userdata); 
    9394 
     
    103104 *         on failure: NULL 
    104105 */ 
    105 struct emu_env_w32_dll_export *emu_env_w32_eip_check(struct emu_env_w32 *env); 
     106struct emu_env_hook *emu_env_w32_eip_check(struct emu_env *env); 
     107 
     108#endif 
     109 
  • libemu/trunk/include/emu/environment/win32/emu_env_w32_dll.h

    r1317 r1607  
    2626 *******************************************************************************/ 
    2727 
     28#ifndef HAVE_EMU_ENV_W32_DLL_H 
     29#define HAVE_EMU_ENV_W32_DLL_H 
     30 
    2831#include <stdint.h> 
    2932 
     33struct emu_env_hook; 
    3034struct emu_env_w32_dll_export; 
    3135 
     
    3943        uint32_t        baseaddr; 
    4044 
    41         struct emu_env_w32_dll_export *exports; 
     45        struct emu_env_w32_dll_export *exportx; 
     46        struct emu_env_hook *hooks; 
    4247        struct emu_hashtable *exports_by_fnptr; 
    4348        struct emu_hashtable *exports_by_fnname; 
     
    6570}; 
    6671 
     72#endif 
  • libemu/trunk/include/emu/environment/win32/emu_env_w32_dll_export.h

    r1442 r1607  
    3434struct emu; 
    3535struct emu_env_w32; 
     36struct emu_env; 
     37struct emu_env_hook; 
    3638 
     39typedef uint32_t        (*win32userhook)(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex, ...); 
    3740 
    3841struct emu_env_w32_dll_export 
     
    4043        char            *fnname; 
    4144        uint32_t        virtualaddr; 
    42     int32_t             (*fnhook)(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
     45    int32_t             (*fnhook)(struct emu_env *env, struct emu_env_hook *hook); 
    4346        void            *userdata; 
     47        uint32_t        (*userhook)(struct emu_env *env, struct emu_env_hook *hook, ...); 
    4448}; 
    4549 
  • libemu/trunk/include/emu/environment/win32/env_w32_dll_export_kernel32_hooks.h

    r1367 r1607  
    2828#include <stdint.h> 
    2929 
    30 int32_t env_w32_hook_CloseHandle(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    31 int32_t env_w32_hook_CreateFileA(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    32 int32_t env_w32_hook_CreateProcessA(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    33 int32_t env_w32_hook_DeleteFileA(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    34 int32_t env_w32_hook_ExitProcess(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    35 int32_t env_w32_hook_ExitThread(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    36 int32_t env_w32_hook_fclose(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    37 int32_t env_w32_hook_fopen(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    38 int32_t env_w32_hook_fwrite(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    39 int32_t env_w32_hook_GetProcAddress(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    40 int32_t env_w32_hook_GetSystemDirectoryA(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    41 int32_t env_w32_hook_GetTickCount(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    42 int32_t env_w32_hook__hwrite(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    43 int32_t env_w32_hook__lclose(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    44 int32_t env_w32_hook__lcreat(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    45 int32_t env_w32_hook__lwrite(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    46 int32_t env_w32_hook_LoadLibrayA(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    47 int32_t env_w32_hook_malloc(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    48 int32_t env_w32_hook_memset(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    49 int32_t env_w32_hook_SetUnhandledExceptionFilter(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    50 int32_t env_w32_hook_WaitForSingleObject(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    51 int32_t env_w32_hook_WinExec(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    52 int32_t env_w32_hook_WriteFile(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
     30int32_t env_w32_hook_CloseHandle(struct emu_env *env, struct emu_env_hook *hook); 
     31int32_t env_w32_hook_CreateFileA(struct emu_env *env, struct emu_env_hook *hook); 
     32int32_t env_w32_hook_CreateProcessA(struct emu_env *env, struct emu_env_hook *hook); 
     33int32_t env_w32_hook_DeleteFileA(struct emu_env *env, struct emu_env_hook *hook); 
     34int32_t env_w32_hook_ExitProcess(struct emu_env *env, struct emu_env_hook *hook); 
     35int32_t env_w32_hook_ExitThread(struct emu_env *env, struct emu_env_hook *hook); 
     36int32_t env_w32_hook_fclose(struct emu_env *env, struct emu_env_hook *hook); 
     37int32_t env_w32_hook_fopen(struct emu_env *env, struct emu_env_hook *hook); 
     38int32_t env_w32_hook_fwrite(struct emu_env *env, struct emu_env_hook *hook); 
     39int32_t env_w32_hook_GetProcAddress(struct emu_env *env, struct emu_env_hook *hook); 
     40int32_t env_w32_hook_GetSystemDirectoryA(struct emu_env *env, struct emu_env_hook *hook); 
     41int32_t env_w32_hook_GetTickCount(struct emu_env *env, struct emu_env_hook *hook); 
     42int32_t env_w32_hook__hwrite(struct emu_env *env, struct emu_env_hook *hook); 
     43int32_t env_w32_hook__lclose(struct emu_env *env, struct emu_env_hook *hook); 
     44int32_t env_w32_hook__lcreat(struct emu_env *env, struct emu_env_hook *hook); 
     45int32_t env_w32_hook__lwrite(struct emu_env *env, struct emu_env_hook *hook); 
     46int32_t env_w32_hook_LoadLibrayA(struct emu_env *env, struct emu_env_hook *hook); 
     47int32_t env_w32_hook_malloc(struct emu_env *env, struct emu_env_hook *hook); 
     48int32_t env_w32_hook_memset(struct emu_env *env, struct emu_env_hook *hook); 
     49int32_t env_w32_hook_SetUnhandledExceptionFilter(struct emu_env *env, struct emu_env_hook *hook); 
     50int32_t env_w32_hook_WaitForSingleObject(struct emu_env *env, struct emu_env_hook *hook); 
     51int32_t env_w32_hook_WinExec(struct emu_env *env, struct emu_env_hook *hook); 
     52int32_t env_w32_hook_WriteFile(struct emu_env *env, struct emu_env_hook *hook); 
    5353 
    5454#define HANDLE int32_t 
  • libemu/trunk/include/emu/environment/win32/env_w32_dll_export_msvcrt_hooks.h

    r1531 r1607  
    2828#include <stdint.h> 
    2929 
    30 int32_t env_w32_hook__execv(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
     30int32_t env_w32_hook__execv(struct emu_env *env, struct emu_env_hook *hook); 
    3131 
  • libemu/trunk/include/emu/environment/win32/env_w32_dll_export_urlmon_hooks.h

    r1367 r1607  
    2828#include <stdint.h> 
    2929 
    30 int32_t env_w32_hook_URLDownloadToFileA(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
     30int32_t env_w32_hook_URLDownloadToFileA(struct emu_env *env, struct emu_env_hook *hook); 
    3131 
  • libemu/trunk/include/emu/environment/win32/env_w32_dll_export_ws2_32_hooks.h

    r1367 r1607  
    2828#include <stdint.h> 
    2929 
    30 int32_t env_w32_hook_accept(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    31 int32_t env_w32_hook_bind(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    32 int32_t env_w32_hook_closesocket(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    33 int32_t env_w32_hook_connect(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    34 int32_t env_w32_hook_listen(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    35 int32_t env_w32_hook_recv(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    36 int32_t env_w32_hook_send(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    37 int32_t env_w32_hook_sendto(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    38 int32_t env_w32_hook_socket(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    39 int32_t env_w32_hook_WSASocketA(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
    40 int32_t env_w32_hook_WSAStartup(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex); 
     30int32_t env_w32_hook_accept(struct emu_env *env, struct emu_env_hook *hook); 
     31int32_t env_w32_hook_bind(struct emu_env *env, struct emu_env_hook *hook); 
     32int32_t env_w32_hook_closesocket(struct emu_env *env, struct emu_env_hook *hook); 
     33int32_t env_w32_hook_connect(struct emu_env *env, struct emu_env_hook *hook); 
     34int32_t env_w32_hook_listen(struct emu_env *env, struct emu_env_hook *hook); 
     35int32_t env_w32_hook_recv(struct emu_env *env, struct emu_env_hook *hook); 
     36int32_t env_w32_hook_send(struct emu_env *env, struct emu_env_hook *hook); 
     37int32_t env_w32_hook_sendto(struct emu_env *env, struct emu_env_hook *hook); 
     38int32_t env_w32_hook_socket(struct emu_env *env, struct emu_env_hook *hook); 
     39int32_t env_w32_hook_WSASocketA(struct emu_env *env, struct emu_env_hook *hook); 
     40int32_t env_w32_hook_WSAStartup(struct emu_env *env, struct emu_env_hook *hook); 
    4141 
  • libemu/trunk/src/Makefile.am

    r1531 r1607  
    7979libemu_la_SOURCES += libdasm.c libdasm.h opcode_tables.h 
    8080 
     81libemu_la_SOURCES += environment/emu_env.c 
    8182libemu_la_SOURCES += environment/emu_profile.c 
    8283libemu_la_SOURCES += environment/win32/emu_env_w32.c 
  • libemu/trunk/src/emu_shellcode.c

    r1592 r1607  
    3737#include "emu/emu_source.h" 
    3838#include "emu/emu_getpc.h" 
     39#include "emu/environment/emu_env.h" 
    3940#include "emu/environment/win32/emu_env_w32.h" 
    4041#include "emu/environment/win32/emu_env_w32_dll_export.h" 
     
    101102//      struct emu_list_root *tested_positions = emu_list_create(); 
    102103 
    103         struct emu_env_w32 *env = NULL; 
     104        struct emu_env *env = NULL; 
    104105 
    105106        while ( !emu_queue_empty(eq) ) 
     
    116117                        emu_memory_clear(mem); 
    117118                        if (env) 
    118                                 emu_env_w32_free(env); 
     119                                emu_env_free(env); 
    119120 
    120121                        /* write the code to the offset */ 
    121122                        emu_memory_write_block(mem, STATIC_OFFSET, data, datasize); 
    122123                         
    123                         env = emu_env_w32_new(e); 
     124                        env = emu_env_new(e); 
     125 
    124126                        /* set the registers to the initial values */ 
    125127                        int reg; 
     
    143145                        eipsave = emu_cpu_eip_get(cpu); 
    144146 
    145                         struct emu_env_w32_dll_export *dllhook = NULL; 
    146  
    147                         dllhook = emu_env_w32_eip_check(env); 
    148  
    149  
    150                         if ( dllhook != NULL ) 
     147                        struct emu_env_hook *hook = NULL; 
     148 
     149                        hook = emu_env_w32_eip_check(env); 
     150 
     151 
     152                        if ( hook != NULL ) 
    151153                        { 
    152                                 if ( dllhook->fnhook == NULL ) 
     154                                if ( hook->hook.win->fnhook == NULL ) 
    153155                                        break; 
    154156                        } 
  • libemu/trunk/src/environment/linux/emu_env_linux.c

    r1526 r1607  
    3131 
    3232 
     33#include "emu/environment/emu_env.h" 
    3334#include "emu/environment/emu_profile.h" 
    3435#include "emu/environment/linux/emu_env_linux.h" 
     
    4647        eel->syscall_hooks_by_name = emu_hashtable_new(256, dll_export_fnname_hash,  dll_export_fnname_cmp); 
    4748        int i; 
    48         eel->syscall_hooks = malloc(sizeof(syscall_hooks)); 
    49         memcpy(eel->syscall_hooks, syscall_hooks, sizeof(syscall_hooks)); 
     49        eel->syscall_hookx = malloc(sizeof(syscall_hooks)); 
     50        eel->hooks = malloc(sizeof(struct emu_env_hook)*(sizeof(syscall_hooks)/sizeof(struct emu_env_linux_syscall))); 
     51        memcpy(eel->syscall_hookx, syscall_hooks, sizeof(syscall_hooks)); 
     52 
    5053        for (i=0;i<sizeof(syscall_hooks)/sizeof(struct emu_env_linux_syscall);i++) 
    5154        { 
    52                 emu_hashtable_insert(eel->syscall_hooks_by_name, (void *)eel->syscall_hooks[i].name, (void *)&eel->syscall_hooks[i]); 
     55                eel->hooks[i].type = emu_env_type_linux; 
     56                eel->hooks[i].hook.lin = &eel->syscall_hookx[i]; 
     57                emu_hashtable_insert(eel->syscall_hooks_by_name, (void *)eel->syscall_hookx[i].name, (void *)&eel->hooks[i]); 
    5358        } 
    5459 
    55       eel->profile = emu_profile_new(); 
     60//    eel->profile = emu_profile_new(); 
    5661 
    5762        return eel; 
     
    6267{ 
    6368        emu_hashtable_free(eel->syscall_hooks_by_name); 
    64         free(eel->syscall_hooks); 
    65         emu_profile_free(eel->profile); 
     69        free(eel->syscall_hookx); 
     70        free(eel->hooks); 
     71//      emu_profile_free(eel->profile); 
    6672        free(eel); 
    6773} 
    6874 
    69 struct emu_env_linux_syscall *emu_env_linux_syscall_check(struct emu_env_linux *env) 
     75struct emu_env_hook *emu_env_linux_syscall_check(struct emu_env *env) 
    7076{ 
    71         struct emu_cpu *cpu = emu_cpu_get(env->emu); 
     77        struct emu_cpu *cpu = emu_cpu_get(env->env.lin->emu); 
    7278 
    7379        if ( cpu->instr.is_fpu == false  
     
    8692                        if ( env_linux_syscalls[callnum].fnhook != NULL ) 
    8793                        { 
    88                                 name = env_linux_syscalls[callnum].fnhook(env); 
     94                                name = env_linux_syscalls[callnum].fnhook(env->env.lin); 
    8995                        } 
    9096 
    9197                        if ( name != NULL ) 
    9298                        { 
    93                                 struct emu_hashtable_item *ehi = emu_hashtable_search(env->syscall_hooks_by_name, (void *)name); 
     99                                struct emu_hashtable_item *ehi = emu_hashtable_search(env->env.lin->syscall_hooks_by_name, (void *)name); 
    94100                                if ( ehi != NULL ) 
    95101                                { 
    96                                         return(struct emu_env_linux_syscall *) ehi->value; 
     102                                        return (struct emu_env_hook *)ehi->value; 
    97103                                } 
    98104                        } 
     
    104110} 
    105111 
    106 int32_t emu_env_linux_syscall_hook(struct emu_env_linux *env, const char *syscallname, 
    107                                                                    int32_t             (*fnhook)(struct emu_env_linux *env, struct emu_env_linux_syscall *syscallfox), 
     112int32_t emu_env_linux_syscall_hook(struct emu_env *env, const char *syscallname, 
     113                                                                   uint32_t (*userhook)(struct emu_env *env, struct emu_env_hook *hook, ...), 
    108114                                                                   void *userdata) 
    109115{ 
    110116 
    111         struct emu_hashtable_item *ehi = emu_hashtable_search(env->syscall_hooks_by_name, (void *)syscallname); 
     117        struct emu_hashtable_item *ehi = emu_hashtable_search(env->env.lin->syscall_hooks_by_name, (void *)syscallname); 
    112118        if (ehi != NULL) 
    113119        { 
    114                 struct emu_env_linux_syscall *syscall = (struct emu_env_linux_syscall *)ehi->value; 
    115                 syscall->fnhook = fnhook; 
    116                 syscall->userdata = userdata; 
     120 
     121                struct emu_env_hook *hook = (struct emu_env_hook *)ehi->value; 
     122                hook->hook.lin->userhook = userhook; 
     123                hook->hook.lin->userdata = userdata; 
    117124                return 0; 
    118125        } 
     
    120127        return -1; 
    121128} 
     129 
    122130 
    123131const char *env_linux_socketcall(struct emu_env_linux *env) 
  • libemu/trunk/src/environment/linux/env_linux_syscall_hooks.c

    r1531 r1607  
    3232#include <sys/types.h> 
    3333#include <sys/socket.h> 
     34#include <stdlib.h> 
    3435 
    3536 
     
    3839#include "emu/emu_memory.h" 
    3940#include "emu/emu_string.h" 
     41#include "emu/environment/emu_env.h" 
    4042#include "emu/environment/emu_profile.h" 
    4143#include "emu/environment/linux/emu_env_linux.h" 
    4244 
    43 int32_t env_linux_hook_exit(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall
     45int32_t env_linux_hook_exit(struct emu_env *env, struct emu_env_hook *hook
    4446{ 
    4547        printf("sys_exit(2)\n"); 
     
    4850        emu_profile_argument_add_ptr(env->profile, "int", "status", c->reg[ebx]); 
    4951 
    50         emu_cpu_reg32_set(c, eax, 0); 
    51         return 0; 
    52 
    53  
    54 int32_t env_linux_hook_fork(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall) 
     52        if (hook->hook.lin->userhook != NULL) 
     53        { 
     54                uint32_t r = hook->hook.lin->userhook(env, hook, c->reg[ebx]); 
     55                emu_cpu_reg32_set(c, eax, r); 
     56        }else 
     57                emu_cpu_reg32_set(c, eax, 0); 
     58 
     59         
     60        return 0; 
     61
     62 
     63int32_t env_linux_hook_fork(struct emu_env *env, struct emu_env_hook *hook) 
    5564{ 
    5665        printf("sys_fork(2)\n"); 
     
    6271} 
    6372 
    64 int32_t env_linux_hook_execve(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall
     73int32_t env_linux_hook_execve(struct emu_env *env, struct emu_env_hook *hook
    6574{ 
    6675        printf("execve\n"); 
     
    115124 
    116125 
    117 int32_t env_linux_hook_dup2(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall
     126int32_t env_linux_hook_dup2(struct emu_env *env, struct emu_env_hook *hook
    118127{ 
    119128        struct emu_cpu *c = emu_cpu_get(env->emu); 
     
    131140 
    132141 
    133 int32_t env_linux_hook_socketcall(struct emu_env_linux *env, struct emu_env_linux_syscall *syscall
     142int32_t env_linux_hook_socketcall(struct emu_env *env, struct emu_env_hook *hook
    134143{ 
    135144        struct emu_cpu *c = emu_cpu_get(env->emu); 
     
    147156                emu_memory_read_dword(emu_memory_get(c->emu),c->reg[ecx]+4*i,a+i); 
    148157        } 
     158 
     159        uint32_t returnvalue = 0; 
    149160 
    150161        switch ( c->reg[ebx] ) 
     
    160171                emu_profile_argument_add_int(env->profile, "int", "protocol",   a[2]); 
    161172 
    162                 emu_profile_function_returnvalue_int_set(env->profile, "int", 4); 
    163                 emu_cpu_reg32_set(c, eax, 4); 
     173                 
     174                if (hook->hook.lin->userhook != NULL) 
     175                        returnvalue = hook->hook.lin->userhook(env, hook, a[0], a[1],  a[2]); 
     176                else 
     177                        returnvalue = 14; 
     178                emu_profile_function_returnvalue_int_set(env->profile, "int", returnvalue); 
     179                emu_cpu_reg32_set(c, eax, returnvalue); 
     180 
    164181                break; 
    165182 
     
    208225 
    209226 
     227                        if (hook->hook.lin->userhook != NULL) 
     228                                returnvalue = hook->hook.lin->userhook(env, hook, a[0], &sa,  a[2]); 
     229                        else 
     230                                returnvalue = 0; 
     231                        emu_profile_function_returnvalue_int_set(env->profile, "int", returnvalue); 
     232                        emu_cpu_reg32_set(c, eax, returnvalue); 
     233 
    210234                } 
    211                 emu_profile_function_returnvalue_int_set(env->profile, "int", 0); 
    212                 emu_cpu_reg32_set(c, eax, 0); 
     235 
     236 
    213237                break; 
    214238 
     
    248272 
    249273 
     274                        if (hook->hook.lin->userhook != NULL) 
     275                                returnvalue = hook->hook.lin->userhook(env, hook, a[0], &sa,  a[2]); 
     276                        else 
     277                                returnvalue = 0; 
     278 
     279                        emu_profile_function_returnvalue_int_set(env->profile, "int", returnvalue); 
     280                        emu_cpu_reg32_set(c, eax, returnvalue); 
    250281                } 
    251                 emu_profile_function_returnvalue_int_set(env->profile, "int", 0); 
    252                 emu_cpu_reg32_set(c, eax, 0); 
    253282                break; 
    254283 
     
    260289                emu_profile_argument_add_int(env->profile, "int", "s", a[0]); 
    261290                emu_profile_argument_add_int(env->profile, "int", "backlog", a[1]); 
    262                 emu_profile_function_returnvalue_int_set(env->profile, "int", 0); 
    263                 emu_cpu_reg32_set(c, eax, 0); 
     291 
     292                if (hook->hook.lin->userhook != NULL) 
     293                        returnvalue = hook->hook.lin->userhook(env, hook, a[0], a[1]); 
     294                else 
     295                        returnvalue = 0; 
     296 
     297                emu_profile_function_returnvalue_int_set(env->profile, "int", returnvalue); 
     298                emu_cpu_reg32_set(c, eax, returnvalue); 
     299 
    264300                break; 
    265301 
     
    283319                emu_profile_argument_add_none(env->profile); 
    284320 
    285  
    286                 emu_profile_function_returnvalue_int_set(env->profile, "int", 112); 
    287                 emu_cpu_reg32_set(c, eax, 112); 
     321                if (hook->hook.lin->userhook != NULL) 
     322                        returnvalue = hook->hook.lin->userhook(env, hook, a[0], &sa, a[2]); 
     323                else 
     324                        returnvalue = 0; 
     325 
     326                emu_profile_function_returnvalue_int_set(env->profile, "int", returnvalue); 
     327                emu_cpu_reg32_set(c, eax, returnvalue); 
    288328                break; 
    289329 
  • libemu/trunk/src/environment/win32/emu_env_w32.c

    r1531 r1607  
    3434#include "emu/emu_memory.h" 
    3535#include "emu/emu_hashtable.h" 
     36#include "emu/environment/emu_env.h" 
    3637#include "emu/environment/emu_profile.h" 
    3738#include "emu/environment/win32/emu_env_w32.h" 
     
    157158        struct emu_env_w32 *env = (struct emu_env_w32 *)malloc(sizeof(struct emu_env_w32)); 
    158159        memset(env,0,sizeof(struct emu_env_w32)); 
    159       env->profile = emu_profile_new(); 
     160//    env->profile = emu_profile_new(); 
    160161        env->emu = e; 
    161162        // write TEB and linklist 
     
    197198 
    198199        // map kernel32.dll to emu's memory at 0x7c800000 
    199         if (emu_env_w32_load_dll(env,"kernel32.dll") == -1
     200        if (emu_env_w32_load_dll(env,"kernel32.dll") == -1
    200201    { 
    201202                free(env); 
     
    215216        } 
    216217        free(env->loaded_dlls); 
    217       emu_profile_free(env->profile); 
     218//    emu_profile_free(env->profile); 
    218219        free(env); 
    219220 
     
    272273 
    273274 
    274 struct emu_env_w32_dll_export *emu_env_w32_eip_check(struct emu_env_w32 *env) 
     275struct emu_env_hook *emu_env_w32_eip_check(struct emu_env *env) 
    275276{ 
    276277        uint32_t eip = emu_cpu_eip_get(emu_cpu_get(env->emu)); 
    277278 
    278279        int numdlls=0; 
    279         while ( env->loaded_dlls[numdlls] != NULL ) 
     280        while ( env->env.win->loaded_dlls[numdlls] != NULL ) 
    280281        { 
    281282/*              printf("0x%08x %s 0x%08x - 0x%08x \n", 
     
    285286                           env->loaded_dlls[numdlls]->baseaddr + env->loaded_dlls[numdlls]->imagesize); 
    286287*/ 
    287                 if ( eip > env->loaded_dlls[numdlls]->baseaddr &&  
    288                          eip < env->loaded_dlls[numdlls]->baseaddr + env->loaded_dlls[numdlls]->imagesize ) 
     288                if ( eip > env->env.win->loaded_dlls[numdlls]->baseaddr &&  
     289                         eip < env->env.win->loaded_dlls[numdlls]->baseaddr + env->env.win->loaded_dlls[numdlls]->imagesize ) 
    289290                { 
    290                         logDebug(env->emu, "eip %08x is within %s\n",eip, env->loaded_dlls[numdlls]->dllname); 
    291                         struct emu_env_w32_dll *dll = env->loaded_dlls[numdlls]; 
     291                        logDebug(env->env.win->emu, "eip %08x is within %s\n",eip, env->env.win->loaded_dlls[numdlls]->dllname); 
     292                        struct emu_env_w32_dll *dll = env->env.win->loaded_dlls[numdlls]; 
    292293 
    293294                        struct emu_hashtable_item *ehi = emu_hashtable_search(dll->exports_by_fnptr, (void *)(eip - dll->baseaddr)); 
     
    299300                        } 
    300301 
    301                         struct emu_env_w32_dll_export *ex = (struct emu_env_w32_dll_export *)ehi->value; 
    302  
    303                         if ( ex->fnhook != NULL ) 
     302                 
     303                        struct emu_env_hook *hook = (struct emu_env_hook *)ehi->value; 
     304 
     305                        if ( hook->hook.win->fnhook != NULL ) 
    304306                        { 
    305                                 ex->fnhook(env, ex); 
    306                                 return ex
     307                                hook->hook.win->fnhook(env, hook); 
     308                                return hook
    307309                        } 
    308310                        else 
    309311                        { 
    310                                 logDebug(env->emu, "unhooked call to %s\n", ex->fnname); 
    311                                 return ex
     312                                logDebug(env->emu, "unhooked call to %s\n", hook->hook.win->fnname); 
     313                                return hook
    312314                        } 
    313315                } 
     
    318320} 
    319321 
    320 int32_t emu_env_w32_export_hook(struct emu_env_w32 *env, 
    321                                                                 const char *dllname, 
     322int32_t emu_env_w32_export_hook(struct emu_env *env, 
    322323                                                                const char *exportname,  
    323                                                                 int32_t                (*fnhook)(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex), 
     324                                                                uint32_t               (*fnhook)(struct emu_env *env, struct emu_env_hook *hook, ...), 
    324325                                                                void *userdata) 
    325326{ 
    326327        int numdlls=0; 
    327         while ( env->loaded_dlls[numdlls] != NULL ) 
     328        while ( env->env.win->loaded_dlls[numdlls] != NULL ) 
    328329        { 
    329                 if (dllname == NULL || strncasecmp(env->loaded_dlls[numdlls]->dllname, dllname, strlen(env->loaded_dlls[numdlls]->dllname)) == 0) 
     330                if (1)//dllname == NULL || strncasecmp(env->loaded_dlls[numdlls]->dllname, dllname, strlen(env->loaded_dlls[numdlls]->dllname)) == 0) 
    330331                { 
    331                         struct emu_hashtable_item *ehi = emu_hashtable_search(env->loaded_dlls[numdlls]->exports_by_fnname, (void *)exportname); 
     332                        struct emu_hashtable_item *ehi = emu_hashtable_search(env->env.win->loaded_dlls[numdlls]->exports_by_fnname, (void *)exportname); 
    332333                        if (ehi != NULL) 
    333334                        { 
    334                                 struct emu_env_w32_dll_export *ex = (struct emu_env_w32_dll_export *)ehi->value; 
    335                                 ex->fnhook = fnhook; 
    336                                 ex->userdata = userdata; 
     335                                printf("hooked %s\n",  exportname); 
     336                                struct emu_env_hook *hook = (struct emu_env_hook *)ehi->value; 
     337                                hook->hook.win->userhook = fnhook; 
     338                                hook->hook.win->userdata = userdata; 
    337339                                return 0; 
    338340                        } 
  • libemu/trunk/src/environment/win32/emu_env_w32_dll.c

    r1317 r1607  
    3030 
    3131#include "emu/emu.h" 
     32#include "emu/environment/emu_env.h" 
    3233#include "emu/environment/win32/emu_env_w32_dll.h" 
    3334#include "emu/environment/win32/emu_env_w32_dll_export.h" 
     
    4546        emu_hashtable_free(dll->exports_by_fnptr); 
    4647        emu_hashtable_free(dll->exports_by_fnname); 
    47         free(dll->exports); 
     48        free(dll->exportx); 
     49        free(dll->hooks); 
    4850        free(dll->dllname); 
    4951        free(dll); 
     
    106108        size = i; 
    107109 
    108         to->exports = (struct emu_env_w32_dll_export *)malloc(sizeof(struct emu_env_w32_dll_export) * size); 
    109         memcpy(to->exports, from, sizeof(struct emu_env_w32_dll_export) * size); 
     110        to->exportx = malloc(sizeof(struct emu_env_w32_dll_export) * size); 
     111        to->hooks = malloc(sizeof(struct emu_env_hook) * size); 
     112        memcpy(to->exportx, from, sizeof(struct emu_env_w32_dll_export) * size); 
     113 
    110114 
    111115        to->exports_by_fnptr = emu_hashtable_new(size, dll_export_fnptr_hash, dll_export_fnptr_cmp); 
     
    114118        for (i=0;from[i].fnname != 0; i++) 
    115119        { 
    116                 struct emu_env_w32_dll_export *ex = &to->exports[i]; 
    117                 emu_hashtable_insert(to->exports_by_fnptr, (void *)from[i].virtualaddr, ex); 
    118                 emu_hashtable_insert(to->exports_by_fnname, (void *)from[i].fnname, ex); 
     120                struct emu_env_w32_dll_export *ex = &to->exportx[i]; 
     121                struct emu_env_hook *hook = &to->hooks[i]; 
     122                hook->type = emu_env_type_win32; 
     123                hook->hook.win = ex; 
     124 
     125                emu_hashtable_insert(to->exports_by_fnptr, (void *)from[i].virtualaddr, hook); 
     126                emu_hashtable_insert(to->exports_by_fnname, (void *)from[i].fnname, hook); 
    119127        } 
    120128} 
  • libemu/trunk/src/environment/win32/env_w32_dll_export_kernel32_hooks.c

    r1531 r1607  
    4949#include "emu/emu_hashtable.h" 
    5050#include "emu/emu_string.h" 
     51#include "emu/environment/emu_env.h" 
    5152#include "emu/environment/emu_profile.h" 
    5253#include "emu/environment/win32/emu_env_w32.h" 
     
    5657 
    5758 
    58 int32_t env_w32_hook_CloseHandle(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex
     59int32_t env_w32_hook_CloseHandle(struct emu_env *env, struct emu_env_hook *hook
    5960{ 
    6061        printf("Hook me Captain Cook!\n"); 
     
    8586 
    8687 
    87 int32_t env_w32_hook_CreateFileA(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex
     88int32_t env_w32_hook_CreateFileA(struct emu_env *env, struct emu_env_hook *hook
    8889{ 
    8990        printf("Hook me Captain Cook!\n"); 
     
    145146 
    146147 
    147 int32_t env_w32_hook_CreateProcessA(struct emu_env_w32 *env, struct emu_env_w32_dll_export *ex
     148int32_t env_w32_hook_CreateProcessA(struct emu_env *env, struct emu_env_hook *hook
    148149{ 
    149150        printf("Hook me Captain Cook!\n"); 
     
    272273 
    273274 
    274  
    275