Skip to content

Commit

Permalink
added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidSec committed Dec 20, 2021
1 parent fd00b5a commit 3a2ec1c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 70 deletions.
4 changes: 3 additions & 1 deletion ioctlpus/AboutForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ public AboutForm()
{
InitializeComponent();
InitializeOSC();
// load about text and assembly version, instead of having it hardcoded
string version = Application.ProductVersion;
lblAbout.Text = "ioctlpus "+version+"\nCreated in 2017 by Jackson Thuraisamy(@Jackson_T)\nUpdated and mantained by Paolo Stagno(@Void_Sec)\n\nDedicated to my partner and family for putting up with me.";
lblAbout.Text = "ioctlpus " + version + "\nCreated in 2017 by Jackson Thuraisamy(@Jackson_T)\nUpdated and mantained by Paolo Stagno(@Void_Sec)\n\nDedicated to my partner and family for putting up with me.";
}

private void InitializeOSC()
{
// populate list of open source components
List<OpenSourceComponent> components = new List<OpenSourceComponent>();

components.Add(new OpenSourceComponent("HexBox", "Bernhard Elbl", "MIT"));
Expand Down
92 changes: 39 additions & 53 deletions ioctlpus/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using BrightIdeasSoftware;
using Microsoft.Win32.SafeHandles;
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
Expand All @@ -22,28 +22,24 @@ public MainForm()
{
InitializeComponent();

// Add placeholder text to filters textbox.
// Add placeholder text to filters textbox
SendMessage(tbFilters.Handle, EM_SETCUEBANNER, 0, "Filters (e.g. 9C412000 br=64 !ec=C000000D)");

// Setup HexBoxes.
// Setup HexBoxes
InitializeHexBox(hbInput, (int)nudInputSize.Value);
InitializeHexBox(hbOutput, (int)nudOutputSize.Value);

// Setup TreeListView.
// Setup TreeListView
InitializeTreeListView();

// Setup initial parameters.
// Setup initial parameters
tbDevicePath.Text = @"\\.\PhysicalDrive0";
tbIOCTL.Text = "70000";
tbAccessMask.Text = "20000000";
cmbACL.SelectedItem = "ANY_ACCESS";
}

/// <summary>
/// Initialize the given buffer view.
/// </summary>
/// <param name="hexBox"></param>
/// <param name="size"></param>
// Initialize the given buffer view
private void InitializeHexBox(HexBox hexBox, int size)
{
hexBox.Visible = true;
Expand All @@ -60,37 +56,35 @@ private void InitializeHexBox(HexBox hexBox, int size)
hexBox.ByteProvider = new DynamicByteProvider(new byte[size]);
}

/// <summary>
/// Initialize the Request History views.
/// </summary>
// Initialize the Request History views
private void InitializeTreeListView()
{
// Add colours to request rows.
// Add colours to request rows
tlvRequestHistory.FormatRow += (sender, eventArgs) =>
{
Request transmission = (Request)eventArgs.Model;
if (transmission.IsFavourite)
eventArgs.Item.BackColor = System.Drawing.Color.LightYellow;
eventArgs.Item.BackColor = Color.LightYellow;
else if (transmission.ReturnValue > 0)
eventArgs.Item.BackColor = System.Drawing.Color.MistyRose;
eventArgs.Item.BackColor = Color.MistyRose;
};

// Rename requests when double-clicked or F2 is pressed.
// Rename requests when double-clicked or F2 is pressed
tlvRequestHistory.CellEditActivation = BrightIdeasSoftware.ObjectListView.CellEditActivateMode.DoubleClick;

// How to identify if a row has children.
// How to identify if a row has children
tlvRequestHistory.CanExpandGetter = delegate (Object tx)
{
return ((Request)tx).Children.Count > 0;
};

// Where row children are located.
// Where row children are located
tlvRequestHistory.ChildrenGetter = delegate (Object tx)
{
return ((Request)tx).Children;
};

// Populate HexBoxes when the selection changes.
// Populate HexBoxes when the selection changes
tlvRequestHistory.SelectionChanged += (sender, eventArgs) =>
{
if (tlvRequestHistory.SelectedIndex == -1) return;
Expand All @@ -110,11 +104,7 @@ private void InitializeTreeListView()
};
}

/// <summary>
/// Validate provided device path.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
// Validate provided DeviceName
private void tbDevicePath_TextChanged(object sender, EventArgs e)
{
Guid guid;
Expand Down Expand Up @@ -142,11 +132,7 @@ private void tbDevicePath_TextChanged(object sender, EventArgs e)
tbDevicePath.BackColor = Color.MistyRose;
}

/// <summary>
/// Validate that provided IOCTL is legitimate.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
// Validate that provided IOCTL is legitimate
private void tbIOCTL_TextChanged(object sender, EventArgs e)
{
Point toolTipCoords = tbIOCTL.Location;
Expand All @@ -169,12 +155,15 @@ private void tbIOCTL_TextChanged(object sender, EventArgs e)

private void btnSend_Click(object sender, EventArgs e)
{
uint fa_mask=Convert.ToUInt32("20000000", 16);
if (tbAccessMask.Enabled == true) {
uint fa_mask = Convert.ToUInt32("20000000", 16);
// if Access mask is enabled use that value
if (tbAccessMask.Enabled == true)
{
fa_mask = Convert.ToUInt32(tbAccessMask.Text, 16);
}
else
{
// otherwise gather the human readable ACL
switch (cmbACL.SelectedItem)
{
case "ANY_ACCESS":
Expand All @@ -189,13 +178,14 @@ private void btnSend_Click(object sender, EventArgs e)
case "WRITE_DATA":
fa_mask = Convert.ToUInt32(FileAccess.Write);
break;
default:
default:
fa_mask = 0;
break;

}
}
Debug.WriteLine("\nAccess Mask: "+fa_mask);
Debug.WriteLine("\nAccess Mask: " + fa_mask);

SafeFileHandle sfh = CreateFile(
tbDevicePath.Text.Trim(),
dwDesiredAccess: (FileAccess)fa_mask,
Expand All @@ -217,10 +207,12 @@ private void btnSend_Click(object sender, EventArgs e)

if (sfh.IsInvalid)
{
// invalid DeviceName
errorCode = Marshal.GetLastWin32Error();
}
else
{
// create buffer with specified content in memory
long hbInputLength = ((DynamicByteProvider)hbInput.ByteProvider).Length;
MemSet(Marshal.UnsafeAddrOfPinnedArrayElement(inputBuffer, 0), 0, (int)hbInputLength);

Expand All @@ -232,18 +224,19 @@ private void btnSend_Click(object sender, EventArgs e)

long hbOutputLength = ((DynamicByteProvider)hbOutput.ByteProvider).Length;
MemSet(Marshal.UnsafeAddrOfPinnedArrayElement(outputBuffer, 0), 0, (int)hbOutputLength);
// execute DeviceIoControl request
DeviceIoControl(sfh, ioctl, inputBuffer, inputSize, outputBuffer, outputSize, ref returnedBytes, IntPtr.Zero);

// gather error code
errorCode = Marshal.GetLastWin32Error();
sfh.Close();

// update Input/Output views
DynamicByteProvider requestData = new DynamicByteProvider(inputBuffer);
hbInput.ByteProvider = requestData;

DynamicByteProvider responseData = new DynamicByteProvider(outputBuffer);
hbOutput.ByteProvider = responseData;
}

// update request view
Request newTx = new Request();
newTx.RequestName = String.Format(
"0x{0:X} ({1:X4}-{2:D5})",
Expand Down Expand Up @@ -283,11 +276,7 @@ private void btnSend_Click(object sender, EventArgs e)
return;
}

/// <summary>
/// Mark the selected request as a favourite.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
// Mark the selected request as a favourite
private void btnStarRequest_Click(object sender, EventArgs e)
{
if (tlvRequestHistory.SelectedIndex == -1) return;
Expand All @@ -300,11 +289,7 @@ private void btnOpenDB_Click(object sender, EventArgs e)
// ToDo
}

/// <summary>
/// Filters results in the Request History view (TODO).
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
// Filters results in the Request History view (TODO)
private void tbFilters_TextChanged(object sender, EventArgs e)
{
tlvRequestHistory.ModelFilter = null;
Expand All @@ -314,11 +299,7 @@ private void tbFilters_TextChanged(object sender, EventArgs e)
});
}

/// <summary>
/// Shows the About window.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
// Shows the About window
private void btnAbout_Click(object sender, EventArgs e)
{
if (Application.OpenForms["AboutForm"] as AboutForm == null)
Expand All @@ -333,6 +314,7 @@ private void btnAbout_Click(object sender, EventArgs e)
}
}

// parse and validate Access Mask
private void tbAccessMask_TextChanged(object sender, EventArgs e)
{
Point toolTipCoords = tbAccessMask.Location;
Expand All @@ -354,12 +336,14 @@ private void tbAccessMask_TextChanged(object sender, EventArgs e)

}

// update input panel when the InputSize is changed
private void nudInputSize_ValueChanged(object sender, EventArgs e)
{
DynamicByteProvider dbpData = new DynamicByteProvider(new byte[(int)nudInputSize.Value]);
hbInput.ByteProvider = dbpData;
}

// update output panel when the OutputSize is changed
private void nudOutputSize_ValueChanged(object sender, EventArgs e)
{
DynamicByteProvider dbpData = new DynamicByteProvider(new byte[(int)nudOutputSize.Value]);
Expand All @@ -372,6 +356,7 @@ private void hbInput_ByteProviderChanged(object sender, EventArgs e)
//nudInputSize.Value = (int)dbpData;
}

// show the settings form
private void btnSettings_Click(object sender, EventArgs e)
{
if (Application.OpenForms["SettingsForm"] as SettingsForm == null)
Expand All @@ -386,6 +371,7 @@ private void btnSettings_Click(object sender, EventArgs e)
}
}

// enable/disable access mask
private void chkEnableAccessMask_CheckedChanged(object sender, EventArgs e)
{
if (chkEnableAccessMask.Checked == true)
Expand All @@ -398,7 +384,7 @@ private void chkEnableAccessMask_CheckedChanged(object sender, EventArgs e)
tbAccessMask.Enabled = false;
cmbACL.Enabled = true;
}

}
}
}
22 changes: 12 additions & 10 deletions ioctlpus/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ static class Program
{
public static byte[] StringToByteArray(string hex)
{
// Convert a string to a byte array
if (hex.Length % 2 == 1)
{
throw new Exception("The binary string cannot have an odd number of digits");
Expand All @@ -27,13 +28,14 @@ public static byte[] StringToByteArray(string hex)

public static string ByteArrayToString(byte[] ba)
{
// Convert a byte array to a string
string hex = "0x" + BitConverter.ToString(ba).Replace("-", " 0x");
return hex;
}

// args options
class Options
{
// args options
[Option("cli", HelpText = "Run IOCTLpus in CLI mode.")]
public bool Cli { get; set; }

Expand All @@ -60,9 +62,7 @@ class Options

}

/// <summary>
/// The main entry point for the application.
/// </summary>
// The main entry point for the application.
[STAThread]
static void Main(string[] args)
{
Expand All @@ -73,7 +73,7 @@ static void Main(string[] args)
if (opts.Cli)
{
Console.WriteLine("IOCTLpus CLI Mode\n--------------------\n");
// hanlde some required parameters
// hanlde some required arguments
if (opts.Guid == null)
{
Console.WriteLine("Required option 'guid' is missing.");
Expand All @@ -89,7 +89,7 @@ static void Main(string[] args)
Console.WriteLine("Required option 'input' is missing.");
return;
}
// if we gathered every parameter we can proceed and perform the IOCTL request printing out the result
// if we gathered every required arguments we can proceed and perform the IOCTL request printing out the result
uint fa_mask = Convert.ToUInt32(opts.Access_mask, 16);
SafeFileHandle sfh = CreateFile(
opts.Guid.Trim(),
Expand All @@ -111,13 +111,14 @@ static void Main(string[] args)
if (sfh.IsInvalid)
{
// print out last occured error (e.g DeviceName not found)
string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
Console.WriteLine(errorMessage);
return;
}
else
{
// grab the opts.Input_buffer transform it into a byte array
// grab the opts.Input_buffer and transform it into a byte array
byte[] hbInput = StringToByteArray(opts.Input_buffer);
long hbInputLength = hbInput.Length;
MemSet(Marshal.UnsafeAddrOfPinnedArrayElement(inputBuffer, 0), 0, (int)hbInputLength);
Expand All @@ -127,12 +128,13 @@ static void Main(string[] args)
inputBuffer[i] = hbInput[i];
}
MemSet(Marshal.UnsafeAddrOfPinnedArrayElement(outputBuffer, 0), 0, (int)outputBuffer.Length);
// send DeviceIoControl
DeviceIoControl(sfh, ioctl, inputBuffer, inputSize, outputBuffer, outputSize, ref returnedBytes, IntPtr.Zero);
// gather last occured error (e.g Access Denied)
errorCode = Marshal.GetLastWin32Error();
string errorMessage = new Win32Exception(Marshal.GetLastWin32Error()).Message;
sfh.Close();
// print out returning values
// print out DeviceIoControl returning values
Console.WriteLine("GUID:\t\t" + opts.Guid);
Console.WriteLine("IOCTL:\t\t0x{0:X}", ioctl);
Console.WriteLine("Input Buffer:\t" + ByteArrayToString(inputBuffer));
Expand All @@ -141,9 +143,9 @@ static void Main(string[] args)
Console.WriteLine("Error:\t\t0x{0:X} - " + errorMessage, errorCode);
}
}
// run IOCTLpus in GUI mode
else
{
// run IOCTLpus in GUI mode
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
Expand Down
Loading

0 comments on commit 3a2ec1c

Please sign in to comment.