C# snippets

OffensiveCSharpCheatsheet

C# Cheatsheet with useful code snippets and oneliners

Converting

IntPtr -> String

// 1 
string intptrString = intPtrVar.ToInt64().ToString("x2");
Console.WriteLine("0x{0}", intptrString);

// 2 
IntPtr intPtr = <something>;
Console.WriteLine("{0}", String.Format("0x{0:X}", intPtr.ToInt64()));

Byte[] -> String - Hex (useful for debugging)

public static string ByteArrayToString(byte[] ba)
{
    byte[] returnArray = new byte[ba.Length];
    returnArray = ba.ToArray();
    Array.Reverse(returnArray);
    return "0x" + String.Concat(Array.ConvertAll(returnArray, x => x.ToString("X2")));
}

Console.WriteLine("[+] something: {0}", ByteArrayToString(someBA));

String -> Byte[]

Byte[] -> String

Base64String -> Byte[]

Byte[] -> Base64String

UInt -> String

Byte[] -> IntPtr

IntPtr -> Byte[]

String -> Byte[]


Memory

Allocate unmanaged memory

Read memory from IntPtr

Write memory to IntPtr

Write IntPtr


Memory Calculations

IntPtr + UInt

ByteArray + Int


Process (debugging)

Find Process by name

Start Process (Notepad)

Kill Process


Encryption & Decryption

XOR ByteArray

AES CBC mode Decrypt

AES CBC mode Encrypt


Web

DownloadData - HTTP

DownloadData - HTTPS


DInvoke


MISC

is64Bit

Sleep

References

AES Encrypt, Decryptarrow-up-right

SharpSploitarrow-up-right

SharpBlockarrow-up-right

Sharp-Suitearrow-up-right

Last updated