Skip to content

Commit

Permalink
scan result + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
phasephasephase committed Oct 5, 2024
1 parent ff3d6f4 commit 7c0cf5c
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 30 deletions.
17 changes: 15 additions & 2 deletions bindings/cs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ You can install it using the following command:
```csharp
using Hat;

// Parse a pattern's string representation into a byte array at runtime
// Parse a pattern's string representation to an array of bytes at runtime
Pattern pattern = new Pattern("48 8D 05 ? ? ? ? E8");

// Create a scanner object for a section in a specific module
Expand All @@ -33,4 +33,17 @@ Span<byte> buffer = /* ... */;
Scanner scanner = new Scanner(buffer);

// Scan for this pattern using your CPU's vectorization features
nint result = scanner.FindPattern(pattern);
ScanResult? result = scanner.FindPattern(pattern);

// Get the address pointed at by the pattern
nint address = result!.Address;

// Resolve an RIP relative address at a given offset
//
// | signature matches here
// | | relative address located at +3
// v v
// 48 8D 05 BE 53 23 01 lea rax, [rip+0x12353be]
//
nint relativeAddress = result!.Relative(3);
```
10 changes: 0 additions & 10 deletions bindings/cs/libhat-sharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,16 @@ EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EF020BDB-543C-4EA3-9C95-A3668E2E5E65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF020BDB-543C-4EA3-9C95-A3668E2E5E65}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF020BDB-543C-4EA3-9C95-A3668E2E5E65}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF020BDB-543C-4EA3-9C95-A3668E2E5E65}.Release|Any CPU.Build.0 = Release|Any CPU
{EF020BDB-543C-4EA3-9C95-A3668E2E5E65}.Debug|x64.ActiveCfg = Debug|x64
{EF020BDB-543C-4EA3-9C95-A3668E2E5E65}.Debug|x64.Build.0 = Debug|x64
{EF020BDB-543C-4EA3-9C95-A3668E2E5E65}.Release|x64.ActiveCfg = Release|x64
{EF020BDB-543C-4EA3-9C95-A3668E2E5E65}.Release|x64.Build.0 = Release|x64
{1FDDD9F4-F557-45C9-96CD-7CDF111B8176}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1FDDD9F4-F557-45C9-96CD-7CDF111B8176}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FDDD9F4-F557-45C9-96CD-7CDF111B8176}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FDDD9F4-F557-45C9-96CD-7CDF111B8176}.Release|Any CPU.Build.0 = Release|Any CPU
{1FDDD9F4-F557-45C9-96CD-7CDF111B8176}.Debug|x64.ActiveCfg = Debug|x64
{1FDDD9F4-F557-45C9-96CD-7CDF111B8176}.Debug|x64.Build.0 = Debug|x64
{1FDDD9F4-F557-45C9-96CD-7CDF111B8176}.Release|x64.ActiveCfg = Release|x64
{1FDDD9F4-F557-45C9-96CD-7CDF111B8176}.Release|x64.Build.0 = Release|x64
EndGlobalSection
EndGlobal
34 changes: 34 additions & 0 deletions bindings/cs/libhat-sharp/ScanResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Numerics;

namespace Hat;

public unsafe class ScanResult
{
/// <summary>
/// The resulting address from the pattern scan.
/// </summary>
public nint Address { get; }

internal ScanResult(nint address)
{
Address = address;
}

/// <summary>
/// Reads a value of type <typeparamref name="T"/> located at a given offset.
/// </summary>
/// <param name="offset">The offset of the value. Defaults to 0.</param>
public T Read<T>(int offset = 0) where T : unmanaged
{
return *(T*)(Address + offset);
}

/// <summary>
/// Resolves a RIP relative address located at a given offset.
/// </summary>
/// <param name="offset">The offset of the relative address.</param>
public nint Relative(int offset)
{
return Address + Read<int>(offset) + offset + sizeof(int);
}
}
16 changes: 12 additions & 4 deletions bindings/cs/libhat-sharp/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,30 @@ public Scanner(Span<byte> buffer)
_size = (uint)buffer.Length;
}

/// <summary>
/// Creates a scanner for a <see cref="Memory{T}"/> of bytes.
/// </summary>
/// <param name="buffer">The buffer to scan in.</param>
public Scanner(Memory<byte> buffer) : this(buffer.Span) { }

