2025-07-04 00:04:51 +03:00
|
|
|
|
/* This software is licensed by the MIT License, see LICENSE file */
|
|
|
|
|
/* Copyright © 2024-2025 Gregory Lirent */
|
2025-07-03 15:22:40 +03:00
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
|
2025-07-03 20:01:19 +03:00
|
|
|
|
override internal async Task<bool> ScanAsync(Dictionary<long, MemoryRegionInfo> data)
|
2025-07-03 15:22:40 +03:00
|
|
|
|
{
|
|
|
|
|
|
2025-07-03 20:01:19 +03:00
|
|
|
|
return await Task.Run<bool>(() =>
|
2025-07-03 15:22:40 +03:00
|
|
|
|
{
|
|
|
|
|
using (var process = new WindowsProcess(PID))
|
|
|
|
|
{
|
2025-07-03 20:01:19 +03:00
|
|
|
|
try
|
2025-07-03 15:22:40 +03:00
|
|
|
|
{
|
2025-07-03 20:01:19 +03:00
|
|
|
|
process.ForeachMemoryRegion(info =>
|
2025-07-03 15:22:40 +03:00
|
|
|
|
{
|
2025-07-03 20:01:19 +03:00
|
|
|
|
long addr = info.BaseAddress.ToInt64();
|
|
|
|
|
data.Add(addr, new MemoryRegionInfo
|
|
|
|
|
{
|
|
|
|
|
MemoryAddress = addr,
|
|
|
|
|
MemorySize = info.RegionSize.ToUInt64(),
|
|
|
|
|
MemoryState = info.State,
|
|
|
|
|
MemoryPageProtection = info.PageProtection,
|
|
|
|
|
MemoryType = info.Type
|
|
|
|
|
});
|
2025-07-03 15:22:40 +03:00
|
|
|
|
});
|
2025-07-03 20:01:19 +03:00
|
|
|
|
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}");
|
|
|
|
|
}
|
2025-07-03 15:22:40 +03:00
|
|
|
|
}
|
2025-07-03 20:01:19 +03:00
|
|
|
|
return false;
|
|
|
|
|
});
|
2025-07-03 15:22:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public MemoryRegionScanner(IScanProvider scanner, LazyConcurrentContainer<MemoryRegionInfo> container, int pid)
|
|
|
|
|
: base(scanner, container)
|
|
|
|
|
{
|
|
|
|
|
PID = pid;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|