|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using BinaryObjectScanner.Interfaces; |
| 5 | +using SabreTools.Matching; |
| 6 | +using SabreTools.Serialization.Wrappers; |
| 7 | + |
| 8 | +namespace BinaryObjectScanner.Packer |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Though not technically a packer, this detection is for any executables that include |
| 12 | + /// archives in their resources in some uncompressed manner to be used at runtime. |
| 13 | + /// </summary> |
| 14 | + public class EmbeddedArchive : IExtractableExecutable<PortableExecutable> |
| 15 | + { |
| 16 | + /// <inheritdoc/> |
| 17 | + public string? CheckExecutable(string file, PortableExecutable pex, bool includeDebug) |
| 18 | + { |
| 19 | + // Get the sections from the executable, if possible |
| 20 | + var sections = pex.Model.SectionTable; |
| 21 | + if (sections == null) |
| 22 | + return null; |
| 23 | + |
| 24 | + // Get the resources that have a PKZIP signature |
| 25 | + if (pex.ResourceData?.Any(kvp => kvp.Value is byte[] ba |
| 26 | + && ba.StartsWith(SabreTools.Models.PKZIP.Constants.LocalFileHeaderSignatureBytes)) == true) |
| 27 | + { |
| 28 | + return "Embedded Archive"; |
| 29 | + } |
| 30 | + |
| 31 | + return null; |
| 32 | + } |
| 33 | + |
| 34 | + /// <inheritdoc/> |
| 35 | + public bool Extract(string file, PortableExecutable pex, string outDir, bool includeDebug) |
| 36 | + { |
| 37 | + try |
| 38 | + { |
| 39 | + // If there are no resources |
| 40 | + if (pex.ResourceData == null) |
| 41 | + return false; |
| 42 | + |
| 43 | + // Get the resources that have a PKZIP signature |
| 44 | + var resources = pex.ResourceData |
| 45 | + .Where(kvp => kvp.Value != null && kvp.Value is byte[]) |
| 46 | + .Select(kvp => kvp.Value as byte[]) |
| 47 | + .Where(b => b != null && b.StartsWith(SabreTools.Models.PKZIP.Constants.LocalFileHeaderSignatureBytes)) |
| 48 | + .ToList(); |
| 49 | + |
| 50 | + for (int i = 0; i < resources.Count; i++) |
| 51 | + { |
| 52 | + try |
| 53 | + { |
| 54 | + // Get the resource data |
| 55 | + var data = resources[i]; |
| 56 | + if (data == null) |
| 57 | + continue; |
| 58 | + |
| 59 | + // Create the temp filename |
| 60 | + string tempFile = $"embedded_resource_{i}.zip"; |
| 61 | + tempFile = Path.Combine(outDir, tempFile); |
| 62 | + var directoryName = Path.GetDirectoryName(tempFile); |
| 63 | + if (directoryName != null && !Directory.Exists(directoryName)) |
| 64 | + Directory.CreateDirectory(directoryName); |
| 65 | + |
| 66 | + // Write the resource data to a temp file |
| 67 | + using var tempStream = File.Open(tempFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); |
| 68 | + tempStream?.Write(data, 0, data.Length); |
| 69 | + } |
| 70 | + catch (Exception ex) |
| 71 | + { |
| 72 | + if (includeDebug) Console.WriteLine(ex); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return true; |
| 77 | + } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + if (includeDebug) Console.WriteLine(ex); |
| 81 | + return false; |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments