danielplohmann / apiscout

This project aims at simplifying Windows API import recovery on arbitrary memory dumps
BSD 2-Clause "Simplified" License
241 stars 41 forks source link

False Positive on DllBaseChecker[32|64] #31

Open akhribfarouk opened 3 years ago

akhribfarouk commented 3 years ago

as i said before you can avoid the False positive of the scanners by forcing the allocated memory and it worked (everytime) this is the fix (maybe it is not sofisticated but it works like a charm):


#include <windows.h>
#include <tchar.h>
#include <winbase.h>
#include <stdio.h>
#include <stdbool.h> 
// bitness check courtesy of http://stackoverflow.com/a/12338526
// Check windows
#if _WIN32 || _WIN64
#if _WIN64
#define ENV64BIT
#else
#define ENV32BIT
#endif
#endif
// Check GCC
#if __GNUC__
#if __x86_64__ || __ppc64__
#define ENV64BIT
#else
#define ENV32BIT
#endif
#endif
#define AllotOff_MEM 100000000
int main() {
    DWORD written_b = 0;
    HANDLE hStdOut = 0;
    HINSTANCE hDllBase = 0;
    LPWSTR* szArglist;
    char buffer[50];
    int nArgs;
    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
    if (nArgs > 1) {
        GetFileAttributesW(szArglist[1]); // from winbase.h
        if (INVALID_FILE_ATTRIBUTES == GetFileAttributesW(szArglist[1]) && GetLastError() == ERROR_FILE_NOT_FOUND)
        {
            char cFileNotFound[] = "DLL not found?\n";
            WriteFile(hStdOut, cFileNotFound, strlen(cFileNotFound), &written_b, 0);
            return 0;
        }
        else {
            // suppress output of popups (Entry Point not found etc.)
            UINT oldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS);
            SetErrorMode(oldErrorMode | SEM_FAILCRITICALERRORS);
            hDllBase = LoadLibraryW(szArglist[1]);

        }
    }
    else {
        char output[] = "Usage: DllBaseChecker[32|64].exe <dll_to_load>";
        WriteFile(hStdOut, output, strlen(output), &written_b, 0);
        return 0;
    }
#if defined(ENV64BIT)
    bool a = true;
    bool b = false;
    int s, l = 0;
    char * memdmp = NULL;
    //ByPassing the False Positive here because it is to much for the memory but not the CPU.
    memdmp = (char *)malloc(AllotOff_MEM);
    if (a != false)
    {
        goto zone_two;
        for (s = 0; s < 100000; s++)
        {
            l += s;
        }
    }
zone_three:
    if (memdmp != NULL)
    {
    if (sizeof(void*) != 8)
    {
        wprintf(L"ENV64BIT: Error: pointer should be 8 bytes. Exiting.");
        return 0;
        b = true;
    }
    else {
        sprintf(buffer, "DLL loaded at: 0x%llx\n", hDllBase);
        WriteFile(hStdOut, buffer, strlen(buffer), &written_b, 0);
        b = true;
    }
    }
zone_two:
    if (b != true)
    {
        for (s = 0; s < 100000; s++)
        {
            l += s;
        }
        goto zone_three;
    }
    return 0;
#elif defined (ENV32BIT)
    bool a = true;
    bool b = false;
    int s,l = 0;
    char * memdmp = NULL;
    memdmp = (char *)malloc(AllotOff_MEM);
    if (a!=false)
    {
        goto zone_two;
        for (s=0;s<100000;s++)
        {
            l += s;
        }
    }
    zone_three:
    if (memdmp != NULL)
    {
        if (sizeof(void*) != 4)
        {
            wprintf(L"ENV32BIT: Error: pointer should be 4 bytes. Exiting.");
            b = true;
            return 0;
        }
        else {
            sprintf(buffer, "DLL loaded at: 0x%x\n", (unsigned int)hDllBase);
            WriteFile(hStdOut, buffer, strlen(buffer), &written_b, 0);
            b = true;
        }
    }
zone_two:
    if (b!=true)
    {
        for (s = 0; s < 100000; s++)
        {
            l += s;
        }
        goto zone_three;
    }
#else
#error "Must define either ENV32BIT or ENV64BIT".
#endif
    return 0;
}