Changeset 1661

Show
Ignore:
Timestamp:
07/21/08 00:47:21 (3 months ago)
Author:
common
Message:

libemu

  • sctest
    • implement hooks for fopen fwrite fclose CreateFile? WriteFile? CloseHandle?
      implement a nanny to make sure we do not use invalid filehandles
      add the nanny to the Makefile
      now sctest can emulate shellcodes which download files themselves, it will store the file in /tmp/<filename>-XXXXXX
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libemu/trunk/tools/sctest/Makefile.am

    r1654 r1661  
    1818sctest_SOURCES += userhooks.c 
    1919sctest_SOURCES += userhooks.h 
    20 #sctest_SOURCES += nanny.c 
    21 #sctest_SOURCES += nanny.h 
     20sctest_SOURCES += nanny.c 
     21sctest_SOURCES += nanny.h 
    2222 
  • libemu/trunk/tools/sctest/sctestmain.c

    r1654 r1661  
    9696#include "dot.h" 
    9797#include "tests.h" 
     98#include "nanny.h" 
     99 
    98100 
    99101struct run_time_options opts; 
     
    131133        struct emu_env *env = emu_env_new(e); 
    132134 
     135        struct nanny *na = nanny_new(); 
    133136//      lenv->profile = wenv->profile; 
    134137 
     
    151154        if ( opts.interactive == true ) 
    152155        { 
     156 
     157                emu_env_w32_load_dll(env->env.win,"msvcrt.dll"); 
     158                emu_env_w32_export_hook(env, "fclose", user_hook_fclose, na); 
     159                emu_env_w32_export_hook(env, "fopen", user_hook_fopen, na); 
     160                emu_env_w32_export_hook(env, "fwrite", user_hook_fwrite, na); 
     161 
    153162                emu_env_w32_export_hook(env, "CreateProcessA", user_hook_CreateProcess, NULL); 
    154163                emu_env_w32_export_hook(env, "WaitForSingleObject", user_hook_WaitForSingleObject, NULL); 
     164                emu_env_w32_export_hook(env, "CreateFileA", user_hook_CreateFile, na); 
     165                emu_env_w32_export_hook(env, "WriteFile", user_hook_WriteFile, na); 
     166                emu_env_w32_export_hook(env, "CloseHandle", user_hook_CloseHandle, na); 
     167 
    155168 
    156169                emu_env_w32_load_dll(env->env.win,"ws2_32.dll"); 
     
    159172                emu_env_w32_export_hook(env, "closesocket", user_hook_closesocket, NULL); 
    160173                emu_env_w32_export_hook(env, "connect", user_hook_connect, NULL); 
    161                 emu_env_w32_export_hook(env, "fclose", user_hook_fclose, NULL); 
    162                 emu_env_w32_export_hook(env, "fopen", user_hook_fopen, NULL); 
    163                 emu_env_w32_export_hook(env, "fwrite", user_hook_fwrite, NULL); 
    164174 
    165175                emu_env_w32_export_hook(env, "listen", user_hook_listen, NULL); 
     
    402412 
    403413        emu_profile_debug(env->profile); 
    404         emu_profile_dump(env->profile, opts.profile_file); 
     414 
     415        if (opts.profile_file) 
     416                emu_profile_dump(env->profile, opts.profile_file); 
    405417 
    406418        if (eh != NULL) 
  • libemu/trunk/tools/sctest/userhooks.c

    r1653 r1661  
    8686#include "userhooks.h" 
    8787#include "options.h" 
     88#include "nanny.h" 
    8889 
    8990#include <stdint.h> 
     
    340341        printf("Hook me Captain Cook!\n"); 
    341342        printf("%s:%i %s\n",__FILE__,__LINE__,__FUNCTION__); 
    342         return 0; 
     343//int fclose(FILE *fp); 
     344 
     345        va_list vl; 
     346        va_start(vl, hook); 
     347        FILE *f = va_arg(vl, FILE *); 
     348        va_end(vl); 
     349 
     350        struct nanny_file *nf = nanny_get_file(hook->hook.win->userdata, (uint32_t)f); 
     351 
     352        if (nf != NULL) 
     353        { 
     354                FILE *f = nf->real_file; 
     355                nanny_del_file(hook->hook.win->userdata, (uint32_t)f); 
     356        return fclose(f); 
     357        } 
     358        else  
     359                return 0; 
     360 
    343361} 
    344362 
     
    348366        printf("Hook me Captain Cook!\n"); 
    349367        printf("%s:%i %s\n",__FILE__,__LINE__,__FUNCTION__); 
    350         return 0; 
     368 
     369        va_list vl; 
     370        va_start(vl, hook); 
     371 
     372        char *filename                  = va_arg(vl,  char *); 
     373        /*char *mode                            = */(void)va_arg(vl,  char *); 
     374        va_end(vl); 
     375 
     376 
     377        char *localfile; 
     378        asprintf(&localfile, "/tmp/%s-XXXXXX",filename); 
     379        int fd = mkstemp(localfile); 
     380        close(fd); 
     381 
     382        FILE *f = fopen(localfile,"w"); 
     383 
     384        uint32_t file; 
     385        nanny_add_file(hook->hook.win->userdata, localfile, &file, f); 
     386 
     387        return file; 
    351388} 
    352389 
     
    355392        printf("Hook me Captain Cook!\n"); 
    356393        printf("%s:%i %s\n",__FILE__,__LINE__,__FUNCTION__); 
    357         return 0; 
     394 
     395/*       size_t fwrite(const void *ptr, size_t size, size_t nmemb, 
     396                     FILE *stream); 
     397*/ 
     398        va_list vl; 
     399        va_start(vl, hook); 
     400        void *data = va_arg(vl, void *); 
     401        size_t size = va_arg(vl, size_t); 
     402        size_t nmemb = va_arg(vl, size_t); 
     403        FILE *f = va_arg(vl, FILE *); 
     404        va_end(vl); 
     405 
     406        struct nanny_file *nf = nanny_get_file(hook->hook.win->userdata, (uint32_t)f); 
     407 
     408        if (nf != NULL) 
     409                return fwrite(data, size, nmemb, nf->real_file); 
     410        else  
     411                return size*nmemb; 
     412 
    358413} 
    359414 
     
    450505} 
    451506 
     507 
     508uint32_t user_hook_CreateFile(struct emu_env *env, struct emu_env_hook *hook, ...) 
     509{ 
     510        printf("Hook me Captain Cook!\n"); 
     511        printf("%s:%i %s\n",__FILE__,__LINE__,__FUNCTION__); 
     512/* 
     513HANDLE CreateFile( 
     514  LPCTSTR lpFileName, 
     515  DWORD dwDesiredAccess, 
     516  DWORD dwShareMode, 
     517  LPSECURITY_ATTRIBUTES lpSecurityAttributes, 
     518  DWORD dwCreationDisposition, 
     519  DWORD dwFlagsAndAttributes, 
     520  HANDLE hTemplateFile 
     521); 
     522*/ 
     523 
     524        va_list vl; 
     525        va_start(vl, hook); 
     526        char *lpFileName                        = va_arg(vl, char *); 
     527        /*int dwDesiredAccess           =*/(void)va_arg(vl, int); 
     528        /*int dwShareMode                       =*/(void)va_arg(vl, int); 
     529        /*int lpSecurityAttributes      =*/(void)va_arg(vl, int); 
     530        /*int dwCreationDisposition     =*/(void)va_arg(vl, int); 
     531        /*int dwFlagsAndAttributes      =*/(void)va_arg(vl, int); 
     532        /*int hTemplateFile                     =*/(void)va_arg(vl, int); 
     533        va_end(vl); 
     534 
     535        char *localfile; 
     536        asprintf(&localfile, "/tmp/%s-XXXXXX",lpFileName); 
     537        int fd = mkstemp(localfile); 
     538        close(fd); 
     539 
     540        FILE *f = fopen(localfile,"w"); 
     541 
     542        uint32_t handle; 
     543        nanny_add_file(hook->hook.win->userdata, localfile, &handle, f); 
     544 
     545        return (uint32_t)handle; 
     546} 
     547 
     548uint32_t user_hook_WriteFile(struct emu_env *env, struct emu_env_hook *hook, ...) 
     549{ 
     550        printf("Hook me Captain Cook!\n"); 
     551        printf("%s:%i %s\n",__FILE__,__LINE__,__FUNCTION__); 
     552/* 
     553BOOL WriteFile( 
     554  HANDLE hFile, 
     555  LPCVOID lpBuffer, 
     556  DWORD nNumberOfBytesToWrite, 
     557  LPDWORD lpNumberOfBytesWritten, 
     558  LPOVERLAPPED lpOverlapped 
     559); 
     560*/ 
     561 
     562        va_list vl; 
     563        va_start(vl, hook); 
     564        FILE *hFile                                     = va_arg(vl, FILE *); 
     565        void *lpBuffer                                  = va_arg(vl, void *); 
     566        int   nNumberOfBytesToWrite     = va_arg(vl, int); 
     567        /* int *lpNumberOfBytesWritten  =*/(void)va_arg(vl, int*); 
     568        /* int *lpOverlapped                =*/(void)va_arg(vl, int*); 
     569        va_end(vl); 
     570 
     571        struct nanny_file *nf = nanny_get_file(hook->hook.win->userdata, (uint32_t)hFile); 
     572 
     573        if (nf != NULL) 
     574                fwrite(lpBuffer, nNumberOfBytesToWrite, 1, nf->real_file); 
     575        else 
     576                printf("shellcode tried to write data to not existing handle\n"); 
     577 
     578        return 1; 
     579 
     580} 
     581 
     582 
     583uint32_t user_hook_CloseHandle(struct emu_env *env, struct emu_env_hook *hook, ...) 
     584{ 
     585        printf("Hook me Captain Cook!\n"); 
     586        printf("%s:%i %s\n",__FILE__,__LINE__,__FUNCTION__); 
     587/* 
     588BOOL CloseHandle( 
     589  HANDLE hObject 
     590); 
     591*/ 
     592 
     593        va_list vl; 
     594        va_start(vl, hook); 
     595        FILE *hObject = va_arg(vl, FILE *); 
     596        va_end(vl); 
     597 
     598        struct nanny_file *nf = nanny_get_file(hook->hook.win->userdata, (uint32_t)hObject); 
     599 
     600        if (nf != NULL) 
     601        { 
     602                FILE *f = nf->real_file; 
     603                nanny_del_file(hook->hook.win->userdata, (uint32_t)hObject); 
     604                fclose(f); 
     605        } 
     606        else  
     607        { 
     608                printf("shellcode tried to close not existing handle (maybe closed it already?)\n"); 
     609        } 
     610 
     611 
     612        return 0; 
     613 
     614} 
     615 
  • libemu/trunk/tools/sctest/userhooks.h

    r1653 r1661  
    1818uint32_t user_hook_WSASocket(struct emu_env *env, struct emu_env_hook *hook, ...); 
    1919 
     20uint32_t user_hook_CreateFile(struct emu_env *env, struct emu_env_hook *hook, ...); 
     21uint32_t user_hook_WriteFile(struct emu_env *env, struct emu_env_hook *hook, ...); 
     22uint32_t user_hook_CloseHandle(struct emu_env *env, struct emu_env_hook *hook, ...);