Skip to content

Commit

Permalink
add changeMemoryProtection command to ceserver
Browse files Browse the repository at this point in the history
  • Loading branch information
cheat-engine committed Oct 20, 2022
1 parent def3415 commit 84abeee
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 56 deletions.
43 changes: 43 additions & 0 deletions Cheat Engine/ceserver/ceserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include <errno.h>
#include <dlfcn.h>

#include <sys/mman.h>




Expand Down Expand Up @@ -1148,6 +1150,47 @@ case CMD_SETTHREADCONTEXT:
sendall(currentsocket, &r, sizeof(r),0);
}

break;
}

case CMD_CHANGEMEMORYPROTECTION:
{
CeChangeMemoryProtection c;
debug_log("CESERVER: CMD_CHANGEMEMORYPROTECTION \n");
if (recvall(currentsocket, &c, sizeof(c),0)>0)
{
RegionInfo ri;
uint32_t r;
uint32_t oldprotection;
uint32_t newprotection;

if (VirtualQueryEx(c.hProcess, (void*)c.address, &ri, NULL))
{
oldprotection=ri.protection;


//convert the given protection to a linux protection
newprotection=0;
switch (c.windowsprotection)
{
newprotection=0;

case PAGE_EXECUTE_READWRITE: newprotection=PROT_WRITE | PROT_READ | PROT_EXEC; break;
case PAGE_EXECUTE_READ: newprotection=PROT_READ | PROT_EXEC; break;
case PAGE_EXECUTE: newprotection=PROT_EXEC; break;
case PAGE_READWRITE: newprotection=PROT_READ | PROT_WRITE; break;
case PAGE_READONLY: newprotection=PROT_READ; break;
}

r=ext_changememoryprotection(c.hProcess, c.address, c.size, newprotection);
}
else
debug_log("Failure getting the old protection");

sendall(currentsocket, &r, sizeof(r),MSG_MORE);
sendall(currentsocket, &oldprotection, sizeof(oldprotection),0);
}

break;
}

Expand Down
10 changes: 10 additions & 0 deletions Cheat Engine/ceserver/ceserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

#define CMD_CREATETOOLHELP32SNAPSHOTEX 35

#define CMD_CHANGEMEMORYPROTECTION 36

#define CMD_AOBSCAN 200

//just in case I ever get over 255 commands this value will be reserved for a secondary command list (FF 00 - FF 01 - ... - FF FE - FF FF 01 - FF FF 02 - .....
Expand Down Expand Up @@ -241,6 +243,14 @@ typedef struct {
uint32_t result;
} CeSpeedhackSetSpeedOutput, *PCeSpeedhackSetSpeedOutput;

typedef struct {
HANDLE hProcess;
uint64_t address;
uint32_t size;
uint32_t windowsprotection;
} CeChangeMemoryProtection, *PCeChangeMemoryProtection;


