Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memory stream API for the child process #17

Merged
merged 4 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ src/Libs
/bin/dotplugins/myplugin/My Plugin.csproj
/bin/dotplugins/myplugin/obj
/src/Dotx64DbgManagedTests/obj
/src/Dotx64Managed/Properties/launchSettings.json
2 changes: 1 addition & 1 deletion bin/dotplugins/example/ExamplePlugin.Disassembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class ExamplePlugin : IPlugin, IHotload
public void DisassembleAtIP(string[] args)
{
var decoder = Decoder.Create();
var ip = Thread.Main.Rip;
var ip = Thread.Main.Nip;

var decodedInstr = decoder.Decode(ip);

Expand Down
42 changes: 42 additions & 0 deletions src/Bindings/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,33 @@ namespace Dotx64Dbg::Native
return (int)bytesWritten;
}

/// <summary>
/// Attempt to write memory to the debugged process.
/// </summary>
/// <param name="addr">Virtual address in the debugged process space</param>
/// <param name="data">The bytes to be written</param>
/// <param name="length">The maximum amount of bytes to write, can not be bigger than `data`</param>
/// <returns>The amount of bytes written</returns>
static int Write(System::UIntPtr addr, array<System::Byte>^ data, int offset, int length)
{
auto va = static_cast<duint>(addr.ToUInt64());

duint maxLength = data->Length < length ? data->Length : length;
if (maxLength <= 0)
return 0;

pin_ptr<uint8_t> ptr = &data[0];
const uint8_t* buf = ptr;

duint bytesWritten = 0;
if (!Script::Memory::Write(va, buf + offset, maxLength, &bytesWritten))
{
return 0;
}

return (int)bytesWritten;
}

static uint32_t GetProtection(duint addr)
{
return 0;
Expand All @@ -72,5 +99,20 @@ namespace Dotx64Dbg::Native
{
return 0;
}

static System::UIntPtr GetBase(System::UIntPtr addr)
{
auto va = static_cast<duint>(addr.ToUInt64());

auto base = Script::Memory::GetBase(va);
return System::UIntPtr(reinterpret_cast<void*>(base));
}

static uint64_t GetSize(System::UIntPtr addr)
{
auto va = static_cast<duint>(addr.ToUInt64());

return Script::Memory::GetSize(va);
}
};
}
92 changes: 92 additions & 0 deletions src/Dotx64Managed/API/Memory.Stream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dotx64Dbg
{
public static partial class Memory
{
public class Stream : System.IO.Stream
{
public nuint Base { get; init; }

internal long Size;
internal long Offset;

internal Stream(nuint address)
{
Base = Memory.GetBase(address);
Size = (long)Memory.GetSize(address);
Offset = (long)(address - Base);
}

public override bool CanRead => true;

public override bool CanSeek => true;

public override bool CanWrite => true;

public override long Length => (long)Size;

public override long Position
{
get => Offset;
set
{
Offset = value;
if (Offset > Size)
Offset = Size;
if (Offset < 0)
Offset = 0;
}
}

public override void Flush()
{
throw new NotSupportedException();
}

public override int Read(byte[] buffer, int offset, int count)
{
var bytesLeft = Size - Offset;
var numBytes = (int)Math.Min(bytesLeft, count);

var data = Native.Memory.Read(Base + (nuint)Offset, numBytes);
data.CopyTo(buffer, offset);

Offset += numBytes;

return numBytes;
}

public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}

public override void SetLength(long value)
{
throw new NotSupportedException();
}

public override void Write(byte[] buffer, int offset, int count)
{
var bytesLeft = Size - Offset;
var numBytes = (int)Math.Min(bytesLeft, count);
numBytes = Math.Min(numBytes, buffer.Length - offset);

Native.Memory.Write(Base + (nuint)Offset, buffer, offset, numBytes);

Offset += numBytes;
}
}

public static Stream GetStream(nuint address)
{
return new Stream(address);
}
}
}
20 changes: 19 additions & 1 deletion src/Dotx64Managed/API/Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Dotx64Dbg
{
public class Memory
public static partial class Memory
{
[Flags]
public enum Protection
Expand Down Expand Up @@ -72,5 +72,23 @@ public static int Write(ulong address, byte[] data)
{
return Write((nuint)address, data, data.Length);
}

public static nuint GetSize(nuint address)
{
return (nuint)Native.Memory.GetSize(address);
}
public static nuint GetSize(ulong address)
{
return GetSize((nuint)address);
}

public static nuint GetBase(nuint address)
{
return Native.Memory.GetBase(address);
}
public static nuint GetBase(ulong address)
{
return GetBase((nuint)address);
}
};
}
10 changes: 0 additions & 10 deletions src/Dotx64Managed/Properties/launchSettings.json

This file was deleted.