Skip to content

Instantly share code, notes, and snippets.

@dhaupin
Last active March 11, 2016 01:10
Show Gist options
  • Save dhaupin/c1772d27d4a623e389a9 to your computer and use it in GitHub Desktop.
Save dhaupin/c1772d27d4a623e389a9 to your computer and use it in GitHub Desktop.
Function - Parse list of 10,000 most used passwords and return JSON or Object match for use on account validation
<?php
// This is part of a form class, this function shalt also be available via route with optional ?search= param
// password list: https://github.com/danielmiessler/SecLists/blob/master/Passwords/10_million_password_list_top_10000.txt
// example: www.example.com/chkpw.php&search=dra would return "dragon" (always returns the first pass found)
public function chkpass($password = false, $json = true) {
$pool = $output = array();
$pool = file('https://src.creadev.org/apps/pwlist/pwlist.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
if (isset($_GET['search'])) {
$search = $_GET['search'];
} elseif (isset($password)) {
$search = $password;
} else {
return false;
}
$search = str_replace('%20', '~~~', (string)$search);
foreach ($pool as $value) {
str_replace(' ', '~~~', $value);
if ((stripos($value, $search) !== false) || (stripos($search, $value) !== false)) {
$output = $value;
break;
}
}
// done with $pool
unset($pool);
// return output as json or array
if (empty($output)) {
return false;
} elseif ($json) {
echo json_encode($output);
} else {
return $output;
}
}
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"></script>
</head>
<body>
<input type="text" name="password_test" value="" />
<script type ="text/javascript">
var target = $('[name="password_test"]');
function chkpw(value) {
$.ajax({
type: 'GET',
url: 'https://src.creadev.org/apps/pwlist/pwlist.php?search=' + value,
dataType: 'json',
success: function(d) {
if (d) {
$(target).after(' <span class="invalid_pass">Insecure Password: ' + d + '</span>');
}
}
});
}
$(target).on('keyup', _.debounce(function(e) {
$('.invalid_pass').remove();
chkpw(target.val());
}, 300));
</script>
</body>
</html>
@dhaupin
Copy link
Author

dhaupin commented Mar 10, 2016

We are using the underscore _debounce function to throttle requests - In this example, it's set to 300ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment