Compare commits
7
Commits
f94a1ebbd5
...
fix/root
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f7a2d2d61 | ||
|
|
fb80cb78eb | ||
|
|
c06e1bd64b | ||
|
|
57cc2c3fa0 | ||
|
|
4b5d0dd704 | ||
|
|
244b91677f | ||
|
|
bd9aec48d7 |
@@ -22,6 +22,12 @@ CREATE TABLE IF NOT EXISTS `dummy` (
|
|||||||
`id` int(11) DEFAULT NULL
|
`id` int(11) DEFAULT NULL
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
|
-- Create a dedicated DB user for the web application (least privilege).
|
||||||
|
-- Grant only the required privileges on the application database.
|
||||||
|
CREATE USER IF NOT EXISTS 'passman_app'@'%' IDENTIFIED BY 'passman_app_pw';
|
||||||
|
GRANT SELECT, INSERT, UPDATE, DELETE ON pwd_mgr.* TO 'passman_app'@'%';
|
||||||
|
FLUSH PRIVILEGES;
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `login_users` (
|
CREATE TABLE IF NOT EXISTS `login_users` (
|
||||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
`username` varchar(20) NOT NULL,
|
`username` varchar(20) NOT NULL,
|
||||||
@@ -31,7 +37,7 @@ CREATE TABLE IF NOT EXISTS `login_users` (
|
|||||||
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
INSERT INTO `login_users` (`id`, `username`, `password`) VALUES
|
INSERT INTO `login_users` (`id`, `username`, `password`) VALUES
|
||||||
(1, 'u1', 'p1');
|
(1, 'u1', '$2y$10$L18u5/PyVkDgsce/DsUOQu0sKhTzh854Euhog3cVb1W4YAfgRzY8W'); -- php -r 'echo password_hash("p1", PASSWORD_DEFAULT), PHP_EOL;'
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS `notes` (
|
CREATE TABLE IF NOT EXISTS `notes` (
|
||||||
`notesid` int(11) NOT NULL AUTO_INCREMENT,
|
`notesid` int(11) NOT NULL AUTO_INCREMENT,
|
||||||
|
|||||||
@@ -8,9 +8,9 @@ services:
|
|||||||
- ./php:/var/www/html
|
- ./php:/var/www/html
|
||||||
environment:
|
environment:
|
||||||
DB_HOST: db
|
DB_HOST: db
|
||||||
DB_USER: root
|
|
||||||
DB_PASS: rootpass
|
|
||||||
DB_NAME: pwd_mgr
|
DB_NAME: pwd_mgr
|
||||||
|
DB_USER: passman_app
|
||||||
|
DB_PASS: passman_app_pw
|
||||||
depends_on:
|
depends_on:
|
||||||
- db
|
- db
|
||||||
|
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
// NOTE: In Docker, the DB host is the service name (e.g., "db"), not "localhost".
|
// NOTE: In Docker, the DB host is the service name (e.g., "db"), not "localhost".
|
||||||
|
|
||||||
$DB_HOST = getenv('DB_HOST') ?: 'db';
|
$DB_HOST = getenv('DB_HOST') ?: 'db';
|
||||||
$DB_USER = getenv('DB_USER') ?: 'root';
|
$DB_USER = getenv('DB_USER') ?: 'passman_app';
|
||||||
$DB_PASS = getenv('DB_PASS') ?: 'rootpass';
|
$DB_PASS = getenv('DB_PASS') ?: 'passman_app_pw';
|
||||||
$DB_NAME = getenv('DB_NAME') ?: 'pwd_mgr';
|
$DB_NAME = getenv('DB_NAME') ?: 'pwd_mgr';
|
||||||
|
|
||||||
// Create a DB connection.
|
// Create a DB connection.
|
||||||
|
|||||||
@@ -26,12 +26,23 @@ if(isset($_POST['new_website'], $_POST['new_username'], $_POST['new_password'])
|
|||||||
$new_username = trim($_POST["new_username"]);
|
$new_username = trim($_POST["new_username"]);
|
||||||
$new_password = trim($_POST["new_password"]);
|
$new_password = trim($_POST["new_password"]);
|
||||||
|
|
||||||
// Insert new web site
|
// Insert new web site using a prepared statement to prevent SQL injection.
|
||||||
$sql_query = "INSERT INTO websites (login_user_id,web_url,web_username,web_password) VALUES " .
|
$sql_query = "INSERT INTO websites (login_user_id, web_url, web_username, web_password) VALUES " .
|
||||||
"((SELECT id FROM login_users WHERE username='{$username}'),'{$new_website}','{$new_username}','{$new_password}');";
|
"((SELECT id FROM login_users WHERE username = ?), ?, ?, ?)";
|
||||||
|
|
||||||
|
$stmt = $conn->prepare($sql_query);
|
||||||
|
if ($stmt === false) {
|
||||||
|
$conn->close();
|
||||||
|
die("Prepare failed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$stmt->bind_param("ssss", $username, $new_website, $new_username, $new_password);
|
||||||
//echo $sql_query;
|
//echo $sql_query;
|
||||||
$result = $conn->query($sql_query);
|
|
||||||
$conn -> close();
|
$result = $stmt->execute();
|
||||||
|
$stmt->close();
|
||||||
|
$conn->close();
|
||||||
|
|
||||||
|
|
||||||
// After processing, redirect to the same page to clear the form
|
// After processing, redirect to the same page to clear the form
|
||||||
unset($_POST['new_website']);
|
unset($_POST['new_website']);
|
||||||
@@ -45,11 +56,25 @@ if(isset($_POST['new_website'], $_POST['new_username'], $_POST['new_password'])
|
|||||||
if(isset($_POST['delete_website']) && trim($_POST["websiteid"] != '')) {
|
if(isset($_POST['delete_website']) && trim($_POST["websiteid"] != '')) {
|
||||||
$webid = trim($_POST["websiteid"]);
|
$webid = trim($_POST["websiteid"]);
|
||||||
|
|
||||||
|
// Cast to int to avoid unexpected input and use a prepared statement to prevent SQL injection.
|
||||||
|
$webid = (int)trim($_POST["websiteid"]);
|
||||||
|
|
||||||
// Delete selected web site
|
// Delete selected web site
|
||||||
$sql_query = "DELETE FROM websites WHERE webid='{$webid}';";
|
$sql_query = "DELETE FROM websites WHERE webid = ?";
|
||||||
|
|
||||||
|
$stmt = $conn->prepare($sql_query);
|
||||||
|
if ($stmt === false) {
|
||||||
|
$conn->close();
|
||||||
|
die("Prepare failed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$stmt->bind_param("i", $webid);
|
||||||
//echo $sql_query;
|
//echo $sql_query;
|
||||||
$result = $conn->query($sql_query);
|
|
||||||
$conn -> close();
|
$result = $stmt->execute();
|
||||||
|
$stmt->close();
|
||||||
|
$conn->close();
|
||||||
|
|
||||||
|
|
||||||
// After processing, redirect to the same page to clear the form
|
// After processing, redirect to the same page to clear the form
|
||||||
unset($_POST['websiteid']);
|
unset($_POST['websiteid']);
|
||||||
@@ -57,22 +82,40 @@ if(isset($_POST['delete_website']) && trim($_POST["websiteid"] != '')) {
|
|||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display list of user's web sites
|
// Display list of user's web sites using a prepared statement to prevent SQL injection.
|
||||||
$sql_query = "SELECT * FROM websites INNER JOIN login_users ON websites.login_user_id=login_users.id WHERE login_users.username='{$username}';";
|
$sql_query = "SELECT * FROM websites INNER JOIN login_users ON websites.login_user_id=login_users.id WHERE login_users.username = ?";
|
||||||
//echo $sql_query;
|
//echo $sql_query;
|
||||||
$result = $conn->query($sql_query);
|
|
||||||
|
$stmt = $conn->prepare($sql_query);
|
||||||
|
if ($stmt === false) {
|
||||||
|
$conn->close();
|
||||||
|
die("Prepare failed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$stmt->bind_param("s", $username);
|
||||||
|
$stmt->execute();
|
||||||
|
$result = $stmt->get_result();
|
||||||
|
$stmt->close();
|
||||||
|
|
||||||
|
|
||||||
//echo htmlspecialchars($username);
|
//echo htmlspecialchars($username);
|
||||||
echo "<h3>Entries of " . $username . "</h3>";
|
$safe_username = htmlspecialchars($username, ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
|
||||||
|
echo "<h3>Entries of " . $safe_username . "</h3>";
|
||||||
|
|
||||||
if (!empty($result) && $result->num_rows >= 1) {
|
if (!empty($result) && $result->num_rows >= 1) {
|
||||||
while ($row = $result -> fetch_assoc()) {
|
while ($row = $result -> fetch_assoc()) {
|
||||||
|
// Escape output to prevent stored XSS (DB content must be treated as untrusted).
|
||||||
|
$safe_url = htmlspecialchars($row["web_url"], ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
|
||||||
|
$safe_user = htmlspecialchars($row["web_username"], ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
|
||||||
|
$safe_pass = htmlspecialchars($row["web_password"], ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
|
||||||
|
$webid_safe = (int)$row["webid"];
|
||||||
|
|
||||||
echo "<table border=0>";
|
echo "<table border=0>";
|
||||||
echo "<tr style='background-color: #f4f4f4;'><td colspan=2>" . $row["web_url"] . "</td></tr>" .
|
echo "<tr style='background-color: #f4f4f4;'><td colspan=2>" . $safe_url . "</td></tr>" .
|
||||||
"<tr><td>Username: " . $row["web_username"] . "</td><td>Password: " . $row["web_password"] . "</td></tr>";
|
"<tr><td>Username: " . $safe_user . "</td><td>Password: " . $safe_pass . "</td></tr>";
|
||||||
|
|
||||||
echo "<tr><td><form method='POST' style='height: 3px'>" .
|
echo "<tr><td><form method='POST' style='height: 3px'>" .
|
||||||
"<input type='hidden' name='websiteid' value='" . $row["webid"] . "'>" .
|
"<input type='hidden' name='websiteid' value='" . $webid_safe . "'>" .
|
||||||
"<button type='submit' name='delete_website'>Delete</button></form></td></tr>";
|
"<button type='submit' name='delete_website'>Delete</button></form></td></tr>";
|
||||||
|
|
||||||
echo "<tr><td colspan=2 style=height: 20px;></td></tr>";
|
echo "<tr><td colspan=2 style=height: 20px;></td></tr>";
|
||||||
|
|||||||
@@ -26,46 +26,51 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
// }
|
// }
|
||||||
require_once __DIR__ . "/config.php";
|
require_once __DIR__ . "/config.php";
|
||||||
|
|
||||||
// SQL injection mitigation: use a prepared statement with bound parameters.
|
// Authentication with hashed passwords:
|
||||||
// User input is treated strictly as data, not as part of the SQL syntax.
|
// 1) Fetch the stored hash by username
|
||||||
$stmt = $conn->prepare("SELECT id FROM login_users WHERE username = ? AND password = ?");
|
// SQL injection mitigation: use a prepared statement with bound parameters.
|
||||||
|
// User input is treated strictly as data, not as part of the SQL syntax.
|
||||||
|
// 2) Verify the submitted password with password_verify()
|
||||||
|
$stmt = $conn->prepare("SELECT id, password FROM login_users WHERE username = ?");
|
||||||
if ($stmt === false) {
|
if ($stmt === false) {
|
||||||
// Fail closed (do not leak details in production).
|
// Fail closed (do not leak details in production).
|
||||||
die("Prepare failed.");
|
die("Prepare failed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt->bind_param("ss", $username, $password);
|
$stmt->bind_param("s", $username);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$stmt->store_result(); // Needed to use $stmt->num_rows
|
|
||||||
|
$result = $stmt->get_result(); // Requires mysqlnd (usually enabled)
|
||||||
unset($_POST['username']);
|
unset($_POST['username']);
|
||||||
unset($_POST['password']);
|
unset($_POST['password']);
|
||||||
|
|
||||||
if ($stmt->num_rows >= 1) {
|
if ($result && $result->num_rows === 1) {
|
||||||
// Regenerate session ID to prevent session fixation!
|
$row = $result->fetch_assoc();
|
||||||
//session_regenerate_id(true);
|
$stored_hash = $row["password"];
|
||||||
|
|
||||||
// Successfully logged in
|
// Verify password against the stored hash.
|
||||||
$_SESSION['username'] = $username;
|
if (password_verify($password, $stored_hash)) {
|
||||||
$_SESSION['loggedin'] = true;
|
// Regenerate session ID to prevent session fixation!
|
||||||
|
//session_regenerate_id(true);
|
||||||
|
|
||||||
//while ($row = $result -> fetch_assoc()) {
|
// Successfully logged in
|
||||||
// print_r($row);
|
$_SESSION['username'] = $username;
|
||||||
// $_SESSION['user_id'] = $row['id'];
|
$_SESSION['loggedin'] = true;
|
||||||
//}
|
|
||||||
|
|
||||||
// Close
|
$stmt->close();
|
||||||
$stmt->close();
|
$conn->close();
|
||||||
$conn -> close();
|
|
||||||
|
|
||||||
// Redirect to a dashboard page
|
header("Location: dashboard.php");
|
||||||
header("Location: dashboard.php");
|
exit;
|
||||||
exit;
|
} else {
|
||||||
|
$login_message = "Invalid username or password";
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$login_message = "Invalid username or password";
|
$login_message = "Invalid username or password";
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt->close();
|
$stmt->close();
|
||||||
$conn -> close();
|
$conn->close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
@@ -50,13 +50,24 @@ if(isset($_POST['new_note']) && trim($_POST['new_note']) !='') {
|
|||||||
//$sql_query = "INSERT INTO notes (login_user_id,note) VALUES " .
|
//$sql_query = "INSERT INTO notes (login_user_id,note) VALUES " .
|
||||||
// "((SELECT id FROM login_users WHERE username='{$username}'),('{$new_note}'));";
|
// "((SELECT id FROM login_users WHERE username='{$username}'),('{$new_note}'));";
|
||||||
|
|
||||||
$sql_query = "INSERT INTO notes (login_user_id, note) ".
|
// Insert new note using a prepared statement to prevent SQL injection.
|
||||||
"VALUES ((SELECT id FROM login_users WHERE username='{$username}'), '{$new_note}')";
|
$sql_query = "INSERT INTO notes (login_user_id, note) ".
|
||||||
|
"VALUES ((SELECT id FROM login_users WHERE username = ?), ?)";
|
||||||
|
|
||||||
//echo $sql_query;
|
$stmt = $conn->prepare($sql_query);
|
||||||
|
if ($stmt === false) {
|
||||||
|
// Fail closed (do not leak DB details).
|
||||||
|
$conn->close();
|
||||||
|
die("Prepare failed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
$stmt->bind_param("ss", $username, $new_note);
|
||||||
|
//echo $sql_query;
|
||||||
|
|
||||||
|
$result = $stmt->execute();
|
||||||
|
$stmt->close();
|
||||||
|
$conn->close();
|
||||||
|
|
||||||
$result = $conn->query($sql_query);
|
|
||||||
$conn -> close();
|
|
||||||
|
|
||||||
// After processing, redirect to the same page to clear the form
|
// After processing, redirect to the same page to clear the form
|
||||||
unset($_POST['new_note']);
|
unset($_POST['new_note']);
|
||||||
@@ -72,12 +83,17 @@ $result = $conn->query($sql_query);
|
|||||||
echo "<h3>List of notes/comments</h3>";
|
echo "<h3>List of notes/comments</h3>";
|
||||||
|
|
||||||
if (!empty($result) && $result->num_rows >= 1) {
|
if (!empty($result) && $result->num_rows >= 1) {
|
||||||
while ($row = $result -> fetch_assoc()) {
|
while ($row = $result -> fetch_assoc()) {
|
||||||
echo "<div class='note'>";
|
// Escape output to prevent stored XSS (DB content must be treated as untrusted).
|
||||||
echo "<div class='note-content'>" . $row["note"] . "</div>";
|
$safe_note = htmlspecialchars($row["note"], ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
|
||||||
echo "<div class='note-signature'> by " . $row["username"] . "</div>";
|
$safe_user = htmlspecialchars($row["username"], ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8");
|
||||||
echo "</div>";
|
|
||||||
}
|
echo "<div class='note'>";
|
||||||
|
echo "<div class='note-content'>" . $safe_note . "</div>";
|
||||||
|
echo "<div class='note-signature'> by " . $safe_user . "</div>";
|
||||||
|
echo "</div>";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Free result set
|
// Free result set
|
||||||
$result -> free_result();
|
$result -> free_result();
|
||||||
|
|||||||
@@ -29,11 +29,28 @@ if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
|||||||
//}
|
//}
|
||||||
require_once __DIR__ . "/config.php";
|
require_once __DIR__ . "/config.php";
|
||||||
|
|
||||||
// Insert a new user
|
// Insert a new user using a prepared statement to prevent SQL injection.
|
||||||
$sql_query = "INSERT INTO login_users (username,password) VALUES ('{$new_username}','{$new_password}');";
|
$sql_query = "INSERT INTO login_users (username, password) VALUES (?, ?)";
|
||||||
//echo $sql_query;
|
|
||||||
|
$stmt = $conn->prepare($sql_query);
|
||||||
|
if ($stmt === false) {
|
||||||
|
$login_message = "Database error (prepare failed).";
|
||||||
|
$result = false;
|
||||||
|
} else {
|
||||||
|
// Hash the password before storing it.
|
||||||
|
// Never store login passwords in plaintext.
|
||||||
|
$password_hash = password_hash($new_password, PASSWORD_DEFAULT);
|
||||||
|
if ($password_hash === false) {
|
||||||
|
$login_message = "Password hashing failed.";
|
||||||
|
$result = false;
|
||||||
|
} else {
|
||||||
|
// Store the hash (not the plaintext password).
|
||||||
|
$stmt->bind_param("ss", $new_username, $password_hash);
|
||||||
|
$result = $stmt->execute();
|
||||||
|
}
|
||||||
|
$stmt->close();
|
||||||
|
}
|
||||||
|
|
||||||
$result = $conn->query($sql_query);
|
|
||||||
|
|
||||||
unset($_POST['new_username']);
|
unset($_POST['new_username']);
|
||||||
unset($_POST['new_password']);
|
unset($_POST['new_password']);
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
PHPSESSID=2c215dd41fe1090a5da5d0f3adc6ba64
|
||||||
|
PHPSESSID=2c215dd41fe1090a5da5d0f3adc6ba64
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
PHPSESSID=knjfug3u4gavdas9o4eupe38l1; seclab_user=u1
|
|
||||||
seclab_user=u1; PHPSESSID=o1mg400lipd2mck69kpfnl6p5s
|
|
||||||
Reference in New Issue
Block a user