Brute Force A Website Login In Python


Screen shot of python script running brute-force attack on my college’s website.

This post gives brief introduction to Brute Force Attack, Mechanize in Python for web browsing and explains a sample python script to brute force a website login.

Brute Force Attack

Brute force is the easiest way one can implement to recover lost passwords (Yet it can take literally ages to crack one). Basically,  this involves checking all possible combinations of passwords until the right one is found. This can be really helpful when characters in the password is known but not the correct combination, here possible combinations decrease drastically. Following paragraph gives a vague idea of how much time it can take to find right combination in the worst case scenario.

Suppose the length of password is N and we know nothing about characters present in the string, possible characters can be all alphabets (upper and lowercase), numbers (0-9) and special characters (~, @, #, $, ^), thus each character of the password string can be any of the above 67 characters which leads to a total of 67^N combinations (as you can clearly see it increases exponentially with the length). If we are brute forcing a website login, time taken significantly depends on the internet speed, for instance it can do four login checks per second, it takes nearly 58 hours to crack a password of four character length. Suppose if we know the characters, we can find the correct combination in 64 seconds, far less than previous case.


In the following brute-force script we use Mechanize, a python library for stateful programmatic web browsing, used for automating interaction with websites (Initially it was written for PERL users). There are many ways to install this library. Following two ways will automatically download the latest version source and install it (for linux users).

Easy Install:

easy_install mechanize


pip install mechanize

For installing it manually you can go through their documentation at Mechanize. Here are few things you have to know about mechanize in order to understand the sample script.

1. Initializing browser object: 

import mechanize
br = mechanize.Browser()

2. Opening the login page:

response =

3. Selecting the required form in the page:

br.select_form("form name") #selecting forms by name
br.select_form(nr=0)        #use to select the first form in the page if it is unnamed

Continue reading

Explicit Incompressible Euler Solver – Written in Matlab

Pressure Distribution on a Bump Geometry

Pressure Distribution on a Bump

The following report presents the solver written in Matlab (Code in the Appendix) for incompressible Euler equations and results obtained on solving them for a bump geometry with a given free stream conditions. Results include contour, mesh plots of flow variables, velocity vector plots, convergence histories etc.

Explicit Incompressible Euler Solver – Written In Matlab

How to create your own subtitles?

Have you ever wondered how people write subtitles/captions to movies? The other day I was watching a pretty new movie(obviously pirated print) and unfortunately I couldn’t find subtitles anywhere on internet, so I thought, how about I write one and upload in internet. After few minutes of browsing I realized that they are pretty easy to make, all you need is patience to listen every word carefully and of course you should be quick enough to type what you listen. I have neither of the above qualities and got exhausted just after 20 minutes through the movie :-), so I had to quit. Here I am, writing a step-by-step guide to create subtitles for any videos or audios.

If you are using windows, you will need a text editor( I used notepad) and a software called DivXLand Media Subtitler which you can download here.

Step 1: Writing what is said 

Movie and text editior

Simultaneously listening and writing

Open video or audio for which you want to create subtitles in your favourite player( you will have to rewind, pause multiple times). Simultaneously open a new text document in notepad and start writing what is said line-by-line as shown in the image (Only one line appears on the screen at any moment). If you want to split a caption into two lines, inset a ‘|’ character(without quotes) in the position where the line should be split. When you’re done writing all spoken words, save this as a plain text document (Other formats might not be ideal for the subtitler software).

2. Synchronizing the text file

Here we will synchronize our text file with the audio. Go to ‘file -> open text or subtitle..’, open the above text file. Open you video by ‘file -> open video file..’ . Hit the play button and let the video play, and when the selected text is about to spoken, in default mode press and hold the Apply button until the line is spoken completely. The next line will be automatically selected for sync. Various other modes are available, you can read about them here.

Continue reading

Selection Problem and its solution in ‘C’

We define selection problem as follows:

Input    : An array of N distinct numbers and an integer i, with 1 ≤ i ≤ N. 

Output : The element that is larger than exactly i – 1 other elements of the array.

You will obviously think of sorting the array and then simply finding the ith element in it, which can be done with O(N log N) time complexity using merge sort or quicksort or heapsort. But, can we do better than this? Yes, following randomized algorithm achieves this in O(N) expected running time practically.

This algorithm SELECT is modeled after the quicksort algorithm. As in quciksort, we partition the input array recursively. But unlike quicksort, which processes both sub-arrays, SELECT works on only one of these sub-arrays. This difference shows up in the analysis: whereas quicksort has an expected running time of O(N log N), the expected running time of SELECT is O(N). Explanation for the algorithm is clearly given in the code.          

Solution in C

Question: Select the ith minimum element in an array with O(N) expected running time.


First line of the input contains T, number of test cases

Each test case contains an integer N ≤ 200, the number of integers in that array, followed by N distinct integers separated by N spaces with i in the next line.

Output: ith minimum element.

Sample Input

5 4 7 9 3 1
4 6 10 5 7

Sample Output



Explanation is given in the gist.

Quicksort and its Implementation in ‘C’

Like Merge Sort, Quicksort is a divide and conquer algorithm. Quicksort first divides the array into two smaller sub-arrays around a picked element called Pivot. Then recursively sort the sub-arrays. It works as follows:

  1. Pick an element, called a pivot, from the array.
  2. Reorder the array such that all the elements before pivot are less than pivot value, while all elements after pivot are grater than or equal to pivot. After this operation, the pivot is in its final position. This is the key process in quicksort, called the partition().
  3. Recursively apply this to the sub-arrays before and after the pivot. Look at this example.

There are different variants of Quicksort that pick pivots in different ways:

  1. First element of array.
  2. Last element of array.(used in the following implementation)
  3. Random element of array.
  4. Middle index element of array.


Time complexity : O( N log N --  N log N -- N² ) [Best -- Average -- Worst] 
Memory          :  O(N or log N) # auxiliary i.e. apart from storing input array 
Stable          :  No i.e. can change relative order of elements with same key
# N being the number of elements in the array

Other popular sorting algorithms’ complexities are given here.

Usage and advantages

Continue reading

Merge Sort and its Implementation in ‘C’

Merge sort is an advanced sorting algorithm, derived from divide and conquer algorithmic paradigm. It works as follows:

1. Divide unsorted array into N sub-arrays, each containing one element. (which is sorted already)

2. Repeatedly merge these sub-arrays to produce sorted new ones until only 1 sub-array is left-out, this is the required sorted array.

Look at the following animation for clear understanding.

Merge sort animation.

Merge sort animation. Courtesy:


Time complexity : O( N log N --  N log N -- N log N ) [Best -- Average -- Worst] 
Memory          :  O(N) # auxiliary i.e. apart from storing input array 
Stable          :  Yes i.e. doesn't change relative order of elements with same key
# N being the number of elements in the array

You can read complexities of other popular sorting algorithms here.

Usage and Applications

As of Perl 5.8, merge sort is its default sorting algorithm (it was quicksort in previous versions of Perl). In Java, the Arrays.sort() methods use merge sort or a tuned quicksort depending on the datatypes and for implementation efficiency switch to insertion sort when fewer than seven array elements are being sorted. Merge sort is often the best choice for sorting linked lists. Used to find inversion count of an array.

Bottom Line: Typically, default sort implementation for most of the languages is either Mergesort or Quicksort.

Implementation in C:

Continue reading