这种方法是否足以保护我的主机(php)?

这种方法是否足以保护我的主机(php)?,php,security,xss,Php,Security,Xss,我想让我的主机高度安全(防止xss和CSRF攻击… 第一防御(令牌) if(time()>=$\u会话['token']['expire']{ $length=兰特(31,50); 试一试{ $\会话['token']['code']=bin2hex(随机字节($length)); $\会话['token']['input']=bin2hex(随机字节($length)); }捕获(\异常$e){ $\会话['token']['code']=substr(基本转换(sha1(uniqid(m

我想让我的主机高度安全(防止xss和CSRF攻击…

  • 第一防御(令牌)
if(time()>=$\u会话['token']['expire']{
$length=兰特(31,50);
试一试{
$\会话['token']['code']=bin2hex(随机字节($length));
$\会话['token']['input']=bin2hex(随机字节($length));
}捕获(\异常$e){
$\会话['token']['code']=substr(基本转换(sha1(uniqid(mt_rand())),16,36),0,$length);
$\会话['token']['input']=substr(基本转换(sha1(uniqid(mt_rand())),16,36),0,$length);
}
$\会话['token']['expire']=time()+3600;
死亡(JSON_超时);
}
  • 第二道防线(检查所有查询)
$value=trim(strip_标签(htmlspecialchars(stripslashes($POST['query'])));
  • 第三防御(仅允许post请求)
if($\u服务器['REQUEST\u方法]!='POST'| | sizeof($\u GET)){
http_响应_代码(405);
出口
}
  • 第四防御(用于在数据库中保存密码)
$pass=password\u散列(“password”,password\u默认值);

我还遗漏了什么吗?您遗漏了SQL注入

您可以使用准备好的语句来避免SQL注入

以下是一个例子:

$conn = mysqli_connect("localhost", "username", "password", "database");

$username = "someone";
$comments = "something like ); SELECT * FROM table;"; #some kind of sql injection
$current_date = date("h:i:s a d-m-Y");
        $sql = "INSERT INTO comments (name, comments, date_publish) VALUES (?, ?, ?);";
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
            echo "An error occured!";
        } else {
            mysqli_stmt_bind_param($stmt, "sss", $username, $comment, $current_date);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_get_result($stmt);
            echo "Done!";
        }
尽管您的标题中包含了(php),但我还是想分享一些安全标题:

如果使用Apache,请将以下内容添加到Apache:

<IfModule headers_module>
Header always set Expires "-1"
Header always set Cache-Control "no-store, no-cache, must-revalidate, max-age=0"
Header always set Pragma "no-cache"

<FilesMatch "\.(gif|jpe?g|png|webp|ico|mp4|mp3)$">
Header always unset Expires
Header always set Cache-Control "must-revalidate, max-age=3600"
Header always unset Pragma
</FilesMatch>
Header always set Content-Security-Policy "default-src 'none'; img-src data: https: 'self'; script-src 'self'; object-src 'none'; frame-ancestors 'none'; style-src 'self'; base-uri 'none'; form-action 'self'; media-src https: 'self'; frame-src 'none'; child-src 'none'; connect-src 'self'"
Header always set X-Frame-Options "DENY"
Header always set X-XSS-Protection "1; mode=block"
Header always set X-Content-Type-Options nosniff
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" "expr=%{HTTPS} == 'on'"
#Header always set Referrer-Policy "no-referrer"
Header always set Permissions-Policy "geolocation=();midi=();notifications=();push=();sync-xhr=(self);microphone=();camera=();magnetometer=();gyroscope=();speaker=(self);vibrate=();fullscreen=(self);payment=();"
Header always set X-Permitted-Cross-Domain-Policies "none"
</IfModule>
你可以根据自己的需要改变它们。
内容安全策略
头是最重要的头。它可能会破坏你的网站,但是,它会帮助很多

用于测试站点的安全性

以下是最安全分数的Apache配置:


您是否也对SQL注入进行了保护?无论如何,这太宽泛了,我们不知道您的应用程序还做了什么,因此也不知道它还可能有什么漏洞。咨询OWASP列表中的顶级Web应用程序威胁,并考虑是否有漏洞。如果您不确定,请聘请经验丰富的安全顾问来检查您的代码和服务器环境。查看我的答案。如果这么简单,就不会有漏洞。
strip_标签(htmlspecialchars(…
您确实意识到您首先要转换为ents
b
,然后尝试剥离标签。默认密码毫无意义。这就像在屋外举着一个巨大的标牌“花盆下的备用钥匙”.我不擅长Apache,你的意思是将此代码放入.htaccess?响应头:
HTTP/1.1200确定日期:2021年2月2日星期二20:43:26 GMT服务器:Apache/2.4.46(Win64)PHP/7.4.9 X-Powered-By:PHP/7.4.9访问控制允许来源:*访问控制允许方法:过期:1981年11月19日星期四08:52:00 GMT缓存控制:无存储,无缓存,必须重新验证Pragma:no-Cache内容长度:1199保持活动:超时=5,最大=100连接:保持活动内容类型:application/json
我认为不工作。对吗?about SQL injections->黑客如何查找表和列的名称?是否可以在PDO中运行两个查询?使用addcslashes是否足够?@H.jalali,如果他们执行
SHOW tables
@H.jalali,他们可以找到您的表。将该代码放入htaccess中。。
set-cookie: __Secure-YOURSESSID=abcdefghijklmnopqrstuvwxyz123456789; path=/; secure; HttpOnly; SameSite=Lax
expires: -1
cache-control: no-store, no-cache, must-revalidate, max-age=0
pragma: no-cache
content-security-policy: default-src 'none'; img-src data: https: 'self'; script-src 'self'; object-src 'none'; frame-ancestors 'none'; style-src 'self'; base-uri 'none'; form-action 'self'; media-src https: 'self'; frame-src 'none'; child-src 'none'; connect-src 'self'
x-frame-options: DENY
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
strict-transport-security: max-age=63072000; includeSubDomains; preload
permissions-policy: geolocation=();midi=();notifications=();push=();sync-xhr=(self);microphone=();camera=();magnetometer=();gyroscope=();speaker=(self);vibrate=();fullscreen=(self);payment=();
x-permitted-cross-domain-policies: none
content-type: text/html; charset=UTF-8
SSLCipherSuite TLSv1.3 TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
SSLCipherSuite ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-SHA384
SSLOpenSSLConfCmd ECDHParameters secp384r1

#generate DH param using: openssl dhparam -out dhparam.pem 4096
SSLOpenSSLConfCmd DHParameters "/path/to/ssl/dh4096.pem"

SSLHonorCipherOrder On
SSLProtocol -all +TLSv1.2 +TLSv1.3
SSLPassPhraseDialog builtin
SSLSessionCache "shmcb:/usr/local/apache2/logs/ssl_scache(512000)"
SSLSessionCacheTimeout 300
SSLUseStapling On
SSLStaplingCache "shmcb:ssl_stapling(32768)"