/// <summary>
/// Scans for a pattern.
/// </summary>
/// <param name="pattern">The pattern to scan for.</param>
/// <param name="alignment">The byte alignment of the result.</param>
/// <returns>The address of the pattern if found, otherwise, 0.</returns>
public nint FindPattern(Pattern pattern, ScanAlignment alignment = ScanAlignment.X1)
/// <returns>A <see cref="ScanResult"/> containing the result of the scan if found, otherwise null.</returns>
public ScanResult? FindPattern(Pattern pattern, ScanAlignment alignment = ScanAlignment.X1)
{
if (_buffer is not null && _size is not null)
{
return Functions.libhat_find_pattern(pattern.Signature, _buffer.Value, _size.Value, alignment);
var result = Functions.libhat_find_pattern(pattern.Signature, _buffer.Value, _size.Value, alignment);
return result == 0 ? null : new ScanResult(result);
}

if (_section is not null && _module is not null)
{
return Functions.libhat_find_pattern_mod(pattern.Signature, _module.Value, _section, alignment);
var result = Functions.libhat_find_pattern_mod(pattern.Signature, _module.Value, _section, alignment);
return result == 0 ? null : new ScanResult(result);
}

throw new InvalidOperationException("Scanner is not initialized.");
Expand Down
14 changes: 6 additions & 8 deletions bindings/cs/libhat-sharp/libhat-sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AssemblyVersion>0.1.1</AssemblyVersion>
<FileVersion>0.1.1.1</FileVersion>
<AssemblyVersion>0.1.2</AssemblyVersion>
<FileVersion>0.1.2.1</FileVersion>

<!-- nuget package -->
<PackageId>libhat-sharp</PackageId>
<Version>0.1.1</Version>
<Version>0.1.2</Version>
<Authors>phase</Authors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Title>libhat-sharp</Title>
Expand All @@ -21,18 +21,16 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/phasephasephase/libhat</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Configurations>Debug;Release</Configurations>
<Platforms>AnyCPU;x64</Platforms>
<PackageTags>libhat;vectorized;game-hacking;modding;pattern-scanning</PackageTags>
</PropertyGroup>

<ItemGroup>
<!-- make README available in the package -->
<None Include="../README.md" Pack="true" PackagePath="/" />

<!-- include the native library in the package (libhat_c) -->
<None Include="runtimes/win-x64/native/libhat_c.dll" Pack="true" PackagePath="runtimes/win-x64/native/" />
<None Include="runtimes/win-x86/native/libhat_c.dll" Pack="true" PackagePath="runtimes/win-x86/native/" />
<!-- include the native libraries in the package -->
<Content Include="runtimes/win-x64/native/libhat_c.dll" Pack="true" PackagePath="runtimes/win-x64/native/" />
<Content Include="runtimes/win-x86/native/libhat_c.dll" Pack="true" PackagePath="runtimes/win-x86/native/" />
</ItemGroup>

</Project>
Binary file modified bindings/cs/libhat-sharp/runtimes/win-x64/native/libhat_c.dll
Binary file not shown.
Binary file modified bindings/cs/libhat-sharp/runtimes/win-x86/native/libhat_c.dll
Binary file not shown.
8 changes: 4 additions & 4 deletions bindings/cs/libhat-tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@

var pattern = randomBytes.AsSpan().Slice(0x1000, 0x10).ToArray().AsPattern();
var scanner = new Scanner(Marshal.UnsafeAddrOfPinnedArrayElement(randomBytes, 0), (uint)randomBytes.Length);
var address = scanner.FindPattern(pattern);
var result = scanner.FindPattern(pattern);

Console.WriteLine($"found pattern at 0x{address:X}");
if (result != null) Console.WriteLine($"found pattern at 0x{result.Address:X}");

Console.WriteLine("\nscanning in module:");

var modulePattern = new Pattern("48 89 5C 24 ? 48 89 6C 24 ? 48 89 74 24 ? 57 48 81 EC");

var module = Process.GetCurrentProcess().MainModule!;
var moduleScanner = new Scanner(module);
var moduleAddress = moduleScanner.FindPattern(modulePattern);
var moduleResult = moduleScanner.FindPattern(modulePattern);

Console.WriteLine($"found pattern at 0x{moduleAddress:X}");
if (moduleResult != null) Console.WriteLine($"found pattern at 0x{moduleResult.Address:X}");
4 changes: 2 additions & 2 deletions bindings/cs/libhat-tests/libhat-tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Configurations>Debug;Release</Configurations>
<Platforms>AnyCPU;x64</Platforms>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="libhat-sharp" Version="0.1.1" />
<ProjectReference Include="..\libhat-sharp\libhat-sharp.csproj" />
</ItemGroup>

</Project>

0 comments on commit 7c0cf5c

Please sign in to comment.