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

漏洞原理

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中执行代码的函数

1404622-20190916194516436-2125802833.png

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

1404622-20190916194522178-927576347.png

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

1404622-20190916194528593-162033391.png

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

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

1404622-20190916194536472-542229541.png

1404622-20190916194543319-1162882248.png

成功写入一句话

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

转载于:https://www.cnblogs.com/r0ckysec/p/11529380.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值