Files
BinaryObjectScanner/BurnOutSharp/PackerType/UPX.cs

103 lines
3.8 KiB
C#
Raw Normal View History

2021-03-21 15:24:23 -07:00
using System.Collections.Generic;
2020-10-26 23:30:06 -07:00
using System.Text;
using BurnOutSharp.ExecutableType.Microsoft.NE;
using BurnOutSharp.ExecutableType.Microsoft.PE;
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
{
2021-02-26 01:26:49 -08:00
public class UPX : IContentCheck
2020-10-26 23:30:06 -07:00
{
/// <inheritdoc/>
public string CheckContents(string file, byte[] fileContent, bool includeDebug, PortableExecutable pex, NewExecutable nex)
2020-10-26 23:30:06 -07:00
{
// Get the sections from the executable, if possible
var sections = pex?.SectionTable;
if (sections == null)
return null;
2020-10-26 23:30:06 -07:00
// Standard UPX
int foundPosition = FindData(pex, "UPX");
if (foundPosition > -1)
{
var matchers = new List<ContentMatchSet>
{
// UPX!
new ContentMatchSet(
new ContentMatch(new byte?[] { 0x55, 0x50, 0x58, 0x21 }, end: foundPosition),
GetVersion,
"UPX"),
};
2021-02-25 11:27:08 -08:00
return MatchUtil.GetFirstMatch(file, fileContent, matchers, includeDebug);
}
2021-02-25 11:13:57 -08:00
// NOS Variant
foundPosition = FindData(pex, "NOS");
if (foundPosition > -1)
{
var matchers = new List<ContentMatchSet>
{
// NOS
new ContentMatchSet(
new ContentMatch(new byte?[] { 0x4E, 0x4F, 0x53, 0x20 }, end: foundPosition),
GetVersion,
"UPX (NOS Variant)"),
};
2021-03-21 15:24:23 -07:00
return MatchUtil.GetFirstMatch(file, fileContent, matchers, includeDebug);
}
2021-03-21 15:24:23 -07:00
return null;
}
2021-02-25 11:13:57 -08: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)";
}
}
/// <summary>
/// Find the location of the first matched section, if possible
/// </summary>
/// <param name="pex">PortableExecutable representing the read-in file</param>
/// <param name="sectionPrefix">Prefix of the sections to check for</param>
/// <returns>Real address of the section data, -1 on error</returns>
private int FindData(PortableExecutable pex, string sectionPrefix)
{
// Get the two matching sections, if possible
var firstSection = pex.GetFirstSection($"{sectionPrefix}0", exact: true);
var secondSection = pex.GetFirstSection($"{sectionPrefix}1", exact: true);
// If either section is null, we can't do anything
if (firstSection == null || secondSection == null)
return -1;
// Return the first section address
return (int)firstSection.PointerToRawData;
}
2020-10-26 23:30:06 -07:00
}
}