typedef struct {
HANDLE hProcess;
uint64_t start;
Expand Down
116 changes: 64 additions & 52 deletions Cheat Engine/ceserver/extension/.cproject
Original file line number Diff line number Diff line change
Expand Up @@ -1111,18 +1111,24 @@

<configuration configurationName="Release-Android-x86"/>

<configuration configurationName="Release-Android-x86_old"/>

<configuration configurationName="Release-Android">

<resource resourceType="PROJECT" workspacePath="/ceserver-extension"/>

</configuration>

<configuration configurationName="Release-Android-arm"/>

<configuration configurationName="Debug-Linux">

<resource resourceType="PROJECT" workspacePath="/ceserver-extension"/>

</configuration>

<configuration configurationName="Release-Android-x86_64"/>

<configuration configurationName="Release">

<resource resourceType="PROJECT" workspacePath="/ceserver-extension"/>
Expand All @@ -1139,23 +1145,53 @@

</storageModule>

<storageModule moduleId="scannerConfiguration">

<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets">

<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.exe.release.11823139.878848786;cdt.managedbuild.config.gnu.cross.exe.release.11823139.878848786.;cdt.managedbuild.tool.gnu.cross.c.compiler.944680769;cdt.managedbuild.tool.gnu.c.compiler.input.282826520">
<buildTargets>

<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.managedbuilder.core.GCCManagedMakePerProjectProfileC"/>

</scannerConfigBuildInfo>

<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.exe.debug.1358909468.1869431552">
<target name="Debug-linux" path="Debug-linux" targetID="org.eclipse.cdt.build.MakeTargetBuilder">

<buildCommand>make</buildCommand>

<buildArguments/>

<buildTarget>Debug-linux</buildTarget>

<stopOnError>true</stopOnError>

<useDefaultCommand>true</useDefaultCommand>

<runAllBuilders>true</runAllBuilders>

</target>

<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
<target name="Debug-Linux" path="Debug-linux" targetID="org.eclipse.cdt.build.MakeTargetBuilder">

<buildCommand>make</buildCommand>

<buildArguments/>

<buildTarget>Debug-linux</buildTarget>

<stopOnError>true</stopOnError>

<useDefaultCommand>true</useDefaultCommand>

<runAllBuilders>true</runAllBuilders>

</target>

</scannerConfigBuildInfo>
</buildTargets>

</storageModule>

<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/>

<storageModule moduleId="scannerConfiguration">

<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.so.debug.1388177519;cdt.managedbuild.config.gnu.cross.so.debug.1388177519.;cdt.managedbuild.tool.gnu.cross.c.compiler.1460960264;cdt.managedbuild.tool.gnu.c.compiler.input.1714379932">
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>

<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.so.release.1593536990;cdt.managedbuild.config.gnu.cross.so.release.1593536990.;cdt.managedbuild.tool.gnu.cross.c.compiler.1906152945;cdt.managedbuild.tool.gnu.c.compiler.input.284285820">

<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.managedbuilder.core.GCCManagedMakePerProjectProfileC"/>

Expand All @@ -1181,7 +1217,19 @@

</scannerConfigBuildInfo>

<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.so.release.1593536990;cdt.managedbuild.config.gnu.cross.so.release.1593536990.;cdt.managedbuild.tool.gnu.cross.c.compiler.1906152945;cdt.managedbuild.tool.gnu.c.compiler.input.284285820">
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.exe.debug.1358909468.1869431552">

<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>

</scannerConfigBuildInfo>

<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.exe.release.11823139.878848786.59856166;cdt.managedbuild.config.gnu.cross.exe.release.11823139.878848786.59856166.;cdt.managedbuild.tool.gnu.cross.c.compiler.70616554;cdt.managedbuild.tool.gnu.c.compiler.input.128517512">

<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>

</scannerConfigBuildInfo>

<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.so.debug.1388177519;cdt.managedbuild.config.gnu.cross.so.debug.1388177519.;cdt.managedbuild.tool.gnu.cross.c.compiler.1460960264;cdt.managedbuild.tool.gnu.c.compiler.input.1714379932">

<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.managedbuilder.core.GCCManagedMakePerProjectProfileC"/>

Expand All @@ -1206,49 +1254,13 @@
</profile>

</scannerConfigBuildInfo>

</storageModule>

<storageModule moduleId="org.eclipse.cdt.make.core.buildtargets">

<buildTargets>

<target name="Debug-linux" path="Debug-linux" targetID="org.eclipse.cdt.build.MakeTargetBuilder">

<buildCommand>make</buildCommand>

<buildArguments/>

<buildTarget>Debug-linux</buildTarget>

<stopOnError>true</stopOnError>

<useDefaultCommand>true</useDefaultCommand>

<runAllBuilders>true</runAllBuilders>

</target>
<scannerConfigBuildInfo instanceId="cdt.managedbuild.config.gnu.cross.exe.release.11823139.878848786;cdt.managedbuild.config.gnu.cross.exe.release.11823139.878848786.;cdt.managedbuild.tool.gnu.cross.c.compiler.944680769;cdt.managedbuild.tool.gnu.c.compiler.input.282826520">

<target name="Debug-Linux" path="Debug-linux" targetID="org.eclipse.cdt.build.MakeTargetBuilder">

<buildCommand>make</buildCommand>

<buildArguments/>

<buildTarget>Debug-linux</buildTarget>

<stopOnError>true</stopOnError>

<useDefaultCommand>true</useDefaultCommand>

<runAllBuilders>true</runAllBuilders>

</target>
<autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.managedbuilder.core.GCCManagedMakePerProjectProfileC"/>

</buildTargets>
</scannerConfigBuildInfo>

</storageModule>

<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/>

</cproject>
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>

<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>

<provider class="org.eclipse.cdt.internal.build.crossgcc.CrossGCCBuiltinSpecsDetector" console="false" env-hash="1842147579442440790" id="org.eclipse.cdt.build.crossgcc.CrossGCCBuiltinSpecsDetector" keep-relative-paths="false" name="CDT Cross GCC Built-in Compiler Settings" parameter="${COMMAND} ${FLAGS} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">

<language-scope id="org.eclipse.cdt.core.gcc"/>

<language-scope id="org.eclipse.cdt.core.g++"/>

</provider>

</extension>

Expand Down
24 changes: 23 additions & 1 deletion Cheat Engine/ceserver/extension/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ int DispatchCommand(int currentsocket, unsigned char command)
struct {
float speed;
} params;
#pragma pack()


uint32_t result;

Expand All @@ -361,6 +361,28 @@ int DispatchCommand(int currentsocket, unsigned char command)
break;
}

