C/C++ for Hackers: Part 10 (System Commands)
welcome back, fellow hackers! finally, i am releasing part 10 in my series on C++. this will surprisingly be a short article. in this part of my series, we will be looking how to execute system commands!
but first, a disclaimer:
I AM NOT RESPONSIBLE FOR ANY DAMAGE THAT YOU DO TO EITHER YOUR OWN COMPUTER OR SOMEONE ELSE'S! This post is for educational purposes only! do any of the following outside your lab AT YOUR OWN RISK!
so, let's get started!
on windows, it isn't as useful. but in Unix based systems where the terminal still rules, executing system commands in a virus is vital. we could for example make our virus update itself using the wget command in linux distros. or make a fork bomb and execute it, to make the target system crash after some time (viruses that do this are known as logic bombs.) or do permanent damage using the dd command. the possibilities are almost endless! one thing we can't do unfortunately is make the virus switch directories.
our first step is to fire up Ubuntu and our newly installed IDE called codeblocks. i am on a holiday when i wrote this and i only have my windows laptop., so i will use codeblocks for windows, but the code i am going to write should be the same for you. but for my tutorial, i will execute the "ipconfig" command on windows as a demonstration. you can use the "ifconfig" instead if you want, or any other command.
the function we are going to use is the "system()" function, located in the stdlib.h library. the system function take a string as a parameter. that string is the command we want to execute. so if we for example want to execute the "ifconfig" command, we would type our function as followed:
i already wrote an example, it is located here.
but before we can go and compile our code, we first need to make a project. a project is a way for CodeBlocks to order your code. to make a project, simply open codeblocks, and on the top left, go to file -> new -> project. a popup wizard will open like below:
we will select a console application so we can view the output of our command. so select it, then click "go". then you will be prompted with a welcoming screen. then click next. then you will be asked to choose either C or C++. we will select C++. then you will be asked to name your project and where to save it. you can name it whatever you want, but make sure to use a underscore instead of a space! (this is to avoid problems with the debugger). for example: Phoenix750-Tutorials.
as for where to save it, if you read part 9 of my series, you know we made a folder where we can save our projects, so you should save your new project there. if everything is set up, click next.
then you will see advanced options. leave these at their defaults and just click finished.
congratulations, you just made a new project! but how do you access the main.cpp file? on your left, you will see what is known as your "workspace". there you will also see the name of your project. click on the + symbol, and a folder will appear called "sources" then click on + before sources, and you will see your main.cpp file! to open it, just double click it. but it should be open by default though.
then you can just copy/paste my code and compile/run it in debugging mode. to do the latter, just navigate to the upper bar and click on the gear with the "play" symbol. (sorry for my poor paint skills :P)
and then, you will see the output of the ifconfig command or in my case, the windows ipconfig command!
executing system commands is crucial for computer viruses, especially in Unix based systems. remember that system() can execute any system command, even destructive ones (especially if you have root permission!), so be creative! but be aware of the disclaimer!
also, if you want to know some destructive commands check out This list.
in the next few tutorials, we will start making small viruses that utilize the system() function. so cya then!