mirror of
https://github.com/andreacristaldi/DefenderRuleParser
synced 2026-06-16 13:55:00 +00:00
63 lines
2.2 KiB
C#
63 lines
2.2 KiB
C#
// DefenderRuleParser
|
|
// Author: Andrea Cristaldi 2025 - https://github.com/andreacristaldi/DefenderRuleParser
|
|
// This project is licensed under the Apache 2.0 License.
|
|
/*
|
|
* Summary: Captures friendly SHA-256 lists (human-annotated hashes) present in some signatures.
|
|
* Origin: dump-driven; hash length/format recognized from hex shape.
|
|
* Role: Emits sanitized SHA-256 values for lookups and YARA emission.
|
|
*/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using DefenderRuleParser2.Models;
|
|
|
|
namespace DefenderRuleParser2.Parsers
|
|
{
|
|
public class FriendlyFileSha256Parser : ISignatureParser
|
|
{
|
|
public void Parse(BinaryReader reader, int size, uint threatId)
|
|
{
|
|
long offset = reader.BaseStream.Position;
|
|
|
|
try
|
|
{
|
|
if (size != 32)
|
|
{
|
|
Logger.Warn($"[!] FRIENDLYFILE_SHA256 ! Unexpected size: {size} (expected 32 bytes)");
|
|
reader.BaseStream.Seek(offset + size, SeekOrigin.Begin);
|
|
return;
|
|
}
|
|
|
|
byte[] hashBytes = reader.ReadBytes(32);
|
|
string hexHash = BitConverter.ToString(hashBytes).Replace("-", "").Trim();
|
|
|
|
Logger.Info($"[FRIENDLYFILE_SHA256] Threat ID: {threatId}");
|
|
Logger.Info($" > SHA256 (internal): {hexHash}");
|
|
|
|
if (ThreatDatabase.TryGetThreat(threatId, out var threat))
|
|
{
|
|
threat.Signatures.Add(new SignatureEntry
|
|
{
|
|
Type = "SIGNATURE_TYPE_FRIENDLYFILE_SHA256",
|
|
Offset = offset,
|
|
Pattern = new List<string> { hexHash },
|
|
Parsed = true,
|
|
ConditionType = "PRESENT",
|
|
ConditionValue = 1
|
|
});
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.Error($"[!] FRIENDLYFILE_SHA256 Error parsing at offset 0x{offset:X}: {ex.Message}");
|
|
}
|
|
finally
|
|
{
|
|
reader.BaseStream.Seek(offset + size, SeekOrigin.Begin);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|