using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Security.Cryptography; using BurnOutSharp.ExecutableType.Microsoft.PE; namespace BurnOutSharp.Tools { internal static class Utilities { #region Dictionary Manipulation /// /// Append one result to a results dictionary /// /// Dictionary to append to /// Key to add information to /// String value to add public static void AppendToDictionary(ConcurrentDictionary> original, string key, string value) { // If the value is empty, don't add it if (string.IsNullOrWhiteSpace(value)) return; var values = new ConcurrentQueue(); values.Enqueue(value); AppendToDictionary(original, key, values); } /// /// Append one result to a results dictionary /// /// Dictionary to append to /// Key to add information to /// String value to add public static void AppendToDictionary(ConcurrentDictionary> original, string key, ConcurrentQueue values) { // If the dictionary is null, just return if (original == null) return; // Use a placeholder value if the key is null key = key ?? "NO FILENAME"; // Add the key if needed and then append the lists original.TryAdd(key, new ConcurrentQueue()); original[key].AddRange(values); } /// /// Append one results dictionary to another /// /// Dictionary to append to /// Dictionary to pull from public static void AppendToDictionary(ConcurrentDictionary> original, ConcurrentDictionary> addition) { // If either dictionary is missing, just return if (original == null || addition == null) return; // Loop through each of the addition keys and add accordingly foreach (string key in addition.Keys) { original.TryAdd(key, new ConcurrentQueue()); original[key].AddRange(addition[key]); } } /// /// Remove empty or null keys from a results dictionary /// /// Dictionary to clean public static void ClearEmptyKeys(ConcurrentDictionary> original) { // If the dictionary is missing, we can't do anything if (original == null) return; // Get a list of all of the keys var keys = original.Keys.ToList(); // Iterate and reset keys for (int i = 0; i < keys.Count; i++) { // Get the current key string key = keys[i]; // If the key is empty, remove it if (original[key] == null || !original[key].Any()) original.TryRemove(key, out _); } } /// /// Prepend a parent path from dictionary keys, if possible /// /// Dictionary to strip values from /// Path to strip from the keys public static void PrependToKeys(ConcurrentDictionary> original, string pathToPrepend) { // If the dictionary is missing, we can't do anything if (original == null) return; // Use a placeholder value if the path is null pathToPrepend = (pathToPrepend ?? "ARCHIVE").TrimEnd(Path.DirectorySeparatorChar); // Get a list of all of the keys var keys = original.Keys.ToList(); // Iterate and reset keys for (int i = 0; i < keys.Count; i++) { // Get the current key string currentKey = keys[i]; // Otherwise, get the new key name and transfer over string newKey = $"{pathToPrepend}{Path.DirectorySeparatorChar}{currentKey.Trim(Path.DirectorySeparatorChar)}"; original[newKey] = original[currentKey]; original.TryRemove(currentKey, out _); } } /// /// Strip a parent path from dictionary keys, if possible /// /// Dictionary to strip values from /// Path to strip from the keys public static void StripFromKeys(ConcurrentDictionary> original, string pathToStrip) { // If either is missing, we can't do anything if (original == null || string.IsNullOrEmpty(pathToStrip)) return; // Get a list of all of the keys var keys = original.Keys.ToList(); // Iterate and reset keys for (int i = 0; i < keys.Count; i++) { // Get the current key string currentKey = keys[i]; // If the key doesn't start with the path, don't touch it if (!currentKey.StartsWith(pathToStrip, StringComparison.OrdinalIgnoreCase)) continue; // Otherwise, get the new key name and transfer over string newKey = currentKey.Substring(pathToStrip.Length); original[newKey] = original[currentKey]; original.TryRemove(currentKey, out _); } } #endregion #region Concurrent Manipulation /// /// Add a range of values from one queue to another /// /// Queue to add data to /// Queue to get data from public static void AddRange(this ConcurrentQueue original, ConcurrentQueue values) { while (!values.IsEmpty) { if (!values.TryDequeue(out string value)) return; original.Enqueue(value); } } #endregion #region Processed Executable Information /// /// Get the internal version as reported by the resources /// /// Byte array representing the file contents /// Version string, null on error public static string GetInternalVersion(byte[] fileContent) { if (fileContent == null || !fileContent.Any()) return null; return GetInternalVersion(new PortableExecutable(fileContent, 0)); } /// /// Get the internal version as reported by the resources /// /// PortableExecutable representing the file contents /// Version string, null on error public static string GetInternalVersion(PortableExecutable pex) { string version = pex.FileVersion; if (!string.IsNullOrWhiteSpace(version)) return version; version = pex.ProductVersion; if (!string.IsNullOrWhiteSpace(version)) return version; version = pex.ManifestVersion; if (!string.IsNullOrWhiteSpace(version)) return version; return null; } /// /// Get the internal version as reported by the filesystem /// /// File to check for version /// Version string, null on error public static string GetInternalVersion(string file) { var fvinfo = GetFileVersionInfo(file); if (fvinfo?.FileVersion == null) return string.Empty; if (fvinfo.FileVersion != "") return fvinfo.FileVersion.Replace(", ", "."); else return fvinfo.ProductVersion.Replace(", ", "."); } #endregion #region Executable Information /// /// Get the file version info object related to a path, if possible /// /// File to get information for /// FileVersionInfo object on success, null on error private static FileVersionInfo GetFileVersionInfo(string file) { if (file == null || !File.Exists(file)) return null; try { return FileVersionInfo.GetVersionInfo(file); } catch { return null; } } /// /// Get the SHA1 hash of a file, if possible /// /// Path to the file to be hashed /// SHA1 hash as a string on success, null on error public static string GetFileSHA1(string path) { if (string.IsNullOrWhiteSpace(path)) return null; try { SHA1 sha1 = SHA1.Create(); using (Stream fileStream = File.OpenRead(path)) { byte[] buffer = new byte[32768]; while (true) { int bytesRead = fileStream.Read(buffer, 0, 32768); if (bytesRead == 32768) { sha1.TransformBlock(buffer, 0, bytesRead, null, 0); } else { sha1.TransformFinalBlock(buffer, 0, bytesRead); break; } } } string hash = BitConverter.ToString(sha1.Hash); hash = hash.Replace("-", string.Empty); return hash; } catch { return null; } } #endregion #region Wrappers for Matchers /// /// Wrapper for GetInternalVersion for use in content matching /// /// File to check for version /// Byte array representing the file contents /// Last matched positions in the contents /// Version string, null on error public static string GetInternalVersion(string file, byte[] fileContent, List positions) => GetInternalVersion(fileContent); /// /// Wrapper for GetInternalVersion for use in path matching /// /// File to check for version /// Full list of input paths /// Version string, null on error public static string GetInternalVersion(string firstMatchedString, IEnumerable files) => GetInternalVersion(firstMatchedString); #endregion } }