Skip to content

Commit

Permalink
Merge pull request #26 from ZehMatt/bindings/memory-protect
Browse files Browse the repository at this point in the history
Memory protection bindings
  • Loading branch information
ZehMatt authored Oct 23, 2021
2 parents 2e9f33e + d9d1e27 commit b3849c7
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Bindings/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,18 @@ namespace Dotx64Dbg::Native
return (int)bytesWritten;
}

static uint32_t GetProtection(duint addr)
static uint32_t GetProtection(System::UIntPtr addr, bool useCache)
{
return 0;
auto va = static_cast<duint>(addr.ToUInt64());

return Script::Memory::GetProtect(va, false, useCache);
}

static uint32_t SetProtection(duint addr, uint32_t prot)
static bool SetProtection(System::UIntPtr addr, uint32_t prot, int size)
{
return 0;
auto va = static_cast<duint>(addr.ToUInt64());

return Script::Memory::SetProtect(va, prot, (duint)size);
}

static System::UIntPtr GetBase(System::UIntPtr addr)
Expand Down
39 changes: 39 additions & 0 deletions src/Dotx64Managed/API/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public static partial class Memory
[Flags]
public enum Protection
{
Invalid = 0,
NoAccess = 0x01,
ReadOnly = 0x02,
ReadWrite = 0x04,
Expand Down Expand Up @@ -90,5 +91,43 @@ public static nuint GetBase(ulong address)
{
return GetBase((nuint)address);
}

/// <summary>
/// Gets the protection of the memory, if the cache is used this is the last queried page info.
/// It is normally safe to use the cache for performance, when the cache is used the internal
/// API will not use a syscall to determine the protection.
/// </summary>
/// <param name="address">Address of the page to query</param>
/// <param name="useCache">If this is true it will use the last queried page information</param>
/// <returns>In case of failure the result is Protection.Invalid otherwise actual protection</returns>
public static Protection GetProtection(nuint address, bool useCache)
{
return (Protection)Native.Memory.GetProtection(address, useCache);
}
public static Protection GetProtection(ulong address, bool useCache)
{
return GetProtection((nuint)address, useCache);
}

/// <summary>
/// Sets a new protection on the specified address, the address will be aligned to page
/// boundaries and sets the entire page which is by 4 KiB. This may split up
/// an existing range from the memory map.
/// Internally the size will be always aligned to a minimum of a single page, if the size
/// spans more than two pages then both pages will be modified.
/// <note>This will also update the cached protection info</note>
/// </summary>
/// <param name="address">Address of the page</param>
/// <param name="protect">New protection</param>
/// <param name="size">The size of the range</param>
/// <returns>True on success</returns>
public static bool SetProtection(nuint address, Protection protect, int size)
{
return Native.Memory.SetProtection(address, (UInt32)protect, size);
}
public static bool SetProtection(ulong address, Protection protect, int size)
{
return SetProtection((nuint)address, protect, size);
}
};
}
1 change: 1 addition & 0 deletions src/include/pluginsdk/TitanEngine/TitanEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#define UE_ENGINE_CALL_PLUGIN_DEBUG_CALLBACK 8
#define UE_ENGINE_SET_DEBUG_PRIVILEGE 9
#define UE_ENGINE_SAFE_ATTACH 10
#define UE_ENGINE_MEMBP_ALT 11

#define UE_OPTION_REMOVEALL 1
#define UE_OPTION_DISABLEALL 2
Expand Down
1 change: 1 addition & 0 deletions src/include/pluginsdk/_scriptapi_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Script
SCRIPT_EXPORT duint RemoteAlloc(duint addr, duint size);
SCRIPT_EXPORT bool RemoteFree(duint addr);
SCRIPT_EXPORT unsigned int GetProtect(duint addr, bool reserved = false, bool cache = true);
SCRIPT_EXPORT bool SetProtect(duint addr, unsigned int protect, duint size);
SCRIPT_EXPORT duint GetBase(duint addr, bool reserved = false, bool cache = true);
SCRIPT_EXPORT duint GetSize(duint addr, bool reserved = false, bool cache = true);

Expand Down
2 changes: 2 additions & 0 deletions src/include/pluginsdk/bridgemain.h
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@ typedef enum
GUI_INVALIDATE_SYMBOL_SOURCE, // param1=duint base, param2=unused
GUI_GET_CURRENT_GRAPH, // param1=BridgeCFGraphList*, param2=unused
GUI_SHOW_REF, // param1=unused, param2=unused
GUI_SELECT_IN_SYMBOLS_TAB, // param1=duint addr, param2=unused
} GUIMSG;

//GUI Typedefs
Expand Down Expand Up @@ -1378,6 +1379,7 @@ BRIDGE_IMPEXP void GuiInvalidateSymbolSource(duint base);
BRIDGE_IMPEXP void GuiExecuteOnGuiThreadEx(GUICALLBACKEX cbGuiThread, void* userdata);
BRIDGE_IMPEXP void GuiGetCurrentGraph(BridgeCFGraphList* graphList);
BRIDGE_IMPEXP void GuiShowReferences();
BRIDGE_IMPEXP void GuiSelectInSymbolsTab(duint addr);

#ifdef __cplusplus
}
Expand Down
Binary file modified src/include/pluginsdk/x32bridge.lib
Binary file not shown.
Binary file modified src/include/pluginsdk/x32dbg.lib
Binary file not shown.
Binary file modified src/include/pluginsdk/x64bridge.lib
Binary file not shown.
Binary file modified src/include/pluginsdk/x64dbg.lib
Binary file not shown.

0 comments on commit b3849c7

Please sign in to comment.