2

So i have a directory i am getting the hashes for all files in the directory upon writing / adding the hashes for every file to a text document i keep ending up with the same hash 10-100 times over in the document and i can not figure out why php keeps doing this.

Anyone can run this on windows to see for yourself the certutil that the script executes is built into windows so it will work on any windows machine.

<?php
$file_path = 'C:\Users\C0n\Desktop\hash-banned.txt';
foreach (glob("R:\backup\Videos\*") as $filename) {
    exec('CertUtil -hashfile "'.$filename.'" SHA1', $response);
    $str = str_replace(' ', '', $response[1]);
    $find_hashes = file($file_path,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    foreach ($find_hashes as $n1) {
        if($str == $n1) {
            echo "duplicate detected";
            break;
        }
        echo "Hash does not exist so adding " . $str;
        //hash not found so add to file
        //if hash string is not empty then write to file
        if ($str != "") {
            file_put_contents($file_path, $str . "\n", FILE_APPEND);
        }
    }
}
?>

1 Answer 1

2

How does $str even end up being written to file, if the file is empty? Because $find_hashes will also be empty and foreach will not run. Tested the code below, seems to be working fine with me.

    $str = sha1_file($filename);
    $find_hashes = file($file_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    // unsetting response returned by certutil
    // because exec() appends output lines to the end of the array
    unset($response);

    if (in_array($str, $find_hashes)) {
        echo "duplicate detected";
        continue;       
    }

    if ($str != "") file_put_contents($file_path, $str . "\n", FILE_APPEND);            
    echo "Hash does not exist so adding " . $str;  
Sign up to request clarification or add additional context in comments.

6 Comments

I left the first line of the find_hashes with a space so its technically not empty. Also the reason i use "certutil.exe" instead of php's built in sha1_file is because the files are over 2GB in size and php can not read files larger than 2GB on windows.
@C0nw0nk I understand, I went with sha1_file just for testing purposes. You can go with certutil.exe. Does it still spawns duplicate hash?
Yeah that seems to have done the trick thanks JungleZombie :) was so confussed never encountered that problem before.
unsetting the response array seems to have fixed it completely for me edited your answer to include that bit of code.
Yea, good to know that exec() is not reassigning the array, but appending output lines to the end of it. That actually might have be the problem with your first code :) You should be unsetting $response at each iteration.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.