using System;
using System.Collections.Generic;
namespace MemScanEDR.Models;
///
/// AI 输入特征向量 —— 从进程内存提取的全部多维特征
/// 作为无监督基线检测和监督分类的输入
///
public class MemoryFeatures
{
// ========== 一、进程元数据特征 ==========
/// 进程名
public string ProcessName { get; set; } = "";
/// 原始镜像路径
public string ExePath { get; set; } = "";
/// 父进程 PID
public int ParentPid { get; set; }
/// 父进程名
public string ParentName { get; set; } = "";
/// 命令行
public string CmdLine { get; set; } = "";
/// 启动时间
public DateTime StartTime { get; set; }
/// 工作集大小 (MB)
public long WorkingSetMB { get; set; }
/// 线程数
public int ThreadCount { get; set; }
/// 是否已签名
public bool IsSigned { get; set; }
/// 签名者
public string Signer { get; set; } = "";
/// 是否在系统路径
public bool IsSystemPath { get; set; }
/// 是否在临时路径
public bool IsTempPath { get; set; }
// ========== 二、内存页权限分布特征 ==========
/// 内存区域总数
public int TotalRegions { get; set; }
/// 只读页数 (R)
public int ReadOnlyPages { get; set; }
/// 读写页数 (RW)
public int ReadWritePages { get; set; }
/// 只读执行页数 (RX) —— 正常代码段
public int ExecuteReadPages { get; set; }
/// 读写执行页数 (RWX) —— 高可疑指标
public int ExecuteReadWritePages { get; set; }
/// 私有内存页数 (PRIVATE)
public int PrivatePages { get; set; }
/// 映射文件页数 (MAPPED)
public int MappedPages { get; set; }
/// 磁盘镜像页数 (IMAGE)
public int ImagePages { get; set; }
/// 无文件匿名可执行内存页数 (PRIVATE + EXECUTE 且非 IMAGE)
public int AnonymousExecPages { get; set; }
/// RWX私有页数量 (恶意注入核心标志)
public int RwxPrivatePages { get; set; }
/// RWX页总大小 (bytes)
public ulong RwxTotalSize { get; set; }
// ========== 三、内存代码特征 ==========
/// 内存中是否存在 PE 头
public bool HasInMemoryPE { get; set; }
/// 内存PE与磁盘是否不一致 (Process Hollowing 标志)
public bool PeMismatchDisk { get; set; }
/// 检测到的可疑API字符串
public List SuspiciousApiStrings { get; set; } = new();
/// 内存敏感字符串命中数
public int SuspiciousStringHits { get; set; }
/// 检测到的内存敏感字符串样本
public List SuspiciousStrings { get; set; } = new();
/// 签名匹配数量
public int SignatureMatchCount { get; set; }
/// 高危签名匹配数
public int HighSeverityMatches { get; set; }
// ========== 四、熵/字节统计特征 ==========
/// 代码段平均熵值 (0-8)
public double AvgCodeEntropy { get; set; }
/// 最高熵值
public double MaxEntropy { get; set; }
/// 高熵区域数量 (熵 > 7.0)
public int HighEntropyRegions { get; set; }
/// 高熵区域总大小
public ulong HighEntropyTotalSize { get; set; }
/// 熵值标准差 (均匀性指标)
public double EntropyStdDev { get; set; }
// ========== 五、网络行为特征 ==========
/// 网络连接数
public int ConnectionCount { get; set; }
/// 监听端口数
public int ListeningPorts { get; set; }
/// 出站连接到非标准端口数
public int NonStandardPortConnections { get; set; }
/// 可疑端口连接数 (4444, 1337 等)
public int SuspiciousPortConnections { get; set; }
/// 连接到境外IP数 (简化:非私有IP)
public int ForeignConnections { get; set; }
// ========== 六、进程树关系特征 ==========
/// 是否为系统关键进程名
public bool IsSystemProcessName { get; set; }
/// 父进程是否合法
public bool HasLegitimateParent { get; set; }
/// 进程树深度
public int ProcessTreeDepth { get; set; }
// ========== 七、DLL加载特征 ==========
/// 已加载模块数量
public int LoadedModuleCount { get; set; }
/// 无磁盘路径模块数
public int UnmappedModules { get; set; }
/// 从临时目录加载的模块数
public int TempPathModules { get; set; }
// ========== 辅助方法 ==========
///
/// 将特征向量转为数值数组,用于 AI 模型输入
/// 特征顺序必须与训练时一致
///
public double[] ToFeatureVector()
{
return new double[]
{
// 进程元数据 (9维)
IsSigned ? 1.0 : 0.0,
IsSystemPath ? 1.0 : 0.0,
IsTempPath ? 1.0 : 0.0,
WorkingSetMB,
ThreadCount,
IsSystemProcessName ? 1.0 : 0.0,
HasLegitimateParent ? 1.0 : 0.0,
ProcessTreeDepth,
StartTime != default ? (DateTime.Now - StartTime).TotalHours : 0,
// 内存页权限分布 (7维)
TotalRegions,
ReadOnlyPages / Math.Max(TotalRegions, 1.0),
ReadWritePages / Math.Max(TotalRegions, 1.0),
ExecuteReadPages / Math.Max(TotalRegions, 1.0),
ExecuteReadWritePages / Math.Max(TotalRegions, 1.0),
AnonymousExecPages / Math.Max(TotalRegions, 1.0),
RwxPrivatePages,
// 内存类型分布 (3维)
PrivatePages / Math.Max(TotalRegions, 1.0),
MappedPages / Math.Max(TotalRegions, 1.0),
ImagePages / Math.Max(TotalRegions, 1.0),
// 代码特征 (6维)
HasInMemoryPE ? 1.0 : 0.0,
PeMismatchDisk ? 1.0 : 0.0,
SuspiciousApiStrings.Count,
SuspiciousStringHits,
SignatureMatchCount,
HighSeverityMatches,
// 熵特征 (4维)
AvgCodeEntropy / 8.0, // 归一化到 [0,1]
MaxEntropy / 8.0,
HighEntropyRegions,
EntropyStdDev / 4.0,
// 网络特征 (5维)
ConnectionCount,
ListeningPorts,
NonStandardPortConnections,
SuspiciousPortConnections,
ForeignConnections,
// DLL加载特征 (3维)
LoadedModuleCount,
UnmappedModules,
TempPathModules
};
}
/// 特征向量维度
public static int FeatureDimension => 40;
}