Skip to content

Commit a6c120d

Browse files
committed
Check number of read/written bytes.
1 parent 3a85727 commit a6c120d

File tree

2 files changed

+8
-30
lines changed

2 files changed

+8
-30
lines changed

NativeHelper/dllmain.cpp

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ EXTERN_DLL_EXPORT VOID __stdcall Initialize(RequestFunctionPtrCallback requestCa
3131
requestFunction = requestCallback;
3232
}
3333

34-
DWORD lastError = 0;
35-
EXTERN_DLL_EXPORT DWORD __stdcall GetLastErrorCode()
36-
{
37-
return lastError;
38-
}
39-
4034
EXTERN_DLL_EXPORT BOOL __stdcall IsProcessValid(HANDLE process)
4135
{
4236
if (!process)
@@ -64,15 +58,12 @@ EXTERN_DLL_EXPORT VOID __stdcall CloseRemoteProcess(HANDLE process)
6458

6559
EXTERN_DLL_EXPORT BOOL __stdcall ReadRemoteMemory(HANDLE process, LPCVOID address, LPVOID buffer, SIZE_T size)
6660
{
67-
if (ReadProcessMemory(process, address, buffer, size, nullptr))
61+
SIZE_T numberOfBytesRead;
62+
if (ReadProcessMemory(process, address, buffer, size, &numberOfBytesRead) && size == numberOfBytesRead)
6863
{
69-
lastError = 0;
70-
7164
return TRUE;
7265
}
7366

74-
lastError = GetLastError();
75-
7667
return FALSE;
7768
}
7869

@@ -81,18 +72,18 @@ EXTERN_DLL_EXPORT BOOL __stdcall WriteRemoteMemory(HANDLE process, LPVOID addres
8172
DWORD oldProtect;
8273
if (VirtualProtectEx(process, address, size, PAGE_EXECUTE_READWRITE, &oldProtect))
8374
{
84-
if (WriteProcessMemory(process, address, buffer, size, nullptr))
75+
SIZE_T numberOfBytesWritten;
76+
if (WriteProcessMemory(process, address, buffer, size, &numberOfBytesWritten))
8577
{
8678
VirtualProtectEx(process, address, size, oldProtect, nullptr);
8779

88-
lastError = 0;
89-
90-
return TRUE;
80+
if (size == numberOfBytesWritten)
81+
{
82+
return TRUE;
83+
}
9184
}
9285
}
9386

94-
lastError = GetLastError();
95-
9687
return FALSE;
9788
}
9889

@@ -182,13 +173,7 @@ EXTERN_DLL_EXPORT VOID __stdcall EnumerateProcesses(EnumerateProcessCallback cal
182173
}
183174

184175
CloseHandle(handle);
185-
186-
lastError = 0;
187-
188-
return;
189176
}
190-
191-
lastError = GetLastError();
192177
}
193178

194179
typedef VOID(__stdcall EnumerateRemoteSectionsCallback)(LPVOID baseAddress, SIZE_T regionSize, WCHAR name[IMAGE_SIZEOF_SHORT_NAME + 1], DWORD state, DWORD protection, DWORD type, WCHAR modulePath[PATH_MAXIMUM_LENGTH]);
@@ -297,13 +282,7 @@ EXTERN_DLL_EXPORT VOID __stdcall EnumerateRemoteSectionsAndModules(HANDLE proces
297282
callbackSection(section.BaseAddress, section.RegionSize, section.Name, section.State, section.Protection, section.Type, section.ModulePath);
298283
}
299284
}
300-
301-
lastError = 0;
302-
303-
return;
304285
}
305-
306-
lastError = GetLastError();
307286
}
308287

309288
typedef VOID(__stdcall DisassembleRemoteCodeCallback)(LPVOID address, DWORD length, CHAR instruction[64]);

NativeHelper/exports.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
LIBRARY
22
EXPORTS
33
Initialize
4-
GetLastErrorCode
54
IsProcessValid
65
OpenRemoteProcess
76
CloseRemoteProcess

0 commit comments

Comments
 (0)