2022-05-01 21:02:59 -07:00
|
|
|
using System.Collections.Concurrent;
|
2021-03-21 15:24:23 -07:00
|
|
|
using System.Collections.Generic;
|
2022-05-01 21:02:59 -07:00
|
|
|
using System.IO;
|
2020-10-26 23:30:06 -07:00
|
|
|
using System.Text;
|
2022-03-14 10:40:44 -07:00
|
|
|
using BurnOutSharp.ExecutableType.Microsoft.PE;
|
2022-05-01 17:41:50 -07:00
|
|
|
using BurnOutSharp.Interfaces;
|
2021-03-21 15:24:23 -07:00
|
|
|
using BurnOutSharp.Matching;
|
2020-10-26 23:30:06 -07:00
|
|
|
|
2020-10-30 09:09:16 -07:00
|
|
|
namespace BurnOutSharp.PackerType
|
2020-10-26 23:30:06 -07:00
|
|
|
{
|
2022-05-01 21:02:59 -07:00
|
|
|
// TODO: Add extraction
|
|
|
|
|
public class UPX : IPortableExecutableCheck, IScannable
|
2020-10-26 23:30:06 -07:00
|
|
|
{
|
2022-05-01 21:02:59 -07:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public bool ShouldScan(byte[] magic) => true;
|
|
|
|
|
|
2021-08-26 15:50:38 -07:00
|
|
|
/// <inheritdoc/>
|
2022-05-01 17:17:15 -07:00
|
|
|
public string CheckPortableExecutable(string file, PortableExecutable pex, bool includeDebug)
|
2020-10-26 23:30:06 -07:00
|
|
|
{
|
2021-08-26 15:50:38 -07:00
|
|
|
// Get the sections from the executable, if possible
|
2021-08-27 09:42:05 -07:00
|
|
|
var sections = pex?.SectionTable;
|
2021-08-26 15:50:38 -07:00
|
|
|
if (sections == null)
|
|
|
|
|
return null;
|
2020-10-26 23:30:06 -07:00
|
|
|
|
2021-08-26 15:50:38 -07:00
|
|
|
// Standard UPX
|
2022-03-15 00:30:33 -07:00
|
|
|
var sectionData = FindData(pex, "UPX");
|
|
|
|
|
if (sectionData != null)
|
2021-08-26 15:50:38 -07:00
|
|
|
{
|
|
|
|
|
var matchers = new List<ContentMatchSet>
|
|
|
|
|
{
|
|
|
|
|
// UPX!
|
2022-03-15 00:30:33 -07:00
|
|
|
new ContentMatchSet(new byte?[] { 0x55, 0x50, 0x58, 0x21 }, GetVersion, "UPX"),
|
2021-08-26 15:50:38 -07:00
|
|
|
};
|
2021-02-25 11:27:08 -08:00
|
|
|
|
2022-03-15 00:30:33 -07:00
|
|
|
return MatchUtil.GetFirstMatch(file, sectionData, matchers, includeDebug);
|
2021-08-26 15:50:38 -07:00
|
|
|
}
|
2021-02-25 11:13:57 -08:00
|
|
|
|
2021-08-26 15:50:38 -07:00
|
|
|
// NOS Variant
|
2022-03-15 00:30:33 -07:00
|
|
|
sectionData = FindData(pex, "NOS");
|
|
|
|
|
if (sectionData != null)
|
2021-08-26 15:50:38 -07:00
|
|
|
{
|
|
|
|
|
var matchers = new List<ContentMatchSet>
|
|
|
|
|
{
|
|
|
|
|
// NOS
|
2022-03-15 00:30:33 -07:00
|
|
|
new ContentMatchSet(new byte?[] { 0x4E, 0x4F, 0x53, 0x20 }, GetVersion, "UPX (NOS Variant)"),
|
2021-08-26 15:50:38 -07:00
|
|
|
};
|
2021-03-21 15:24:23 -07:00
|
|
|
|
2022-03-15 00:30:33 -07:00
|
|
|
return MatchUtil.GetFirstMatch(file, sectionData, matchers, includeDebug);
|
2021-08-26 15:50:38 -07:00
|
|
|
}
|
2021-03-21 15:24:23 -07:00
|
|
|
|
2021-08-26 15:50:38 -07:00
|
|
|
return null;
|
2021-08-25 19:37:32 -07:00
|
|
|
}
|
2021-02-25 11:13:57 -08:00
|
|
|
|
2022-05-01 21:02:59 -07:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, string file)
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(file))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
using (var fs = File.OpenRead(file))
|
|
|
|
|
{
|
|
|
|
|
return Scan(scanner, fs, file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, Stream stream, string file)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 11:43:51 -07:00
|
|
|
public static string GetVersion(string file, byte[] fileContent, List<int> positions)
|
2020-10-26 23:30:06 -07:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-08-26 20:43:58 -07:00
|
|
|
// Check the normal version location first
|
|
|
|
|
int index = positions[0] - 5;
|
2020-10-31 14:15:33 -07:00
|
|
|
string versionString = Encoding.ASCII.GetString(fileContent, index, 4);
|
2021-08-26 20:43:58 -07:00
|
|
|
if (char.IsNumber(versionString[0]))
|
|
|
|
|
return versionString;
|
|
|
|
|
|
|
|
|
|
// Check for the old-style string
|
|
|
|
|
//
|
|
|
|
|
// Example:
|
|
|
|
|
// $Info: This file is packed with the UPX executable packer http://upx.tsx.org $
|
|
|
|
|
// $Id: UPX 1.02 Copyright (C) 1996-2000 the UPX Team. All Rights Reserved. $
|
|
|
|
|
// UPX!
|
|
|
|
|
index = positions[0] - 67;
|
|
|
|
|
versionString = Encoding.ASCII.GetString(fileContent, index, 4);
|
|
|
|
|
if (char.IsNumber(versionString[0]))
|
|
|
|
|
return versionString;
|
2020-10-31 14:15:33 -07:00
|
|
|
|
2021-08-26 20:43:58 -07:00
|
|
|
return "(Unknown Version)";
|
2020-10-26 23:30:06 -07:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return "(Unknown Version)";
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-26 15:50:38 -07:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Find the location of the first matched section, if possible
|
|
|
|
|
/// </summary>
|
2021-09-11 21:03:36 -07:00
|
|
|
/// <param name="pex">PortableExecutable representing the read-in file</param>
|
2021-08-26 15:50:38 -07:00
|
|
|
/// <param name="sectionPrefix">Prefix of the sections to check for</param>
|
2022-03-15 00:30:33 -07:00
|
|
|
/// <returns>Section data, null on error</returns>
|
|
|
|
|
private byte[] FindData(PortableExecutable pex, string sectionPrefix)
|
2021-08-26 15:50:38 -07:00
|
|
|
{
|
|
|
|
|
// Get the two matching sections, if possible
|
2021-09-11 21:03:36 -07:00
|
|
|
var firstSection = pex.GetFirstSection($"{sectionPrefix}0", exact: true);
|
|
|
|
|
var secondSection = pex.GetFirstSection($"{sectionPrefix}1", exact: true);
|
2021-08-26 15:50:38 -07:00
|
|
|
|
|
|
|
|
// If either section is null, we can't do anything
|
|
|
|
|
if (firstSection == null || secondSection == null)
|
2022-03-15 00:30:33 -07:00
|
|
|
return null;
|
2021-08-26 15:50:38 -07:00
|
|
|
|
2022-03-15 00:30:33 -07:00
|
|
|
// This subtract is needed because the version is before the section
|
|
|
|
|
return pex.ReadRawSection($"{sectionPrefix}0", first: true, offset: -128);
|
2021-08-26 15:50:38 -07:00
|
|
|
}
|
2020-10-26 23:30:06 -07:00
|
|
|
}
|
|
|
|
|
}
|