Saturday, December 24, 2011

Cracking MD5 with Google

It has been known for a long time now that Google can be used for all kinds of awesome things (Google query hacking etc), and hash cracking is one of them.

I thought I would share something I wrote last year to demonstrate how easy it is to crack MD5, just by using Google. This is easily adapted for use with other hashes, cracking SHA1 works well too. I've found the success rate to be extremely high. Enjoy :)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package md5crack;

import java.io.*;
import java.net.*;
import java.util.logging.*;
import org.apache.commons.codec.digest.DigestUtils;

/**
 *
 * @author Adam Boulton - Using Google to crack MD5
 */
public class MD5Crack {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        if(args[0] == null || args[0].isEmpty())
        {
            System.out.println("-= Google MD5 Cracker =-");
            System.out.println("-= Adam Boulton - 2010 =- ");
            System.out.println("Usage: MD5crack <hash>");
        }
 
        String hash = args[0];
        String url = String.format("https://www.google.com/search?q=%s", hash);

        try {
            URL oracle = new URL(url);
            URLConnection conn = oracle.openConnection();

            //keep Google happy, otherwise connection refused.
            conn.setRequestProperty("user-agent", "Mozilla/5.0 Windows NT6.1 WOW64 AppleWebKit/535.7 KHTML, like Gecko Chrome/16.0.912.63 Safari/535.7");

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    conn.getInputStream()));
            
            String inputLine;

            while ((inputLine = in.readLine()) != null) {
                String[] words = inputLine.split("\\s+");

                for (String word : words) {
                    String wordHash = DigestUtils.md5Hex(word);
                    if (wordHash.equals(hash)) {

                        System.out.println("[*] Found: " + word);
                        System.exit(0);
                    }
                }
            }
            
            System.out.println("[!] No results.");
            in.close();

        } catch (IOException ex) {
            Logger.getLogger(MD5Crack.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}