Python modules for networking

Our fun little hello_world program needed nothing more than Python to interpret your sophisticated code. However, you've no doubt realized that hello_world doesn't really serve the pen tester too well. For one, all it does is display an overused cliche. But even if it was handier, there are no imports. In terms of capability, what you see is what you get. Truly unleashing Python happens when we expose capability with modules. If I were to guess what kind of task you'll be employing the most, I'd guess networking.

There are many options to the Python coder to make his or her script chatty with the network. The key to understanding modules in general is by organizing them in terms of layers or levels. Lower-layer modules give you the most power, but they can be difficult to use properly; higher-layer modules allow you to write code that's more Pythonic by taking care of lower constructs behind the scenes. Anything that works at a higher layer of abstraction can be coded with lower layers, but typically with more lines of code. Take, for example, the socket module. The socket module is a low-level networking module: it exposes the BSD Sockets API. A single import of socket combined with the right code will allow your Python program to do just about anything on the network. If you're the ambitious type who is hoping to replace, say, Nmap with your own Python magic, then I bet your very first line of code is simply import socket. On the high-level side of things, you have modules such as requests, which allows for highly intuitive HTTP interaction. A single line of code with requests imported will put an entire web page into a single manipulable Python object. Not too shabby.

Remember, anything that works at a high level can be built with low-level code and modules; you can't use high-level modules to do low-level tasks. So, let's take an example. Using Python in pen testing contexts will make heavy use of socket, so let's throw together a quick and dirty client. With only 11 lines of code, we can connect and talk to a service, and store its response.

Keep in mind that socket, being low-level, makes calls to socket APIs of the operating system. This may make your script platform dependent!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.181.52