62 lines
2.1 KiB
C#
62 lines
2.1 KiB
C#
|
// File: Services/Scanners/MemoryRegionScanner.cs
|
|||
|
|
|||
|
using WebmrAPI.Exceptions;
|
|||
|
using WebmrAPI.Models;
|
|||
|
using WebmrAPI.Utils;
|
|||
|
|
|||
|
namespace WebmrAPI.Services.Scanners
|
|||
|
{
|
|||
|
public class MemoryRegionScanner : AbstractScanner<MemoryRegionInfo>
|
|||
|
{
|
|||
|
override public ScanTarget Target { get => ScanTarget.MemoryRegions; }
|
|||
|
internal int PID { get; private set; }
|
|||
|
|
|||
|
override internal bool Scan(out Dictionary<long, MemoryRegionInfo>? data)
|
|||
|
{
|
|||
|
data = new();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
using (var process = new WindowsProcess(PID))
|
|||
|
{
|
|||
|
var regions = data;
|
|||
|
process.ForeachMemoryRegion(info =>
|
|||
|
{
|
|||
|
long addr = info.BaseAddress.ToInt64();
|
|||
|
regions.Add(addr, new MemoryRegionInfo
|
|||
|
{
|
|||
|
MemoryAddress = addr,
|
|||
|
MemorySize = info.RegionSize.ToUInt64(),
|
|||
|
MemoryState = info.State,
|
|||
|
MemoryPageProtection = info.PageProtection,
|
|||
|
MemoryType = info.Type
|
|||
|
});
|
|||
|
});
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (ProcessAccessDeniedException ex)
|
|||
|
{
|
|||
|
Provider.Logger.LogWarning($"Access denied to process {PID} for memory region scanning. Error: {ex.Message}");
|
|||
|
}
|
|||
|
catch (MemoryRegionException ex)
|
|||
|
{
|
|||
|
Provider.Logger.LogWarning($"Error scanning memory regions for PID {PID}. Error: {ex.Message}");
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Provider.Logger.LogError($"An unexpected error occurred while scanning memory regions for PID {PID}. Error: {ex.Message}");
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
public MemoryRegionScanner(IScanProvider scanner, LazyConcurrentContainer<MemoryRegionInfo> container, int pid)
|
|||
|
: base(scanner, container)
|
|||
|
{
|
|||
|
PID = pid;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|