Using python mechanics to enter web page with javascript md5 hash function

I am trying to use python / mechanize to access this web page: http://www.solaradata.com/cgi-bin/mainProgram.cgi

The Javascript function is used in the login form, which generates an MD5 hash from several field values ​​before sending the results for authentication. Since mechanize cannot execute javascript, I tried to reproduce the same functionality inside python and then present the resulting values. However, I still get the "wrong user / password" errors.

Here is my current code, can someone tell me where I went wrong? Thanks!

url_login = 'http://www.solaradata.com/cgi-bin/mainProgram.cgi' import mechanize import md5 username = 'superfly' #not my real user/pass password = 'stickyguy' #not my real user/pass br = mechanize.Browser() br.open(url_login) br.select_form(nr=0) br.set_all_readonly(False) session = br['session'] br['user'] = username br['password'] = password m1 = md5.new() m1.update(password + username) br['password'] = m1.digest() m2 = md5.new() m2.update(password + session) br['hash'] = m2.digest() for form in br.forms(): #print form request2 = form.click() # mechanize.Request object try: response2 = mechanize.urlopen(request2) except mechanize.HTTPError, response2: pass print response2.geturl() # headers for name, value in response2.info().items(): if name != "date": print "%s: %s" % (name.title(), value) print response2.read() # body response2.close() 
+4
source share
3 answers

Use m1.hexdigest () instead of m1.digest ()

+1
source

I am not familiar with python, but it looks like they are returning the hexadecimal value of the MD5 hash in the javascript version of the algorithm. Does python MD5 support the same?

You should be able to verify this without going through the process of submitting and testing for success. Instead, using a JavaScript developer tool such as Firebug or Chrome developer tools, calculate the result you get on the page. Then, using the same inputs, see what you get from your program. They must match the character for the character.

0
source

This may be redundant, but if you really need a script to access a heavy javascript site, you can look at the selenium-rc lab or source lab.

These tools allow you to use the actual browser script as well as the user.

Selenium

0
source

All Articles