/* Query.cpp : demonstrates the use of Win32 API functions VirtualQuery() and VirtualQueryEx(). Author : Mithun Shanbhag, mrshanbh@syr.edu */ /* Version History =============== Version 1.0 : 07/20/2004 - First Release Planned Modifications ===================== - Make a module to retrive Handles / IDs of running processes. using tl32help api. */ #include #include #include #include #include #include // istringstream using namespace std ; //----< outputs string in Heading format >---- void Title ( const std::string & str ) { for ( int i = 0 ; i < ( int ) str.length() ; i ++ ) printf ( "-" ) ; printf ( "\n" ) ; printf ( str.c_str() ) ; printf ( "\n" ) ; for ( int i = 0 ; i < ( int ) str.length() ; i ++ ) printf ( "-" ) ; printf ( "\n\n" ) ; } //----< Hex Dump of Memory >---- void DisplayInHex ( DWORD dwAddress, DWORD dwDataSize ) { DWORD diff = dwAddress % 16 ; unsigned char * pAddress = ( diff == 0 ) ? (unsigned char *) (dwAddress) : (unsigned char *) (dwAddress - diff) ; cout << "\n" ; std::ostringstream os ("") ; os << "Memory Dump at 0x" ; os << hex << dwAddress ; Title ( os.str() ) ; bool flag = true ; unsigned char Buffer [16] ; SIZE_T nSize = 16 ; SIZE_T BytesRead ; int iterations = 0 ; while ( ( flag ) && ( iterations < dwDataSize / 16 ) ) { if ( ReadProcessMemory ( GetCurrentProcess(), /* view of the PE is mapped into current process */ pAddress, /* pointer to the base address to read from */ Buffer, /* pointer to a buffer that receives the contents */ nSize, /* number of bytes to be read */ & BytesRead /* actual number of bytes transferred */ ) ) { /* offset display */ //////////////////// cout << setw(15) << left << hex << showbase << (DWORD) ((DWORD)pAddress) << noshowbase ; /* the actual memory dump */ ////////////////////////////////// for ( int index = 0 ; index < (int) nSize ; ++index ) { if ( Buffer[index] < 0xA ) cout << "0" << setw(2) << left << hex << (unsigned int) Buffer[index] ; else cout << setw(3) << left << hex << (unsigned int) Buffer[index] ; if ( index == nSize/2 - 1 ) /* a padding after every 8 bytes */ cout << setw(2) << left << " " ; } cout << setw(10) << left << " " ; /* the charcters */ /////////////////// for ( int index = 0 ; index < (int) nSize ; ++index ) { if (isprint(Buffer[index])) cout << Buffer[index] ; else cout << '.' ; } /* increments for next iteration */ /////////////////////////////////// pAddress = (unsigned char *) (pAddress + 0x10) ; ++ iterations ; cout << "\n" ; } else { flag = false ; if ( iterations == 0 ) cout << "Error : Unable to display memory dump !\n\n" ; } } cout << "\n" ; } //----< process entry >---- void main ( int argc, char * argv[] ) { /* a little command-line check */ ///////////////////////////////// if ( argc != 3 ) { cout << "Syntax Error ! \n" << "Usage : memdisp
\n" ; return ; } DWORD dwAddr = (DWORD) atol( (const char *) argv[1] ) ; DWORD dwCnt = (DWORD) atol( (const char *) argv[2] ) ; // DisplayInHex ( 0x0041d660, 1024 ) ; DisplayInHex ( dwAddr, dwCnt ) ; int dummy = 0 ; }