ads

Functions and Modules (Python for Hackers Part-6.2)




Iteration

There are some instances wherein you may find it to redundant to write the same code multiple times to do a similar function, such as checking different IP addresses or analyze different ports. For this reason, you may want to use a for-loop instead to iterate the same code for different elements. For example, if you wish to iterate a code for the subnet of IP addresses from 192.168.0.1 through 192.168.0.254, you can use a for-loop that contains a range of 1 to 255 to display the entire subnet. Take a look at this sample code to see how it is done: 


If you want to iterate the same code through a list of known ports to analyze a system’s vulnerabilities, you can iterate through a list of elements that you want to check instead. Take a look at this example:


Exception Handling


Even if you are already able to write a program with correct syntax, you may still go through some errors upon execution or runtime. For example, when you divide anything by zero, you are likely to experience a runtime error because Python knows that it is impossible to do so. When you attempt to perform this action, Python might return with this output:

If you want to fix the error while you are already running your code, Python’s ability to perform exception handling will come in handy. Using the example above, you can use the try or except statement in order to make use of the exception handling so that when the error happens, the exception handling feature will catch the error and then print the message on the screen. 

Take a look at this example:


If you want to see where the error specifically happened in your script, you can use the following code instead:


Modules


If you want to make use of the functions that you have already created from another program to another, instead of having to rewrite the entire code, then you can use of modules.


The simplest way to make modules is to create a file that contains all the variables and functions that you may need to use in a future program and then save it as a .py file. Alternatively, you can also create your modules in a language in which the Python interpreter is written, such as the C language. You can also have a module imported by another program and use all the functionality saved in there, which is the same as you use the standard libraries that you use in Python.

Take a look at how you can use a standard library module through this example:

Save this code as module_using_sys.py an then run it on the interpreter prompt. You should get this output:



What happened here is that you first imported the sys module. By using the import statement, you are able to tell python that you want to use a module that contains the functionality that is related to the Python environment. When this programming language executes the statement, it will then look for the .sys module. Since this is a built-in module, Python knows the location where it can be found. 

In any case you are trying to import a module written in Python, the interpreter will then search all directories that are listed in the variable sys.path. Once it is found, the statements found in that module will be run, making it available for you to use. This initialization process only takes place the first time you import a module.

Sys Module

Python has a built-in module that provides you access to all objects that the programming language’s interpreter maintains or uses. Called the sys module, this module includes command line arguments, maximum size of integers that can be used, flags, path hooks, as well as other available modules.


Being able to interact with the sys module will allow you to create different scripts that you can use for different hacking purposes. For example, you may want to analyze different command line arguments during runtime. If you are going to build a scanner to discover system vulnerabilities, you may want to pass a filename as a command line argument, which can be done by using the list sys.argv which is comprised of all the command line arguments. Take a look at this sample code to see how this module is used:



When you run this piece of code, you will see that the command line argument has been analyzed and then Python prints out the results on the screen. The output will look like this:


OS Module


Python’s OS module provides a great deal of routines for different operating systems, such as Mac, Posix, and NT. Using this module, you can allow the programming language to interact on its own with the file-system, permissions, user database, and different OS environment.

Using the previous example, you, the user, submitted a text file as a command line argument. However, it will also be of value if you can check if the file that you have passed exists and the current user of the machine you are targeting have the necessary permissions to read that file. To
determine this, you can create a code that will display an error message if either one of the condition is not met. You can use this code to do that:



To check your code, you can attempt to read a file that is not available in the system, which will cause the script you just typed in to display the error. Afterwards, you can enter a filename that will be successfully read. Finally, you can create permission restrictions and see that the script that you have created print out a conventional Access Denied message:




Post a Comment

0 Comments