wahaha2026-05-26文章来源:SecHub网络安全社区
tar -zcvf 1.tar.gz * 将文件夹下的内容压缩到1.tar.gz
tar zcvf update.tar.gz
mv <修复的php文件> /var/www/html/ //一般都是这个目录视情况而定
cp -r <修复的php文件> /var/www/html/
#!/bin/sh
mv file1 /var/www/html/index.php
chmod 777 /var/www/html/index.php
mv file2 /home/ctf/pwn
chmod 777 /home/ctf/pwn
防御
通防
$str ="";
foreach ($_POST as $key => $value) {
$str.=$key;
$str.=$value;
}
if (preg_match("/system|tail|flag|exec|base64/i", $str)) {
die('no!');
}
通防正则黑名单
/\'|http|php|data|\"|\`|cookie|regexp|from|count|procedure|and|ascii|substr|substring|l
eft|right|union|if|case|pow|exp|order|sleep|benchmark|into|load|outfile|dumpfile|load_f
ile|join|show|select|update|set|concat|delete|alter|insert|create|union|or|drop|not|for
|join|is|between|group_concat|like|where|user|ascii|greatest|mid|substr|left|right|char
|hex|ord|case|limit|conv|table|mysql_history|flag|count|rpad|\&|\*|\.|\<|\>|-/i
多加正则过滤
<?
$filter =
"regexp|from|count|procedure|and|ascii|substr|substring|left|right|union|if|case|pow|ex
p|order|sleep|benchmark|into|load|outfile|dumpfile|loa
d_file|join|show|select|update|set|concat|delete|alter|insert|create|union|or|drop|not|
for|join|is|between|group_concat|like|where|user|ascii|gre
atest|mid|substr|left|right|char|hex|ord|case|limit|conv|table|mysql_history|flag|count
|rpad|\&|\*|\.|-";
if((preg_match($filter, $username)== 1) || (preg_match($filter,$password)== 1)){ die();
}
?>
addslashes() 函数 直接包裹住传入内容即可
PHP7 新特性 为 unserialize() 提供过滤
这个特性旨在提供更安全的方式解包不可靠的数据。它通过白名单的方式来防止潜在的代码注入。
<?php
// 将所有的对象都转换为 __PHP_Incomplete_Class 对象
$data = unserialize($foo, ["allowed_classes" => false]);
// 将除 MyClass 和 MyClass2 之外的所有对象都转换为 __PHP_Incomplete_Class 对象 $data = unserialize($foo, ["allowed_classes" => ["MyClass", "MyClass2"]); // 默认情况下所有的类都是可接受的,等同于省略第二个参数
$data = unserialize($foo, ["allowed_classes" => true]);
php_serialize 在5.5版本后新加的一种规则, 5.4及之前版本,如果设置成php_serialize会报错。
正确设置序列化及反序列化时使用的处理器
ini_set('session.serialize_handler', 'php_serialize');
ini_set('session.serialize_handler', 'php');
两者处理session的方式不同,错误使用会形成基于session的反序化漏洞
$filter = "phar|zip|compress.bzip2|compress.zlib";
if(preg_match("/".$filter."/is",$name)== 1){
die();
}
类的白名单校验机制
对所有传入的反序列化对象,在反序列化过程开始前,对类型名称做一个检查,不符合白名单的类不进行反序列化 操作。
禁止 JVM 执行外部命令 Runtime.exec
Java 一般来说安全性问题较少,出现的一些问题大部分是利用反射,最终用Runtime.exec(String cmd)函数来执行 外部命令的。
SecurityManager originalSecurityManager = System.getSecurityManager();
if (originalSecurityManager == null) {
// 创建自己的SecurityManager
SecurityManager sm = new SecurityManager() {
private void check(Permission perm) {
// 禁止exec
if (perm instanceof java.io.FilePermission) {
String actions = perm.getActions();
if (actions != null && actions.contains("execute")) {
throw new SecurityException("execute denied!");
}
}
// 禁止设置新的SecurityManager,保护自己
if (perm instanceof java.lang.RuntimePermission) {
String name = perm.getName();
if (name != null && name.contains("setSecurityManager")) {
throw new SecurityException("System.setSecurityManager
denied!");
}
}
}
@Override
public void checkPermission(Permission perm) {
check(perm);
}
@Override
public void checkPermission(Permission perm, Object context) {
check(perm);
}
};
System.setSecurityManager(sm);
}
xss漏洞关键就是寻找参数未过滤的输出函数。 常见的输出函数有: echo 、printf、print 、print_r 、sprintf、die 、var_dump 、 var_export 等
xss都是对用户输入以及客户端显示进行过滤或者转义
<?php
$name = htmlspecialchars( $_GET[ 'name' ] );
echo "<pre>Hello ${name}</pre>"; }
?>
或者过滤关键字
$pattern = "script|<|>"
if(preg_match("/".$pattern."/is",$cc)== 1){
die();
}
方法一:
xxe ,的防御之需要禁用外部实体的依赖即可
PHP:
libxml_disable_entity_loader(true);
JAVA:
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
dbf.setExpandEntityReferences(false);
Python:
from lxml import etree
xmlData = etree.parse(xmlSource,etree.XMLParser(resolve_entities=False))
方法二:
过滤用户提交的XML数据
关键词: <!DOCTYPE和<!ENTITY,或者, SYSTEM和PUBLIC。
$pattern = "ENTITY|<|>|DOCTYPE|SYSTEM|PUBLIC"
if(preg_match("/".$pattern."/is",$cc)== 1){
die();
}
方法一:
过滤类型
<?
if
(($_FILES["Up10defile"]["type"]=="image/gif")&&(substr($_FILES["Up10defile"]["name"],
strrpos($_FILES["Up10defile"]["name"],'.')+1))=='gif')&&($_FILES["file"]["size"]
<1024000){
//文件上传内容
} else{
die();
}
?>
方法二:
强制加后缀
<?php
if (file_exists("upload_file/" . $_FILES["Up10defile"]["name"])) {
echo $_FILES["Up10defile"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["Up10defile"]["tmp_name"], "upload_file/" .
$_FILES["Up10defile"]["name"] . ".gif");
echo "Stored in: " . "upload_file/" . $_FILES["Up10defile"]["name"] . ".gif";
}
文件包含
相关函数: • include() • include_once() • require() • require_once()
本地路径包含限制
<?php
$filename = $_GET['filename'];
$pattern =
"\/|\.\.\/|\.\/|etc|var|php|jpg|jpeg|png|bmp|gif|flag|?";
if(preg_match("/".$pattern."/is",$filename)== 1){
echo "die00000000000000000000000000000";
die();
}
include($filename);
伪协议
<?php
$filename = $_GET['filename'];
$pattern =
"\/|\.\.\/|\.\/|etc|var|php|jpg|jpeg|png|bmp|gif|file|http|ftp|php|zlib|data|glob|phar|
ssh2|rar|ogg|expect|zip|compress|filter|input|flag|?";
if(preg_match("/".$pattern."/is",$filename)== 1){
echo "die00000000000000000000000000000";
die();
}
include($filename);
命令执行
在PHP中常用到以下几个函数来执行外部命令 system() 、exec() 、passthru() 、shell_exec() 、popen()、 proc_open() 、pcntl_exec()
调用函数过滤不严
<?php
$filename = $_GET['filename’];
$pattern =
"eval|assert|passthru|pcntl_exec|exec|system|escapeshellcmd|popen|chroot|scandir|chgrp|
chown|shell_exec|proc_open|proc_get_status|ob_start";
if(preg_match("/".$pattern."/is",$filename)== 1){
die();
}
禁用或过滤代码执行函数
$a=$_GET['db'];
$pattern =
"call_user_func|call_user_func_array|array_map|array_filter|ob_start|phpinfo|eval|asser t|passthru|pcntl_exec|exec|system|escapeshellcmd|popen|chroot|scandir|chgrp|chown|shell _exec|proc_open|proc_get_status|ob_start";
if(preg_match("/".$pattern."/is",$db)== 1){
die();
}
常见任意文件读取函数: file_get_contents() 、highlight_file() 、fopen() 、readfile() 、fread() 、fgetss() 、fgets()、 parse_ini_file() 、 show_source() 、file()
读取限制
<?php
$filename = $_GET['filename'];
$pattern =
"\/|\.\.\/|\.\/|etc|var|file|http|ftp|php|zlib|data|glob|phar|ssh2|rar|ogg|expect|zip|c ompress|filter|input";
if(preg_match("/".$pattern."/is",$filename)== 1){
echo "die00000000000000000000000000000";
die();
}
echo file_get_contents($filename);
?>
限定文件访问范围
可以直接使用伪协议的防御方法也可以限定文件访问范围 open_basedir="/var/www/html"
if request.args.get("name"):
black_symbol = ["class","os","popen","_"," ", "'", '"', "$", "*", ",", ".","
{","}","\\","0x","0o","/","+","*"]
for ban in black_symbol:
if ban in request.args.get("name"):
return "error!"