Skip to content

Commit

Permalink
add memory_search_option
Browse files Browse the repository at this point in the history
  • Loading branch information
KenjiroIchise committed May 20, 2019
1 parent a65e72f commit 16faaaa
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 50 deletions.
216 changes: 166 additions & 50 deletions Cheat Engine/ceserver/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -2280,66 +2280,123 @@ int ReadProcessMemoryDebug(HANDLE hProcess, PProcessData p, void *lpAddress, voi

// printf("After WaitForDebugEventNative (tid=%d)\n", event.threadid);
}
int inflooptest=0;

bytesread=-1;
while (bytesread==-1)
//0=>/proc/pid/mem
//1=>ptrace_peekdata
if(MEMORY_SEARCH_OPTION== 0)
{
inflooptest++;

if (inflooptest>10)
printf("FUUU");

int inflooptest=0;

bytesread=-1;
while (bytesread==-1)
{
inflooptest++;

if (inflooptest>10)
printf("FUUU");

//bytesread=pread(p->mem, buffer, size, (uintptr_t)lpAddress);

lseek64(p->mem, (uintptr_t)lpAddress, SEEK_SET);
bytesread=read(p->mem, buffer, size);

//bytesread=pread(p->mem, buffer, size, (uintptr_t)lpAddress);

if ((bytesread<0) && (errno!=EINTR))
{
/*
printf("pread failed and not due to a signal: %d (isdebugged=%d)\n", errno, isdebugged);
if (isdebugged)
{
printf("event.threadid=%d devent=%d\n", (int)event.threadid, event.debugevent);
}
printf("lpAddress=%p\n", lpAddress);
printf("size=%d\n", size);
*/
lseek64(p->mem, (uintptr_t)lpAddress, SEEK_SET);
bytesread=read(p->mem, buffer, size);

bytesread=0;

if (isdebugged)
if ((bytesread<0) && (errno!=EINTR))
{
// printf("trying to read from specific task\n");
/*
printf("pread failed and not due to a signal: %d (isdebugged=%d)\n", errno, isdebugged);
if (isdebugged)
{
printf("event.threadid=%d devent=%d\n", (int)event.threadid, event.debugevent);
}
printf("lpAddress=%p\n", lpAddress);
printf("size=%d\n", size);
*/

int f;
char mempath[255];
bytesread=0;

sprintf(mempath,"/proc/%d/task/%d/mem", p->pid, (int)event.threadid);
// printf("Opening %s\n", mempath);
f=open(mempath, O_RDONLY);
printf("f=%d\n", f);
if (f>=0)
if (isdebugged)
{
//bytesread=pread(f, buffer, size, (uintptr_t)lpAddress);
lseek64(p->mem, (uintptr_t)lpAddress, SEEK_SET);
bytesread=read(p->mem, buffer, size);
// printf("trying to read from specific task\n");

int f;
char mempath[255];

if ((bytesread<0) && (errno!=EINTR))
sprintf(mempath,"/proc/%d/task/%d/mem", p->pid, (int)event.threadid);
// printf("Opening %s\n", mempath);
f=open(mempath, O_RDONLY);
printf("f=%d\n", f);
if (f>=0)
{
// printf("Also failed on second try\n");
bytesread=0;
//bytesread=pread(f, buffer, size, (uintptr_t)lpAddress);
lseek64(p->mem, (uintptr_t)lpAddress, SEEK_SET);
bytesread=read(p->mem, buffer, size);

if ((bytesread<0) && (errno!=EINTR))
{
// printf("Also failed on second try\n");
bytesread=0;
}
close(f);
}
close(f);
}


break;
}
}
}
else
{

int offset=0;
int max=size-sizeof(long int);

long int *address = (long int *)buffer;

long int value = 0;

int is_readable = 1;

while(offset<max)
{
errno = 0;
value = ptrace(PTRACE_PEEKDATA, pid, (void*)((uintptr_t)lpAddress+offset), (void *)0);

if(errno == 0)
{
*address = value;

address++;
offset+=sizeof(long int);

bytesread+=sizeof(long int);
}
else
{
is_readable = 0;
break;
}

}

if(offset < size && is_readable)
{
errno = 0;
value = ptrace(PTRACE_PEEKDATA, pid, (void*)((uintptr_t)lpAddress+offset), (void *)0);

if(errno == 0)
{
memcpy(address,&value,size-offset);

int i = size-offset;
if(i>=0)
bytesread+=size-offset;
}

break;
}
}

Expand Down Expand Up @@ -2478,19 +2535,75 @@ int ReadProcessMemory(HANDLE hProcess, void *lpAddress, void *buffer, int size)

pid_t pid=wait(&status);

lseek64(p->mem, (uintptr_t)lpAddress, SEEK_SET);
if(MEMORY_SEARCH_OPTION == 0)
{

bread=read(p->mem, buffer, size);
lseek64(p->mem, (uintptr_t)lpAddress, SEEK_SET);

bread=read(p->mem, buffer, size);

if (bread==-1)
{
bread=0;
//printf("pread error for address %p (errno=%d) ", lpAddress, errno);
//printf("\n");
}

if (bread==-1)
{
bread=0;
//printf("pread error for address %p (errno=%d) ", lpAddress, errno);
//printf("\n");
}
else
{

int offset=0;
int max=size-sizeof(long int);

//printf("bread=%d size=%d\n", bread, size);
long int *address = (long int *)buffer;

long int value = 0;

int is_readable = 1;

while(offset<max)
{
errno = 0;
value = ptrace(PTRACE_PEEKDATA, pid, (void*)((uintptr_t)lpAddress+offset), (void *)0);

if(errno == 0)
{
*address = value;

address++;
offset+=sizeof(long int);

bread+=sizeof(long int);
}
else
{
is_readable = 0;
break;
}

}

if(offset < size && is_readable)
{
errno = 0;
value = ptrace(PTRACE_PEEKDATA, pid, (void*)((uintptr_t)lpAddress+offset), (void *)0);

if(errno == 0)
{
memcpy(address,&value,size-offset);

int i = size-offset;
if(i>=0)
bread+=size-offset;
}

}

}

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


ptrace(PTRACE_DETACH, pid,0,0);
}
Expand Down Expand Up @@ -2986,9 +3099,12 @@ HANDLE OpenProcess(DWORD pid)
sprintf(processpath,"/proc/%d/maps", pid);
p->maps=strdup(processpath);

sprintf(processpath,"/proc/%d/mem", pid);
p->mem=open(processpath, O_RDONLY);

if(MEMORY_SEARCH_OPTION == 0)
{
sprintf(processpath,"/proc/%d/mem", pid);
p->mem=open(processpath, O_RDONLY);
}


pthread_mutex_init(&p->extensionMutex, NULL);
p->hasLoadedExtension=0;
Expand Down
1 change: 1 addition & 0 deletions Cheat Engine/ceserver/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,5 @@ void initAPI();

extern pthread_mutex_t debugsocketmutex;

#define MEMORY_SEARCH_OPTION 0
#endif /* API_H_ */

0 comments on commit 16faaaa

Please sign in to comment.