[ Curiosity,Experimentation ]

Random stuff from the parallel universe of Ones and Zeroes

Posts Tagged ‘Python’

A Case of Regular Expression [Python]

Posted by appusajeev on March 30, 2010

Regular expressions can be so cool at times,when there a is need to search and find certain text patterns.Today there was this situation where it was needed to extract the Microprocessor Based Design(ironically,theres absolutely no design involved :D) external marks of 66 students from different 66 web pages(as the result is obtained for each student separately  from the university site).
Python was of course the obvious choice. 🙂
First i thought of using  file operations and string matching to do the job but later decided use regular expressions. I tried to find all occurrences of the string between  “<td>MICROPROCESSOR BASED DESIGN</td>” and the first following “</td>” section in the html code(that was the pattern to be matched in this case if you observe the html code) of each page.
This would produce the section containing the external mark but also would contain certain html tags in between which needs to be removed,the mark has to be extracted from the result and for this,usual string matching would suffice. The mark extracted is saved into a file.

The regular expresion for this would be “<td>MICROPROCESOR BASED DESIGN</td>[a-zA-Z0-9=<>\s\n\t/]*<\td>“. Or better still,you could also try “BASED DESIGN</td>[a-zA-Z0-9=<>\s\n\t/]*<\td>
The expression cannot start with “DESIGN” cuz it would match COMPUTER ORGANIZATION AND DESIGN,another subject !
Symbols <>,\n,\t,/ need to be be included because the section to be extracted contains html tags.

Heres the complete code. As obvious,regular expressions make things a lot easier as opposed to the usual approach.

Regular expression put to use

Regular expression put to use

Posted in Python | Tagged: | 6 Comments »

An alternative to If-Else and Switch [Python]

Posted by appusajeev on February 4, 2010

You know there are times (especially in lab programs) when we have to print a menu on screen,get the choice from the user,do the usual,mechanical if-else-if or switch checks to invoke a function.
The same could be achieved with very little effort from Python avoiding condition checks

Create a list(or a tuple) containing 3-element tuples as the list members,the first member of the tuple being the string to be printed,the second being the function to be invoked and the third member being a list containing the arguments to be passed to the function.
Print the 0th element of the tuples present in the list in order,read a choice from the user and invoke the function as shown in the code.

Since python supports functional paradigm,functions are treated as first class members and hence they can inserted into lists,tuples etc
For dealing with function arguments,it would be good to have the function accept a list as its argument from which it can extract the needed arguments (which,well maynot be a good idea in cases we dont know the arguments to be passed to the function beforehand)
In case the functions that do accept any arguments,make the function accept a dummy lust as argument.

Heres an example based on the good ol’  Binary Search Tree

An alternative to If-Else and Switch

An alternative to If-Else and Switch

Posted in Python | Tagged: | 5 Comments »

An Asynchronous Socket Class in Python

Posted by appusajeev on November 23, 2009

The default socket module can be a pain certain times,especially when you are working with asynchronous/event driven domain.Handling asynchronous communication can be a bit messy in the native python environment .The accept() and recv() methods would only relinquish control after a connection has been accepted or data has arrived. This would sometimes choke the flow of control in the program..
So i thought of creating a socket class utilizing threads internally to handle asynchronous communication and this is what i have finally arrived at.
The Winsock control in VB provides a clean yet powerful interface to handle asynchronous socket communication.I have tried to provide an interface similar to the Winsock control. I have also incorporated callback mechanism by which a function could be invoked upon data arrival.This avoids the need to poll the socket every time. As of now i have just provided the methods working upon TCP,UDP is not yet supported though adding UDP support is trivial ! 🙂

Download Source

An Asynchronous Socket Class in Python Source

An Asynchronous Socket Class in Python

Posted in Asynchronous_Socket, Python | Tagged: , | 3 Comments »

Optimized Port Scanner [With Threading]

Posted by appusajeev on August 13, 2009

I had earlier presented here a Port Scanner program written in Python (In fact,it was my maiden post !). I had stated that the major bottleneck associated with its performance was the amount of time taken to complete the scan for even a small range of ports. The connections were made sequentially,one after the other and took mammoth time to verify the entire ports in the list.

Well,that problem is solved now by using threaded execution,the use of separate threads for processing each port. The entire ports are processed simultaneously by separate  threads and this has brought down the execution time to a great extent.Hence i am presenting here, an optimized version of the port scanner which i had earlier presented.

A thread is a sequence  of instructions that are executed concurrently with other instructions/threads. Python provides two modules to implement threading-Thread module and Threading module. I have used the latter module and has overridden the run() method in the Thread class(by subclassing it)present in the module Threading.

Download Source code

Or see the source below:

Port Scanner-Optimized Source

Port Scanner-Optimized Source

Here the scanner in action:

”]Port Scanning [Screenshot]

I have used microsoft.com as the example. Notice from the scan that port 443 is open. Its the port for HTTPS.

Suggestions are welcome

Port Scanner -Scanning

Posted in Port Scanner, Python | Tagged: , , | 9 Comments »

A Minimal Cross-platform Port Scanner in Python

Posted by appusajeev on July 6, 2009

A  Port Scanner is one of the most basic tools in the arsenal of an attacker/administrator. Basically a port scanner is a tool which can identify the open ports on a remote system. Given an ip address and an optional port range,a port scanner tries to connect to the ports on the machine sequentially and a successful connection means an open port. Open ports can sometimes prove to be a juicy entry point for an attacker,when there is a vulnerable application/service on the other side with an open port.

With that basic idea,presented here is a minimal port scanner i developed Python.

No rocket science,pretty straight forward code. The source can be altered to make it a portsweeper even

You can download the source here

The source is displayed below. And remember,this is a simple portscanner .So no advanced features

Port Scanner Source

Port Scanner Source

First,the program checks for the right number of command line arguments which includes the remote IP address,starting port and ending port. After extracting these parameters , a TCP socket is created each time to connect to the different ports sequentially on the same machine. A successful connection means that that port is open and some application/service is listening to it.

Currently what bothers me is the time taken to complete the scan. It takes quite some time :d

Suggestions and bug reports are welcome…….

Posted in Port Scanner, Python | Tagged: , | 4 Comments »