• 渗透之路基础 -- 代码执行漏洞


    漏洞原理

    PHP代码执行漏洞可以将代码注入到应用中,最终到webserver去执行。攻击者可以将恶意代码通过参数带入到后台并且被函数解析为对应代码进行执行

    该漏洞主要存在于eval()、assert()、preg_replace()、call_user_func()、array_map()以及动态函数中。

    形成原因

    参数没有做好过滤,导致任意代码都会被后台函数解释为脚本代码执行

    漏洞危害

    • 执行任意代码
    • 向网站写WebShell
    • 控制整个网站甚至服务器

    php中常见函数

    • e v a l ( )
    • p r e g _ r e p l a c e ( " / / e " , $ _ G E T [ ' h ' ] , " " ) ;
    • a r r a y _ m a p ( )
    • u s o r t ( ) , u a s o r t ( ) , u k s o r t ( )
    • a r r a y _ f i l t e r ( )
    • a r r a y _ r e d u c e ( )
    • a r r a y _ d i f f _ u a s s o c ( ) , a r r a y _ d i f f _ u k e y ( )
    • a r r a y _ u d i f f ( ) , a r r a y _ u d i f f _ a s s o c ( ) , a r r a y _ u d i f f _ u a s s o c ( )
    • a r r a y _ i n t e r s e c t _ a s s o c ( ) , a r r a y _ i n t e r s e c t _ u a s s o c ( )
    • a r r a y _ u i n t e r s e c t ( ) , a r r a y _ u i n t e r s e c t _ a s s o c ( ) , a r r a y _ u i n t e r s e c t
    • _ u a s s o c ( )
    • a r r a y _ w a l k ( ) , a r r a y _ w a l k _ r e c u r s i v e ( )
    • x m l _ s e t _ c h a r a c t e r _ d a t a _ h a n d l e r ( )
    • x m l _ s e t _ d e f a u l t _ h a n d l e r ( )
    • x m l _ s e t _ e l e m e n t _ h a n d l e r ( )
    • o b _ s t a r t ( )
    • u n s e r i a l i z e ( )
    • x m l _ s e t _ e n d _ n a m e s p a c e _ d e c l _ h a n d l e r ( )
    • x m l _ s e t _ e x t e r n a l _ e n t i t y _ r e f _ h a n d l e r ( )
    • x m l _ s e t _ n o t a t i o n _ d e c l _ h a n d l e r ( )
    • x m l _ s e t _ p r o c e s s i n g _ i n s t r u c t i o n _ h a n d l e r ( )
    • x m l _ s e t _ s t a r t _ n a m e s p a c e _ d e c l _ h a n d l e r ( )
    • x m l _ s e t _ u n p a r s e d _ e n t i t y _ d e c l _ h a n d l e r ( )
    • s t r e a m _ f i l t e r _ r e g i s t e r ( )
    • s e t _ e r r o r _ h a n d l e r ( )
    • r e g i s t e r _ s h u t d o w n _ f u n c t i o n ( )
    • r e g i s t e r _ t i c k _ f u n c t i o n ( )
    • a s s e r t ( )

    防御

    • 对于eval()等解析代码的函数,不能让用户轻易的接触到参数,并且要对传入的参数严格的判断和过滤
    • 字符串利用单引号包裹参数,可以在插入前进行addslashes()
    • 对于preg_replace放弃使用e修饰符。如果必须要用e修饰符,请保证第二个参数中,对于正则匹配出的对象,用单引号包裹 。
    • escapeshellcmd() 将字符串命令中的特殊字符过滤掉
    • escapeshellarg() 将参数加上双引号

    eval 是php中执行代码的函数

    假设访问 192.168.203.128/test/testcmd.php?x=echo 123;

    假设访问 192.168.203.128/test/testcmd.php?x=phpinfo();

    通过给 x 传递写入文件代码,执行命令

    // 简单文件写入代码
    $file=$_GET['f'];
    $data=$_GET['d'];
    $fileio=fopen($file,'w+');
    fwrite($fileio,$data);
    fclose($fileio);
    

    成功写入一句话

    利用代码执行漏洞可以提权

  • 相关阅读:
    关于JAVA中URL传递中文参数的问题
    java中级,知识点归纳(一)
    Python中的常见特殊方法或属性—— dir方法和dict属性
    Python中的常见特殊方法—— del方法
    Python中的常见特殊方法—— repr方法
    Python关于多继承
    Python中的@函数装饰器到底是什么?
    Python中的函数(高级使用)
    Windows怎么安装配置Elasticsearch
    Python中单元测试出错了,会怎么样?
  • 原文地址:https://www.cnblogs.com/r0ckysec/p/11529380.html
Copyright © 2020-2023  润新知