Skip to content

Instantly share code, notes, and snippets.

@philiplaureano
Created April 19, 2013 21:46
Show Gist options
  • Save philiplaureano/5423458 to your computer and use it in GitHub Desktop.
Save philiplaureano/5423458 to your computer and use it in GitHub Desktop.
An example of how I used CFF explorer to generate the byte array for sampling and roundtripping an MS-DOS header from a .NET assembly.
public ShouldMatchGivenHeaderBytesWhenWrittenToTargetStream() : void
{
def bytes = array[ // dos header start
0x4d : byte, 0x5a, 0x90, 0x00, 0x03, 0x00, 0x00,
0x00, 0x04, 0x00, 0x00, 0x00, 0xff, 0xff,
0x00, 0x00, 0xb8, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// lfanew
0x80, 0x00, 0x00, 0x00,
// dos header end
0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09,
0xcd, 0x21, 0xb8, 0x01, 0x4c, 0xcd, 0x21,
0x54, 0x68, 0x69, 0x73, 0x20, 0x70, 0x72,
0x6f, 0x67, 0x72, 0x61, 0x6d, 0x20, 0x63,
0x61, 0x6e, 0x6e, 0x6f, 0x74, 0x20, 0x62,
0x65, 0x20, 0x72, 0x75, 0x6e, 0x20, 0x69,
0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20, 0x6d,
0x6f, 0x64, 0x65, 0x2e, 0x0d, 0x0d, 0x0a,
0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00];
def expectedHash = bytes.GetHash();
def writer = DosHeaderWriter();
def outputStream = MemoryStream();
def outputBytes = array(bytes.Length) : array[byte];
def dosHeader = DosHeader(MemoryStream(bytes));
writer.Write(dosHeader, outputStream);
_ = outputStream.Read(outputBytes, 0, bytes.Length);
def actualHash = outputStream.GetHash();
assert expectedHash equals actualHash otherwiseprint "Invalid DOS header";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment