Files
GhostPack-SharpDPAPI/SharpDPAPI/lib/Helpers.cs
T
HarmJ0y c3a8a15d6c Version 1.1.0 release.
See CHANGELOG for details.
2019-03-15 01:36:12 -07:00

65 lines
2.4 KiB
C#
Executable File

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Security.Principal;
using System.Text.RegularExpressions;
namespace SharpDPAPI
{
public class Helpers
{
public static byte[] Combine(byte[] first, byte[] second)
{
// helper to combine two byte arrays
byte[] ret = new byte[first.Length + second.Length];
Buffer.BlockCopy(first, 0, ret, 0, first.Length);
Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
return ret;
}
public static string RemoveWhiteSpaces(string input)
{
return Regex.Replace(input, @"\ +(?=(\n|\r?$))", "");
}
public static byte[] StringToByteArray(string hex)
{
// helper to convert a string hex representation to a byte array
// yes, I know this inefficient :)
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
public static IEnumerable<string> Split(string text, int partLength)
{
// helper to split strings into blobs of particular length
if (text == null) { Console.WriteLine("[ERROR] Split() - singleLineString"); }
if (partLength < 1) { Console.WriteLine("[ERROR] Split() - 'columns' must be greater than 0."); }
var partCount = Math.Ceiling((double)text.Length / partLength);
if (partCount < 2)
{
yield return text;
}
for (int i = 0; i < partCount; i++)
{
var index = i * partLength;
var lengthLeft = Math.Min(partLength, text.Length - index);
var line = text.Substring(index, lengthLeft);
yield return line;
}
}
public static bool IsHighIntegrity()
{
// returns true if the current process is running with adminstrative privs in a high integrity context
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
}