2

So, I'm having a little bit of trouble in PHP with a file_get_contents...

I'm using this code.

Before, if I ran it with a hash that it could not find (bdfccf20b1db88d835c27685ac39f874), it would return this:

fcf1eed8596699624167416a1e7e122e - found: octopus (Google)
bed128365216c019988915ed3add75fb - found: passw0rd (Google)
d0763edaa9d9bd2a9516280e9044d885 - found: monkey (Google)
dfd8c10c1b9b58c8bf102225ae3be9eb - found: 12081977 (Google)
ede6b50e7b5826fe48fc1f0fe772c48f - found: 1q2w3e4r5t6y (Google)
bdfccf20b1db88d835c27685ac39f874
Warning: file_get_contents(http://md5.gromweb.com/query/bdfccf20b1db88d835c27685ac39f874): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

 in /Users/mihir/MD5Decryptor.php on line 44

Catchable fatal error: Argument 2 passed to MD5Decryptor::dictionaryAttack() must be an array, boolean given, called in /Users/mihir/MD5Decryptor.php on line 56 and defined in /Users/mihir/MD5Decryptor.php on line 25

To stop the warning, I changed

if ($response = file_get_contents($url)) {

on line 43 to

$response = @file_get_contents($url);
if ($response) {

and the output becomes

fcf1eed8596699624167416a1e7e122e - found: octopus (Google)
bed128365216c019988915ed3add75fb - found: passw0rd (Google)
d0763edaa9d9bd2a9516280e9044d885 - found: monkey (Google)
dfd8c10c1b9b58c8bf102225ae3be9eb - found: 12081977 (Google)
ede6b50e7b5826fe48fc1f0fe772c48f - found: 1q2w3e4r5t6y (Google)
bdfccf20b1db88d835c27685ac39f874
Catchable fatal error: Argument 2 passed to MD5Decryptor::dictionaryAttack() must be an array, boolean given, called in /Users/mihir/MD5Decryptor.php on line 56 and defined in /Users/mihir/MD5Decryptor.php on line 25

How can I catch the error? As in, if the hash is not found, how could I modify the script to return "Hash Not Found" and not completely crash?

Thanks in advance...

Community
  • 1
  • 1
citruspi
  • 6,709
  • 4
  • 27
  • 43

3 Answers3

4

The reason you are still getting the error is because of this line:

return $this->dictionaryAttack($hash, $this->getWordlist($hash));

When getWordList gets a 404 from file_get_contents(), FALSE is returned and that is generating the exception about the invalid argument getting passed.

One thing you could try to do to fix it is this:

$list = $this->getWordlist($hash);
if ($list === false) {
    return 'Error fetching URL';
} else {
    return $this->dictionaryAttack($hash, $list);
}

That should at least handle URLs it cant load.

drew010
  • 68,777
  • 11
  • 134
  • 162
  • 2
    alternatively, `$list = FALSE` on line 42 can be replaced by `$list = array()`, if I'm not mistaken. – Jasper Apr 20 '12 at 23:01
1

Wrap it all in a try-catch block. PHP has a mechanism for handling those fatal errors.

Something like this should work:

try {
    if ($response = file_get_contents($url)) {
        ...
    }
}
catch (Exception $e) {
    // return your "Hash Not Found" response
}

Here's some documentation on the construct: http://php.net/manual/en/language.exceptions.php

You'll probably want to determine exactly which line of code is causing the error, and use the most specific subclass of Exception that you can. This is a best practice, since you don't want to miss exceptions that are unrelated to this issue.

Thomas Kelley
  • 10,187
  • 1
  • 36
  • 43
0

The best thing you can do is switch to using cURL. While you can get the errors when using file_get_contents(), it isn't very robust.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • Hmm... I don't have much experience in cURL. How, and where, would I implement it? – citruspi Apr 20 '12 at 22:47
  • @MihirSingh, I just added a link to help you get started. http://davidwalsh.name/download-urls-content-php-curl See also: http://php.net/manual/en/book.curl.php – Brad Apr 20 '12 at 22:48
  • You can control error handling with file_get_contents as well, just use the HTTP context. Works superb, not badier than curl IMHO. – hakre Apr 20 '12 at 23:35