Files
BinaryObjectScanner/BurnOutSharp/PackerType/UPX.cs

115 lines
4.3 KiB
C#
Raw Normal View History

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;
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;
2022-05-01 17:41:50 -07:00
using BurnOutSharp.Interfaces;
2021-03-21 15:24:23 -07:00
using BurnOutSharp.Matching;
using BurnOutSharp.Wrappers;
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
2022-07-13 12:54:08 -07:00
// https://raw.githubusercontent.com/wolfram77web/app-peid/master/userdb.txt
2022-05-01 21:02:59 -07:00
public class UPX : IPortableExecutableCheck, IScannable
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
if (pex.HeaderPaddingStrings.Any())
{
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
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;
2022-12-22 22:03:32 -08:00
using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read))
2022-05-01 21:02:59 -07:00
{
return Scan(scanner, fs, file);
}
}
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, Stream stream, string file)
{
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)";
}
}
}
}