Intro to C#
Many offensive tools these days are written in C# and I have finally decided it is time to jump on the bandwagon. The move to C# isn’t anything new, frankly I am jumping on the bandwagon probably 3 years late. I think a big reason it has taken me a while is because PowerShell has just been way easier and approachable to learn and use. The “whole” compiling thing has always seemed kind of scary and nothing is more frustrating then errors preventing you from compiling. This article will go through some of the basics of C# development and help knock down some of the perceived barriers and difficulties that I had. This article will be most helpful to those that know how to write a little bit of Python, Bash, or PowerShell but are new to the world of C# and/or compiled languages in general.
So what is C# and how does it compare to Python or PowerShell?
Well lets just go over some basic code snippets…
Lets say I want to print to screen “I love pizza!”
In Python we would write:
print("I love pizza!")
We would save that file with a .py ending, say “pizza.py”, and then can execute it running python ./pizza.py
In PowerShell we would write:
Write-Host "I love Pizza!"
We would save that file with a .ps1 ending, say “pizza.ps1”, and then can execute it from a PowerShell terminal by running ./pizza.ps1
For C# we would write:
Console.WriteLine("Hello World!");
Save that to a file with a .cs ending, pizza.cs, and then compile it using “csc.exe”. To find csc.exe run the following snippet:
Get-Childitem -Path C:\Windows\Microsoft.NET\Framework\*\csc.exe
And then to compile we run:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe ./pizza.cs
As we see from the output above we get an error. There is actually several errors preventing us from compiling but in short what I recommend is to always start with the basic “Hello World!” skeleton:
Then whatever you want to run put where the “Hello World” statement is. You may also notice the using System;
declaration at the beginning. This is similar to Python when you import a module like math import math
. So starting with this C# skeleton and adding our pizza code we should now have:
Compile that and look at that, no more errors!
The compiling process takes our pizza.cs file and gives us pizza.exe which we can now run:
While yes its a ton of steps and much more difficult than PowerShell or Python, hopefully I have helped show its not too bad. Also one benefit of the errors before compilation is you can have a lot more faith that the executable will run correctly while with Python or PowerShell you find out when you run the code that you missed a double quote somewhere.
One huge tip I have is to install Visual Studio (different than Visual Studio Code). This gives you that basic C# skeleton for every project you create, helps you see the errors in your code before trying to compile, and provides an easy place for you to compile your project without needing to go searching for csc.exe every time.
Not only is Visual Studio extremely helpful for creating your own projects but it is also very helpful when working on projects other people have made. For example say we want to modify the code base for Seatbelt. We can just clone (or download) this repository and, if Visual Studio is already installed, just double click the .sln file and Visual Studio will load up the code base for us.
Then to compile this we can just hit Build and then Build Solution.
So ya, Visual Studio is kind of a must!
Anyways if you have been on the fence about C#, hopefully this article has encouraged you to make the plunge. My next article will dive deeper into doing some actually pentesty stuff with C# including obfuscation!