mirror of
https://github.com/andreacristaldi/DefenderRuleParser
synced 2026-06-16 13:55:00 +00:00
63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
// DefenderRuleParser
|
|
// Author: Andrea Cristaldi 2025 - https://github.com/andreacristaldi/DefenderRuleParser
|
|
// This project is licensed under the Apache 2.0 License.
|
|
/*
|
|
* Summary: Generic fixed-layout binary parser for small atomized records.
|
|
* Origin: dump-driven; field sizes/endianness inferred empirically.
|
|
* Role: Provides a reusable path when the structure is regular but unnamed.
|
|
*/
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using DefenderRuleParser2.Models;
|
|
|
|
namespace DefenderRuleParser2.Parsers
|
|
{
|
|
public class GenericBinarySignatureParser : ISignatureParser
|
|
{
|
|
private readonly string _signatureType;
|
|
|
|
public GenericBinarySignatureParser(string signatureType)
|
|
{
|
|
_signatureType = signatureType;
|
|
}
|
|
|
|
public void Parse(BinaryReader reader, int size, uint threatId)
|
|
{
|
|
long offset = reader.BaseStream.Position;
|
|
|
|
try
|
|
{
|
|
byte[] data = reader.ReadBytes(size);
|
|
string hex = BitConverter.ToString(data).Replace("-", " ");
|
|
|
|
Logger.Info($"[{_signatureType}] Threat ID: {threatId}, Size: {size} bytes");
|
|
Logger.Info($" > Hex: {hex}");
|
|
|
|
if (ThreatDatabase.TryGetThreat(threatId, out var threat))
|
|
{
|
|
threat.Signatures.Add(new SignatureEntry
|
|
{
|
|
Type = _signatureType,
|
|
Offset = offset,
|
|
Pattern = new List<string> { hex },
|
|
Parsed = true,
|
|
ConditionType = "PRESENT",
|
|
ConditionValue = 1
|
|
});
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Error($"[!] {_signatureType} Error parsing at offset 0x{offset:X}: {ex.Message}");
|
|
}
|
|
finally
|
|
{
|
|
reader.BaseStream.Seek(offset + size, SeekOrigin.Begin);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|