"m-jay, post: 68743, member: 20288":
Dont want to use 3rd party code so Was thinking of just adding a question and answer through config if post answer on login does not match config answer it's a fail
This would keep most bots and spiders out
Since 2017, NIST recommends using a secret input when hashing memorized secrets such as passwords. By mixing in a secret input (commonly called a "pepper"), one prevents an attacker from brute-forcing the password hashes altogether, even if they have the hash and salt. For example, an SQL injection typically affects only the database, not files on disk, so a pepper stored in a config file would still be out of reach for the attacker. A pepper must be randomly generated once and can be the same for all users. Many password leaks could have been made completely useless if site owners had done this.
Since there is no pepper parameter for password_hash (even though Argon2 has a "secret" parameter, PHP does not allow to set it), the correct way to mix in a pepper is to use hash_hmac(). The "add note" rules of php.net say I can't link external sites, so I can't back any of this up with a link to NIST, Wikipedia, posts from the security stackexchange site that explain the reasoning, or anything... You'll have to verify this manually. The code:
// config.conf
pepper=c1isvFdxMDdmjOlvxpecFw
<?php
// register.php
$pepper = getConfigVariable("pepper");
$pwd = $_POST['password'];
$pwd_peppered = hash_hmac("sha256", $pwd, $pepper);
$pwd_hashed = password_hash($pwd_peppered, PASSWORD_ARGON2ID);
add_user_to_database($username, $pwd_hashed);
?>
<?php
// login.php
$pepper = getConfigVariable("pepper");
$pwd = $_POST['password'];
$pwd_peppered = hash_hmac("sha256", $pwd, $pepper);
$pwd_hashed = get_pwd_from_db($username);
if (password_verify($pwd_peppered, $pwd_hashed)) {
echo "Password matches.";
}
else {
echo "Password incorrect.";
}
?>
Note that this code contains a timing attack that leaks whether the username exists. But my note was over the length limit so I had to cut this paragraph out.
Also note that the pepper is useless if leaked or if it can be cracked. Consider how it might be exposed, for example different methods of passing it to a docker container. Against cracking, use a long randomly generated value (like in the example above), and change the pepper when you do a new install with a clean user database. Changing the pepper for an existing database is the same as changing other hashing parameters: you can either wrap the old value in a new one and layer the hashing (more complex), you compute the new password hash whenever someone logs in (leaving old users at risk, so this might be okay depending on what the reason is that you're upgrading).
Why does this work? Because an attacker does the following after stealing the database:
password_verify("a", $stolen_hash)
password_verify("b", $stolen_hash)
...
password_verify("z", $stolen_hash)
password_verify("aa", $stolen_hash)
etc.
(More realistically, they use a cracking dictionary, but in principle, the way to crack a password hash is by guessing. That's why we use special algorithms: they are slower, so each verify() operation will be slower, so they can try much fewer passwords per hour of cracking.)
Now what if you used that pepper? Now they need to do this:
password_verify(hmac_sha256("a", $secret), $stolen_hash)
Without that $secret (the pepper), they can't do this computation. They would have to do:
password_verify(hmac_sha256("a", "a"), $stolen_hash)
password_verify(hmac_sha256("a", "b"), $stolen_hash)
...
etc., until they found the correct pepper.
If your pepper contains 128 bits of entropy, and so long as hmac-sha256 remains secure (even MD5 is technically secure for use in hmac: only its collision resistance is broken, but of course nobody would use MD5 because more and more flaws are found), this would take more energy than the sun outputs. In other words, it's currently impossible to crack a pepper that strong, even given a known password and salt.
$error = '';
// If/else checkings
if (!is_array($_FILES['torrent']))
{
$error = 'The file contains no array';
}
elseif (empty($_FILES['torrent']))
{
$error = 'You did not select a torrent file';
}
//Proceed with uploading if no errors are met.
if ($error === '')
{
$insert = $db->query("INSERT INTO torrents (filename, owner, name, descr, image1, image2, category, added, info_hash, size, numfiles, save_as, announce, external, nfo, torrentlang, anon, last_action)
VALUES ('" . $torr_saveas . "', " . sqlesc($userid) . ", '" . $torr_name . "', 'Torrent POST description', 'image1', 'image2', 1, '" . get_date_time() . "', '" . $torr_hash . "', '" . $torr_size . "', 3, '" . $torr_name . "', '" . $torr_announce . "', '" . $torr_private . "', 'no', 1, 'no', '" . get_date_time() . "')");
die;
}
else
{
die($error);
}
<?php
include 'backend/functions.php';
dbconn();
// This is the PHP side ajax check
if (isset($_GET['ajax']) && $_GET['ajax'] === '1')
{
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$searchstr = strtolower(trim($_POST["query"]));
$search = "MATCH (torrents.name) AGAINST ('".mysqli_real_escape_string($GLOBALS["DBconnector"],$searchstr)."' IN BOOLEAN MODE)";
if (!empty($_POST["query"]) && strlen($_POST["query"]) >= 4)
{
// You can JOIN tables here to get more information about torrents. (But will affect performance)
$stmt = SQL_Query_exec("SELECT * FROM torrents WHERE $search LIMIT 10");
$row = mysqli_fetch_assoc($stmt);
}
}
echo '<div class="ls_box">
<div style="text-align: center;border-bottom: 1px solid #eee;padding-bottom: 5px;">Found results for <span style="color: #e6750c; ">' . $_POST['query'] . '</span></div>';
if ($row)
{
$response = '';
foreach ($stmt as $i)
{
$response .= '<div class="results"><a href="/torrents-details.php?id=' . $i['id'] . '">' . $i['name'] . '</a></div>';
}
}
else
{
$response = '<div class="results">No results found</div>';
}
echo $response;
echo '</div>';
die;
}
stdhead('ajax search');
begin_frame('ajax search');
?>
Search torrents: <input type="text" onkeyup="ajaxSearch(this.value)" name="query" />
<div id="ajaxSearchStatus"></div>
<style>
.ls_box {
position: absolute;
background: #fff;
width: 20%;
border-bottom: 0;
border-radius: 10px;
padding: 10px;
box-shadow: 0 1px 4px rgba(0,0,0,.1);
font-weight: bold;
}
.results {
border-bottom: 1px solid #eee;
padding: 5px;
word-wrap: break-word;
}
.results a {
color: #111;
text-decoration: none;
}
.results a:hover {
color: #333;
}
.results:hover {
background: #e6750c3b;
border-right: 2px solid #e6750c;
}
</style>
<script type="text/javascript">
function ajaxSearch(str)
{
var xhttp;
if (str.length < 4) {
document.getElementById("ajaxSearchStatus").innerHTML= '';
return false;
}
// Modern browser support
if (window.XMLHttpRequest)
{
xhttp = new XMLHttpRequest();
}
// Old browser support (IE 6+)
else
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = '?ajax=1';
var params = 'query=' + str;
xhttp.open("POST", url, true);
// Send the proper header information along with the request
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
// What to do with the response
xhttp.onreadystatechange = function()
{
if (this.readyState === 4 && this.status === 200)
{
document.getElementById("ajaxSearchStatus").innerHTML = this.responseText;
}
};
// Send the post fields
xhttp.send(params);
// Until data returned, show custom text
document.getElementById("ajaxSearchStatus").innerHTML = '<div class="ls_box">Loading data...</div>';
}
</script>
<?php
end_frame();
stdfoot();
<?php
include 'backend/functions.php';
dbconn();
// This is the PHP side ajax check
if (isset($_GET['ajax']) && $_GET['ajax'] === '1')
{
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$username = sqlesc($_POST['username']);
$stmt = SQL_Query_exec("SELECT COUNT(id) as username FROM users WHERE username = $username");
$row = mysqli_fetch_assoc($stmt);
if ($row['username'] < 1)
{
$response = '<font color="green">This username is available</font>';
}
else
{
$response = '<font color="red">This username is not available</font>';
}
echo $response;
die;
}
}
stdhead('username check');
begin_frame('username check');
?>
<form method="post">
Username: <input type="text" onkeyup="usernameCheck(this.value)" name="username" /> <span id="usernameStatus"></span>
</form>
<script>
function usernameCheck(str)
{
// This is just a browser side protection (Check usernames only if username has more than 3 letters)
if (str.length < 3) {
return false;
}
// Define AJAX XMLHTTP
// Modern browsers
if (window.XMLHttpRequest)
{
xhttp = new XMLHttpRequest();
}
// I.E 6
else
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
var url = '?ajax=1';
var params = 'username=' + str;
xhttp.open("POST", url, true);
// Send the proper header information along with the request
xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
// What to do with the response
xhttp.onreadystatechange = function()
{
if (this.readyState === 4 && this.status === 200)
{
document.getElementById("usernameStatus").innerHTML = this.responseText;
}
};
// Send the post fields
xhttp.send(params);
// Until data returned, show custom text
document.getElementById("usernameStatus").innerHTML = "Processing...";
}
</script>
<?php
end_frame();
stdfoot(); CREATE TABLE sessions (
sid varchar(32) NOT NULL default '',
uid int unsigned NOT NULL default '0',
ip varbinary(16) NOT NULL default '',
time int unsigned NOT NULL default '0',
useragent varchar(200) NOT NULL default '',
anonymous tinyint(1) NOT NULL default '0',
nopermission tinyint(1) NOT NULL default '0',
PRIMARY KEY(sid),
KEY time (time),
KEY uid (uid),
KEY ip (ip)
) ENGINE=InnoDBif (password_verify($_POST['password'], $row["password"))
{
/* Add login cookie here
* User has the new hashing method which is working.
*/
logincookie();
}
else
{
if ($row["password"] === $_POST['password'])
{
// We are updating the old md5 password or whatever it is.
$newPassword = password_hash($_POST['password'], PASSWORD_BCRYPT);
$stmt = DB::run("UPDATE users SET password = ? WHERE id= ?", [$newPassword, $id]);
logincookie($row["id"], $newPassword, $row["secret"]);
}
else
{
// Password is not correct in either md5 or bcrypt format
logoutcookie();
}
} "m-jay, post: 68431, member: 20288":
That can be done I'm redoing account controller now with sessions
I have login, logout, recover etc all in one controller just pulling models and views out