I am often asked by CS students interested in a career of pen-testing, what programming language they should learn? Is there one that is best suited for pen-testing? My answer is Python — hands down. It is a very easy language to learn and it is very powerful. When a pen-tester in the field needs to whip up an automated tool, it is usually done in Python because it is fairly easy to code up working prototypes on the fly. I will demonstrate by whipping up a Python key-logger in just a moment.
Another reason you should learn Python is that many pen-testing tools are written in Python. So if you ever need to take an existing tool and extend its capability, you will have to understand the Python language. Python is also available on many pen-testing platforms such as BackTrack and Kali linux.
As I said, learning Python is very easy because it is a well documented programming language. Almost everything you need to learn the language is available at python.org. There are also very good tutorials available here and here. Once you get the basics down, you will be amazed at the tools you can create. Allow me to demonstrate with a simple Python key-logger:
First, a word of warning. This key-logger only works on Windows machines and it will log every key stroke a user presses. Please do not load this key-logger on anybody’s machine but your own. This key-logger is for educational purposes only. Besides, it is not very stealthy.
Next, you will need to install Python on your Windows machine. You can download Python here. I am using Python 2.7.6 for a Windows 7 64-bit machine. Python 2.7.6 is pretty stable so I prefer it to versions 3.x. Be sure you select the proper installer for your Windows machine (i.e. 32-bit or 64-bit).
After installing Python, you will need to install a library called pyHook. pyHook is a wrapper for global input hooks in Windows. It wraps the Windows SetWindowsHookEx API. You can get the appropriate version for your version of Python and Windows 32-bit or 64-bit versions here. Scroll all the way down until you get to the pyHook section. For my machine, I installed the pyHook‑1.5.1.win‑amd64‑py2.7.exe version.
After installing pyHook, fire up a command prompt (cmd.exe) and cd into the C:\Python27 directory, then type ‘python’ at the prompt (without the quotes) you should see:
the three right angle brackets (>>>) is the prompt for python. Type ‘import pyHook’ then enter. You should see no errors if pyHook installed correctly:
Before coding up the key-logger, I visited the documentation page to learn how to use pyHook. You should too. Play with pyHook from the Python command shell to get a feel for what you can do with it; see if you can cobble your own key logger together before looking at my implementation. If you need further hints, see this pyHook wiki.
Here is my implementation:
That is it! It only took about 20 lines of code! That is the power of Python. To run your key-logger, make sure you are in your Python directory (usually C:\Python27) and type the name of your key-logger (I named mine logger.py):
Now open up another command prompt and type, ‘dir’, and ‘whoami’.
Open up notepad and type anything you want:
Once you are done, your logging file should contain every key you typed:
With a little reading and some practice, Python can help you become that evil genius you’ve always aspired to be. That is the power of Python.