// File: Services/Scanners/MemoryRegionScanner.cs using WebmrAPI.Exceptions; using WebmrAPI.Models; using WebmrAPI.Utils; namespace WebmrAPI.Services.Scanners { public class MemoryRegionScanner : AbstractScanner { override public ScanTarget Target { get => ScanTarget.MemoryRegions; } internal int PID { get; private set; } override internal async Task ScanAsync(Dictionary data) { return await Task.Run(() => { using (var process = new WindowsProcess(PID)) { try { process.ForeachMemoryRegion(info => { 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 }); }); 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 container, int pid) : base(scanner, container) { PID = pid; } } }