Skip to content

Commit c29354f

Browse files
committed
Add embedded archive scanning
1 parent 7738630 commit c29354f

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ Below is a list of executable packers detected by BinaryObjectScanner. The three
164164
| AutoPlay Media Studio | Yes | No | No | |
165165
| CExe | Yes | No | Yes | |
166166
| dotFuscator | Yes | No | No | |
167+
| Embedded Archive | Yes | No | Yes | Not technically a packer |
167168
| Embedded Executable | Yes | No | Yes | Not technically a packer |
168169
| EXE Stealth | Yes | No | No | |
169170
| Gentee Installer | Yes | No | No | |

0 commit comments

Comments
 (0)