In the first lecture of creating the Windows application, we will gather following information
operating system and hostname, current user and privileges, network configuration, current directory,
current process name, process id and the executable path of current process.
All of those information are crucial in red teaming, penetration testing and hacking.
We will send all of those information to our attacker server. As an additional idea, you can detect
the antivirus software installed, but let's take it as Homework and do it by yourself.
To do so, we will create a class called GeneralInfo or you can call it whatever you like, and
inside the class we will define functions to gather specified information and assign them to corresponding
variables of class so we can use them later on.
In C Sharp, there are many predefined classes that exist to retrieve information from the system.
For example, and Environment class for enumerating the environment, which contains username info,
current directory info, operating system info, etc., and process class for determining process name,
process id, executable path etc.
If you are new to these concepts, I highly encourage you to learn more about them before continue.
So let's start coding.
Here is our dummy application.
We will start coding by
creating a new file, it will be our new class.
I will name it as info.cs, but you can you can give any name you want and copy the contents
of a program.cs file into the new file you have created.
And change the class name, I will use generalinfo as my class name.
And remove that trivial code, so your class is ready.
What's next?
For the next step, we will create variables for storing data.
We will need a lot of variables.
Here are them.
A string variable for storing the operating system, another string variable for storing username.
An integer variable for
storing the process id, a boolean variable for the storing the privilege information to check if
our user is administrator or not. For the next step, we will create a main function that will gather
all of those information.
It will be our constructor if you are new to this concept, constructor is a method that will be called
whenever an object of class is created.
So it's best option to perform initial operations. To create a constructor
simply create a function and name it with the same name with the class general info.
So this is our constructer.
Let's start to gather information.
We will start with
operating system.
To gather operating system version, we will use the OS version, the property
of the environment class.
It will return the operating system version, but its data type is not string, its type is
system.operating system in order to convert it to String
we will use ToString method.
To gather username information, we will use the username property of the environment class, and for
the current directory, we will use the current directory property of the environment class.
And for proces name, we will use process class to do so, we need the name space.
So let let's define it first.
In order to use process class directly, we need to define a system that diagnostics namespace at the top
of our file and then we would use the get current process method of the process class.
The current process methods.
This method will return the information about the process and people we will use to process name
property of the Process class to retrieve process name information from the system.
And for the Process id, we will use Process class again with the current process method
and we will use the Id property.
And for hostname, we will use DNS class, but in order to use it we need to define a
System.Net namespace.
Let's do it at the top of our file System.Net
We will use get hostname method of the DNS class.
So this method will return the current hostname and for the IP version for address, we will use the
DNS class again.
But this time we will use get lost by name methods, post by name.
And this method returns the DNS information of given hostname.
So we will use our hostname as argument.
And we will get the first element of the address list property.
Hopefully it will be the true one and we need to convert its data type to string.
To do so, we will use the ToString method.
And in order to check if our user has administrator privileges or not, we will use Windows Identity
and Windows principal classes.
Here is the sample code for it.
In the first line.
We are getting the windows are against the object of the current user and the second line, we are enabling
all code to check if our user is in a user group or not.
We are just enabling this feature.
And the third line with the IsInRole method, we are deciding if our user has administrator privileges
or not.
So we have completed our task in order to check if everything are OK, switch back to your
program.cs file and create an object of our class
I will call it info object, but it doesn't matter, the names doesn't matter.
It's new generalinfo.
And let's check.
If the IP version four address variable of the general info class
will return our IP version four address, open up your terminal and type dotnet run.
Ignore the warnings and look at the end of the output, as you can see, here is our IP version four
address excellent. As an advice to not try to memorize all of the code, just take notes and use them
whenever you need.
That's it for this lecture.
See you in the next one.