我怎样才能提高此 php 代码的安全性?
How could I improve the security of this php code?
我已经创建了 PHP 代码:
<?php
include("../config.php");
if(!isset($_COOKIE["sessionid"])) {
header("location:../error_pages/403.php");
} else {
$value = $_COOKIE["sessionid"];
$query = "SELECT * FROM users WHERE sessionid = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("s",$value);
$stmt->execute();
$result = $stmt->get_result();
$rowcount = $result->num_rows;
if ($rowcount != 1 ) {
header("location:../error_pages/403.php");
} else {
$file = "/path/to/file/";
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename("File_Name"));
ob_clean();
flush();
readfile($file);
}
};
?>
我想知道是否有一种方法可以操纵 sessionid cookie 以允许 SQL 注入。以及此下载是否安全。该文件未存储在可公开访问的文件夹中。
为了防止会话被窃取,我只通过 TLS 提供登录页面,还有其他方法可以窃取会话吗?
通过使用查询参数,您可以避免 SQL 注入。
您的代码可能容易受到会话劫持。以下是一些提示和资源:
- 对所有请求使用 TLS (https),这会击败数据包嗅探器。
- http://phpsecurity.org/ 有一个关于会话管理的免费示例章节。或者把整本书都搞定,很好
- What is the best way to prevent session hijacking?
- https://www.owasp.org/index.php/Session_Management_Cheat_Sheet
我已经创建了 PHP 代码:
<?php
include("../config.php");
if(!isset($_COOKIE["sessionid"])) {
header("location:../error_pages/403.php");
} else {
$value = $_COOKIE["sessionid"];
$query = "SELECT * FROM users WHERE sessionid = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param("s",$value);
$stmt->execute();
$result = $stmt->get_result();
$rowcount = $result->num_rows;
if ($rowcount != 1 ) {
header("location:../error_pages/403.php");
} else {
$file = "/path/to/file/";
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename("File_Name"));
ob_clean();
flush();
readfile($file);
}
};
?>
我想知道是否有一种方法可以操纵 sessionid cookie 以允许 SQL 注入。以及此下载是否安全。该文件未存储在可公开访问的文件夹中。
为了防止会话被窃取,我只通过 TLS 提供登录页面,还有其他方法可以窃取会话吗?
通过使用查询参数,您可以避免 SQL 注入。
您的代码可能容易受到会话劫持。以下是一些提示和资源:
- 对所有请求使用 TLS (https),这会击败数据包嗅探器。
- http://phpsecurity.org/ 有一个关于会话管理的免费示例章节。或者把整本书都搞定,很好
- What is the best way to prevent session hijacking?
- https://www.owasp.org/index.php/Session_Management_Cheat_Sheet