case EXTCMD_CHANGEMEMORYPROTECTION:
{
#pragma pack(1)
struct {
uint64_t address;
int size;
int newprotection;
} params;
#pragma pack()

printf("EXTCMD_CHANGEMEMORYPROTECTION\n");

if (recvall(currentsocket, &params, sizeof(params), 0)>0)
{
uint32_t result;
result=mprotect((void*)params.address, params.size, params.newprotection);
sendall(currentsocket, &result, sizeof(result), 0);
}

break;
}

}
return 1;

Expand Down
2 changes: 1 addition & 1 deletion Cheat Engine/ceserver/extension/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#define EXTCMD_CREATETHREAD 2
#define EXTCMD_LOADMODULE 3
#define EXTCMD_SPEEDHACK_SETSPEED 4
//#define EXTCMD_CHANGEMEMORYPROTECTION 5
#define EXTCMD_CHANGEMEMORYPROTECTION 5


#endif /* SERVER_H_ */
50 changes: 49 additions & 1 deletion Cheat Engine/ceserver/extensionfunctions.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "extensionfunctions.h"
#include "api.h"

//todo: Make all of these fail if the debugger is waiting to continue from a debug event
//todo: Make all of these fail if the debugger is waiting to continue from a debug event (or perhaps the pipe server thread can keep running)

int ext_speedhack_setSpeed(HANDLE hProcess, float speed)
{
Expand Down Expand Up @@ -241,7 +241,55 @@ uint64_t ext_alloc(HANDLE hProcess, uint64_t preferedBase, int size)
debug_log("Returned from extension with result %llx\n", result);

}
return result;
}

int ext_changememoryprotection(HANDLE hProcess, uint64_t address, uint32_t size, uint32_t protection)
{
uint32_t result=0;

debug_log("ext_changememoryprotection(%d, %llx, %x,%x)\n", hProcess, address, size, protection);

if (GetHandleType(hProcess) == htProcesHandle )
{
PProcessData p=(PProcessData)GetPointerFromHandle(hProcess);
if (p->hasLoadedExtension==FALSE)
{
debug_log("hasLoadedExtension == FALSE");
if (loadCEServerExtension(hProcess)==FALSE)
{
debug_log("Failure to load the extension\n");
return 0;
}
}

#pragma pack(1)
struct {
uint8_t command;
uint64_t address;
uint32_t size;
uint32_t protection;
} changeProtectionCommand;
#pragma pack()


changeProtectionCommand.command=EXTCMD_CHANGEMEMORYPROTECTION;
changeProtectionCommand.address=address;
changeProtectionCommand.size=size;
changeProtectionCommand.protection=protection;

pthread_mutex_lock(&p->extensionMutex);

if (sendall(p->extensionFD, &changeProtectionCommand, sizeof(changeProtectionCommand), 0)>0)
recvall(p->extensionFD, &result, sizeof(result), 0);

pthread_mutex_unlock(&p->extensionMutex);

debug_log("Returned from extension with result %llx\n", result);



}

return result;
}
4 changes: 3 additions & 1 deletion Cheat Engine/ceserver/extensionfunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
#define EXTCMD_CREATETHREAD 2
#define EXTCMD_LOADMODULE 3
#define EXTCMD_SPEEDHACK_SETSPEED 4
//#define EXTCMD_CHANGEMEMORYPROTECTION x
#define EXTCMD_CHANGEMEMORYPROTECTION 5


uint64_t ext_alloc(HANDLE hProcess, uint64_t preferedBase, int size);
int ext_free(HANDLE hProcess, uint64_t address, int size);
uint64_t ext_createThread(HANDLE hProcess, uint64_t startaddress, uint64_t parameter);
int ext_speedhack_setSpeed(HANDLE hProcess, float speed);
uint64_t ext_loadModule(HANDLE hProcess, char *modulepath);
int ext_changememoryprotection(HANDLE hProcess, uint64_t address, uint32_t size, uint32_t protection);

#endif /* EXTENSIONFUNCTIONS_H_ */
Loading

0 comments on commit 84abeee

Please sign in to comment.