EDN Admin
Well-known member
<div style="color:black; background-color:white
<pre><span style="color:green // MemoryReader.cpp : Defines the entry point for the console application.
<span style="color:green //We will be calling functions
<span style="color:green /*
VirtualQueryEx
ReadProcessMemory
WriteProcessMemory
*/
#include <span style="color:#a31515 "stdafx.h"
#include <span style="color:#a31515 "windows.h"
#include<stdio.h>
<span style="color:blue typedef <span style="color:blue struct _MEMBLOCK{
HANDLE hProc;
<span style="color:blue unsigned <span style="color:blue char *addr;
<span style="color:blue int size;
<span style="color:blue unsigned <span style="color:blue char *buffer;
<span style="color:blue struct _MEMBLOCK *next;
}MEMBLOCK;
MEMBLOCK* create_memblock(HANDLE hProc, MEMORY_BASIC_INFORMATION *meminfo)
{
MEMBLOCK *mb = (MEMBLOCK*)malloc(<span style="color:blue sizeof(MEMBLOCK));
<span style="color:blue if(mb)
{
mb->hProc = hProc;
mb->addr = (<span style="color:blue unsigned <span style="color:blue char*)meminfo->BaseAddress;
mb->size = meminfo->RegionSize;
mb->buffer = (<span style="color:blue unsigned <span style="color:blue char*)malloc(meminfo->RegionSize);
mb->next = NULL;
}
<span style="color:blue return mb;
}
<span style="color:blue void free_memblock(MEMBLOCK *mb)
{
<span style="color:blue if(mb)
{
<span style="color:blue if(mb->buffer)
{
free(mb->buffer);
}
free(mb);
}
}
MEMBLOCK* create_scan(<span style="color:blue unsigned <span style="color:blue int pid)
{
MEMBLOCK *mb_list = NULL;
MEMORY_BASIC_INFORMATION meminfo;
<span style="color:blue unsigned <span style="color:blue char *addr = 0;
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
<span style="color:blue if(hProc)
{
<span style="color:blue while(1)
{
<span style="color:blue if(VirtualQueryEx(hProc, addr, &meminfo, <span style="color:blue sizeof(meminfo)) == 0)
{
<span style="color:blue break;
}
#define WRITABLE (PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)
<span style="color:blue if((meminfo.State & MEM_COMMIT) && (meminfo.Protect & WRITABLE))
{
MEMBLOCK *mb = create_memblock(hProc, &meminfo);
<span style="color:blue if(mb)
{
mb->next = mb_list;
mb_list = mb;
}
}
addr = (<span style="color:blue unsigned <span style="color:blue char*)meminfo.BaseAddress + meminfo.RegionSize;
}
}
<span style="color:blue return mb_list;
}
<span style="color:blue void free_scan(MEMBLOCK *mb_list)
{
CloseHandle(mb_list->hProc);
<span style="color:blue while(mb_list)
{
MEMBLOCK *mb = mb_list;
mb_list = mb_list->next;
free_memblock(mb);
}
}
<span style="color:blue void dump_scan_info(MEMBLOCK *mb_list)
{
MEMBLOCK *mb = mb_list;
<span style="color:blue while(mb)
{
printf(<span style="color:#a31515 "0x%08x %drn", mb->addr, mb->size);
mb = mb->next;
}
}
<span style="color:blue int main(<span style="color:blue int argc, <span style="color:blue char *argv[])
{
<span style="color:green /**********************************************************************************************************
char* str;
char ch;
for(int i = 0; i < argc; i++){ //this are my code to try to work around an error at atoi(argv[1])
str = argv;
printf("%d%nnn", " str.length()"); //observe the use of "" in printf are very important
printf("%s%n", str);
}
for(int i = 0; i < strlen(str) ; i++){
ch = str;
if(str != NULL)
printf("we got a hit %sn, str");<span style="white-spacere //trying to find a NULL to seek if this is the one trowing the error
else
printf("%c", ch);
}
************************************************************************************************************/
MEMBLOCK *scan = create_scan( atoi(argv[1]));
<span style="color:blue if(scan)
{
dump_scan_info(scan);
free_scan(scan);
}
system(<span style="color:#a31515 "pause");
<span style="color:blue return 0;
}
[/code]
<br/>
<
oh<br/>
View the full article
<pre><span style="color:green // MemoryReader.cpp : Defines the entry point for the console application.
<span style="color:green //We will be calling functions
<span style="color:green /*
VirtualQueryEx
ReadProcessMemory
WriteProcessMemory
*/
#include <span style="color:#a31515 "stdafx.h"
#include <span style="color:#a31515 "windows.h"
#include<stdio.h>
<span style="color:blue typedef <span style="color:blue struct _MEMBLOCK{
HANDLE hProc;
<span style="color:blue unsigned <span style="color:blue char *addr;
<span style="color:blue int size;
<span style="color:blue unsigned <span style="color:blue char *buffer;
<span style="color:blue struct _MEMBLOCK *next;
}MEMBLOCK;
MEMBLOCK* create_memblock(HANDLE hProc, MEMORY_BASIC_INFORMATION *meminfo)
{
MEMBLOCK *mb = (MEMBLOCK*)malloc(<span style="color:blue sizeof(MEMBLOCK));
<span style="color:blue if(mb)
{
mb->hProc = hProc;
mb->addr = (<span style="color:blue unsigned <span style="color:blue char*)meminfo->BaseAddress;
mb->size = meminfo->RegionSize;
mb->buffer = (<span style="color:blue unsigned <span style="color:blue char*)malloc(meminfo->RegionSize);
mb->next = NULL;
}
<span style="color:blue return mb;
}
<span style="color:blue void free_memblock(MEMBLOCK *mb)
{
<span style="color:blue if(mb)
{
<span style="color:blue if(mb->buffer)
{
free(mb->buffer);
}
free(mb);
}
}
MEMBLOCK* create_scan(<span style="color:blue unsigned <span style="color:blue int pid)
{
MEMBLOCK *mb_list = NULL;
MEMORY_BASIC_INFORMATION meminfo;
<span style="color:blue unsigned <span style="color:blue char *addr = 0;
HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
<span style="color:blue if(hProc)
{
<span style="color:blue while(1)
{
<span style="color:blue if(VirtualQueryEx(hProc, addr, &meminfo, <span style="color:blue sizeof(meminfo)) == 0)
{
<span style="color:blue break;
}
#define WRITABLE (PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)
<span style="color:blue if((meminfo.State & MEM_COMMIT) && (meminfo.Protect & WRITABLE))
{
MEMBLOCK *mb = create_memblock(hProc, &meminfo);
<span style="color:blue if(mb)
{
mb->next = mb_list;
mb_list = mb;
}
}
addr = (<span style="color:blue unsigned <span style="color:blue char*)meminfo.BaseAddress + meminfo.RegionSize;
}
}
<span style="color:blue return mb_list;
}
<span style="color:blue void free_scan(MEMBLOCK *mb_list)
{
CloseHandle(mb_list->hProc);
<span style="color:blue while(mb_list)
{
MEMBLOCK *mb = mb_list;
mb_list = mb_list->next;
free_memblock(mb);
}
}
<span style="color:blue void dump_scan_info(MEMBLOCK *mb_list)
{
MEMBLOCK *mb = mb_list;
<span style="color:blue while(mb)
{
printf(<span style="color:#a31515 "0x%08x %drn", mb->addr, mb->size);
mb = mb->next;
}
}
<span style="color:blue int main(<span style="color:blue int argc, <span style="color:blue char *argv[])
{
<span style="color:green /**********************************************************************************************************
char* str;
char ch;
for(int i = 0; i < argc; i++){ //this are my code to try to work around an error at atoi(argv[1])
str = argv;
printf("%d%nnn", " str.length()"); //observe the use of "" in printf are very important
printf("%s%n", str);
}
for(int i = 0; i < strlen(str) ; i++){
ch = str;
if(str != NULL)
printf("we got a hit %sn, str");<span style="white-spacere //trying to find a NULL to seek if this is the one trowing the error
else
printf("%c", ch);
}
************************************************************************************************************/
MEMBLOCK *scan = create_scan( atoi(argv[1]));
<span style="color:blue if(scan)
{
dump_scan_info(scan);
free_scan(scan);
}
system(<span style="color:#a31515 "pause");
<span style="color:blue return 0;
}
[/code]
<br/>
<
oh<br/>
View the full article