DLLDetector.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.Reflection;
  2. using System.Text;
  3. using Masuit.Tools.Mime;
  4. namespace Masuit.Tools.Files.FileDetector.Detectors;
  5. [FormatCategory(FormatCategory.System)]
  6. [FormatCategory(FormatCategory.Executable)]
  7. internal sealed class DLLDetector : AbstractSignatureDetector
  8. {
  9. public override string Precondition => "exe";
  10. protected override SignatureInformation[] SignatureInformations { get; } =
  11. {
  12. new()
  13. {
  14. Position = 0,
  15. Signature = "MZ"u8.ToArray()
  16. },
  17. };
  18. public override string Extension => "dll";
  19. public override string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
  20. public override List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
  21. public override bool Detect(Stream stream)
  22. {
  23. if (stream.Length < 100)
  24. {
  25. return false;
  26. }
  27. if (base.Detect(stream))
  28. {
  29. stream.Position = 60;
  30. var reader = new BinaryReader(stream, Encoding.UTF8, true);
  31. var num = reader.ReadInt32();
  32. if (num < 0)
  33. {
  34. return false;
  35. }
  36. stream.Position = num + 4 + 18;
  37. short characteristics = reader.ReadInt16();
  38. return (characteristics & 0x2000) != 0;
  39. }
  40. return false;
  41. }
  42. public override string ToString() => "Windows Dynamic Linkage Library Detector";
  43. }