Files
BinaryObjectScanner/BinaryObjectScanner.Packer/UPX.cs

113 lines
4.2 KiB
C#
Raw Normal View History

2021-03-21 15:24:23 -07:00
using System.Collections.Generic;
2022-05-01 21:02:59 -07:00
using System.IO;
2022-12-10 17:31:41 -08:00
using System.Linq;
2020-10-26 23:30:06 -07:00
using System.Text;
2022-12-10 17:31:41 -08:00
using System.Text.RegularExpressions;
using BinaryObjectScanner.Interfaces;
2023-03-07 16:59:14 -05:00
using BinaryObjectScanner.Wrappers;
2020-10-26 23:30:06 -07:00
2023-03-09 23:52:58 -05:00
namespace BinaryObjectScanner.Packer
2020-10-26 23:30:06 -07:00
{
2022-05-01 21:02:59 -07:00
// TODO: Add extraction
2022-07-13 12:54:08 -07:00
// https://raw.githubusercontent.com/wolfram77web/app-peid/master/userdb.txt
2023-03-09 16:02:51 -05:00
public class UPX : IExtractable, IPortableExecutableCheck
2020-10-26 23:30:06 -07:00
{
2022-12-10 17:31:41 -08:00
private static readonly Regex _oldUpxVersionMatch = new Regex(@"\$Id: UPX (.*?) Copyright \(C\)", RegexOptions.Compiled);
private static readonly Regex _upxVersionMatch = new Regex(@"^([0-9]\.[0-9]{2})$", RegexOptions.Compiled);
/// <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
{
// 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
2022-12-10 17:31:41 -08:00
// Check header padding strings
2023-01-09 16:34:45 -08:00
if (pex.HeaderPaddingStrings?.Any() == true)
{
2022-12-10 17:31:41 -08:00
string match = pex.HeaderPaddingStrings.FirstOrDefault(s => s.Contains("UPX!"));
//if (match != null)
// return "UPX";
2021-02-25 11:13:57 -08:00
2022-12-10 17:31:41 -08:00
match = pex.HeaderPaddingStrings.FirstOrDefault(s => s.StartsWith("$Id: UPX"));
if (match != null)
{
var regexMatch = _oldUpxVersionMatch.Match(match);
if (regexMatch.Success)
return $"UPX {regexMatch.Groups[1].Value}";
else
return "UPX (Unknown Version)";
}
2021-03-21 15:24:23 -07:00
2022-12-10 17:31:41 -08:00
match = pex.HeaderPaddingStrings.FirstOrDefault(s => _upxVersionMatch.IsMatch(s));
if (pex.HeaderPaddingStrings.Any(s => s == "UPX!" && match != null))
{
var regexMatch = _upxVersionMatch.Match(match);
if (regexMatch.Success)
return $"UPX {regexMatch.Groups[1].Value}";
else
return "UPX (Unknown Version)";
}
else if (pex.HeaderPaddingStrings.Any(s => s == "NOS " && match != null))
{
var regexMatch = _upxVersionMatch.Match(match);
if (regexMatch.Success)
return $"UPX (NOS Variant) {regexMatch.Groups[1].Value}";
else
return "UPX (NOS Variant) (Unknown Version)";
}
}
2021-03-21 15:24:23 -07:00
return null;
}
2021-02-25 11:13:57 -08:00
/// <inheritdoc/>
2023-03-09 17:16:39 -05:00
public string Extract(string file, bool includeDebug)
{
if (!File.Exists(file))
return null;
using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read))
{
2023-03-09 17:16:39 -05:00
return Extract(fs, file, includeDebug);
}
}
/// <inheritdoc/>
2023-03-09 17:16:39 -05:00
public string Extract(Stream stream, string file, bool includeDebug)
{
return null;
}
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)";
}
}
}
}