Greetings everyone, I’m red_riot, Hacking A Rise’s very own Forward Defence Minister, and today I am here to teach you all python programming with a specific focus on sockets. After we learn the basics of working with sockets we will move on to working with different python modules for dealing with packets like Scapy, but before we get into any programming at all lets take the time now to properly setup our development environment. I will assume that you are currently using some form of Debian based operating system, in my case I am using Ubuntu in a virtual machine but rest assured everything should work the same for you as long as you are using a Debian OS such as Kali Linux or Raspbian. So fire up your terminal and lets get started!
The very first thing you should do is make sure your operating system’s package manager is up to date, remember the following four commands: sudo apt update (What this does is checks for any available updates), sudo apt upgrade (This upgrades packages but doesn’t remove old ones), sudo apt autoremove (This remove any old packages that are no longer needed), and an all in one command sudo apt full-upgrade (This upgrades your packages and removes any obsolete packages in one go).
Now that we have all our packages up to date the next thing we need to do is install and use a text editor of choice. While you may use any text editor that you feel comfortable with, I prefer to use emacs and will be using it for the purposes of it’s tutorial. Because emacs is very complex I am just going to use a fresh install with out of the box configurations so that you can follow along if you have never used emacs before. Also I should point out that there are two versions of emacs on linux, one version is emacs-gtk, this version of emacs includes a graphical user interface that generally many non technical people find easy to work with. The other version is emacs-nox this version of emacs runs straight from the command line and does not include the user interface, we will be using version emacs-nox for this tutorial because this is the version of emacs you can run if you decide to ssh into your debian box. To install emacs run the command sudo apt-install emacs-nox (if you prefer the gtk version run the command sudo apt-install emacs-gtk). Once emacs has been installed fire it up by typing emacs into the command line, you should get a screen that looks like this:
Now that you have it installed you will have to get the most up to date packages from melpa by updating your configuration file. Emacs is a powerful text editor but it can get overwhelming if you’re not careful, the first thing you need to know is the key bindings, I will describe them using the way you are most likely to see them in documentation, so the first keybinding I will describe is the cancel keybinding, if ever you accidentally type something and want to cancel you should press Ctrl-x and then Ctrl-g, you can also just type g right after you type Ctrl-x and emacs will understand the command the same as typing Ctrl-x first then Ctrl-g. From now on anytime we are in emacs I will describe the ctrl key using just the letter C so again if you want to cancel a command in emacs type C-x g. Now that you can read the keybindings the next thing you should do is type C-x C-f that keybinding should open up a dialog near the bottom of the screen that says Find file: delete the text there and type ~/.emacs this will open your .emacs file this is the holy grail of your emacs configuration and you actually configure your editor by writing lisp code so get used to it. I will discuss the implications of this some other time but for now reproduce the following code in your new .emacs file:
Be very careful of the single quote characters and be sure to reproduce this code exactly as you see it. Once you’re done type C-X C-s to save the current file and then type C-x C-c to quit emacs. Now I know we just opened up emacs made a quick change and quit but lets fire up emacs again just to make sure everything worked properly, if you made any errors in you .emacs file this is the time emacs will catch it and bring it to your attention, hopefully you don’t have any errors. Now that we are back inside emacs the next major set of keybindings you need to learn are the meta keybindings, we’ll take it slow here anytime I say to type in the meta key I am referring to the left alt key, again the reason why I don’t just say Alt is because in emacs documentation it will likely be called the meta key. Using the meta key is a lot like using the control key, this time we are going to type M-X package-list-packages after you press enter you should see output that looks like this:
It may take a second for all of the melpa packages to load but look at all the packages you can add to customize your emacs with, all these packages can do many different things from adding a mode for working with specific programming languages so that you can auto complete, to adding themes for different colors, even to playing games right from emacs like tetris, chess, or flappybirds, yes you read that right you can play games right from your text editor, emacs is very powerful and is like a mini operating system. Now because the package list is so long you can press C-v to scroll down and M-v to scroll up quickly. Once you see a package you like press enter and you should see another frame open up, to switch between the frames press C-x o then press tab to highlight the Install link and press enter to download as demonstrated here:
After you press enter emacs will prompt you to see if you really want to install so type in y for yes or no in case you change your mind, after emacs has downloaded the package from melpa you can press C-x 0 (mind you thats the number zero not the letter o like we use to change frames) that should take you back to the package list. If you accidentally closed the package list frame you can press C-x b to switch between all the open frames or rather buffers that you have recently opened, from now on I will refer to them as buffers. Take the time now to browse the packages you would like to install, I would recommend any related to python you find interesting. If you would like to change your theme type M-x customize-theme and you can change the color scheme of emacs if you would like. Now that you have setup emacs lets go on to install python 3 if you haven’t already!
The easiest way to check if you have python is to type python –version or python3 –version. Most likely if you are on Kali python 3 is already installed. This tutorial will only use python3 as python2 is being deprecated however there are ways to write python3 that is compatible with python2 that I will cover later in this tutorial series. If you already have python3 installed skip this step if not simply type sudo apt install python3 into the command line. The final thing we want to install for this branch of the tutorial is pip python’s very own package manager. To check if it is already installed you can type pip3 help, if it is not installed you can type sudo apt-get install python3-pip and you should be good to go. Now since were going to be using the standard library of python for a while lets just verify pip is working by installing a python module that we will be using in the future called Scapy. Scapy is a packet management library that is useful for analyzing and launching many different network attacks install it by running the command pip3 install –pre scapy[complete] this will likely install all the libraries you need in order to work with scapy. Also if you don’t have tcpdump installed then you should run sudo apt-get install tcpdump.
Now a couple of sanity checks, first lets fire up the python REPL, the REPL or Read Eval Print Loop is an interactive python terminal where you can evaluate small snippets of python code at a time, it’s great for quickly trying out ideas or experimenting with a python module before you build a full script, type in python3 and the REPL should fire up! Try importing the module scapy we just downloaded by entering the following:
If there are no errors then everything has been downloaded successfully. Lets do one last sanity check with emacs just to make sure, if you haven’t already you should create a directory for where you will house all of your python scripts, or at least the ones for this tutorial. I usually keep mine in Documents/workspace/pyt/ or something similar. Navigate to the directory you want using cd (change directory) or create a new directory you want to use by typing mkdir and the name of the folder you want. Once you are in your python workspace, lets fire up emacs one last time for our first python program, type emacs hello.py and for now just type in print(“”) and put any text you want in between the double quotes. Once you’ve done that save your file C-x s then you can fire up your shell from emacs by typing M-x shell (Yes you can run a shell from inside emacs, awesome right, it works on windows too!) type python3 hello.py and you should see the following output: