简体   繁体   中英

C++ Signature Scanner output wrong

I'm making a Signature Scanner in c++, and to test it out I am injecting into steam and comparing the first 10 bytes.

#define sig "\x4D\x5A\x90\x00\x03\x00\x00\x00\x04\x00\x00"

When I arrived to the 3rd byte x90, the output I am getting was:

steam: \x4D\x5A\x90\x00\x03\x00\x00\x00\x04\x00\x00
sig: \x4D\x5A\xFFFFFF90\x00\x03\x00\x00\x00\x04\x00\x00

Here is my entire source:

#include <Windows.h>
#include <stdio.h>
#include <Psapi.h>
#define sig "\x4D\x5A\x90\x00\x03\x00\x00\x00\x04\x00"

#pragma comment( lib, "Psapi" )

HANDLE thread = NULL;

DWORD getImageSize( MEMORY_BASIC_INFORMATION &memInfo )
{
if( !VirtualQuery( GetModuleHandleA(NULL), &memInfo, sizeof( memInfo ) ) )
    return 0x00;

IMAGE_DOS_HEADER *dosHeader     = (IMAGE_DOS_HEADER *)memInfo.AllocationBase;
IMAGE_NT_HEADERS *peHeader      = (IMAGE_NT_HEADERS *)((unsigned long)dosHeader + (unsigned long)dosHeader->e_lfanew);

if( peHeader->Signature != IMAGE_NT_SIGNATURE )
    return 0x00;

return peHeader->OptionalHeader.SizeOfImage;
}

DWORD scan( HANDLE hProc, const DWORD length = 0x200, DWORD base = 0x00 )
{   
  if(!base )
    return base;

if( AllocConsole() )
    AttachConsole( GetCurrentProcessId() );

freopen( "CON", "w", stdout );

SetConsoleTitleW( L"Test Console" );

MEMORY_BASIC_INFORMATION memInfo;
int sigLength = sizeof( sig );

const DWORD size = getImageSize( memInfo );
unsigned char *pBase;
pBase = (unsigned char *)base;

printf( "Size = %d\n", size );

if( !size )
    return size;

for( int i(0); i < size; i++ )
{
    printf( "\\x%02X", pBase[i] );
    printf( "\\x%02X", (unsigned char *)sig[i] );

    if( i && (i % 10 == 0))
        system( "PAUSE" );

    if( pBase[i] == sig[i] )
    {
        for( int j(i), k(0); k < sigLength; j++, k++ )
        {
            if( sig[k] == '?' )
                continue;

            if( pBase[j] != sig[k] )
                break;

            if( j == (sigLength-1) )
                return (DWORD)&pBase[i];
        }
    }
}

printf( "\n" );

system( "PAUSE" );

return 0x00;
}

BOOL WINAPI DllMain ( HMODULE hModule, DWORD dwReason, LPVOID lpReserved )
{
if( dwReason == DLL_PROCESS_ATTACH )
{
    DWORD base = (DWORD)GetModuleHandleA( NULL );
    scan( GetCurrentProcess(), 0x200, base );
}

return TRUE;
}

The only thing I could think of is that the value is being seen as signed, but doesn't %02X make it unsigned?

Thanks in advance.

The format specifier, %02X , says to print the value using uppercase hex letters. Nothing about signed or unsigned.

If you want unsigned, you can use the argument h or hh , as in the printf format specifiers.

I personally would cast the argument to unsigned in the call to {f}printf.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM