2020-02-12Jsp挖掘(6)-JSP命令执行漏洞

JSP命令执行漏洞

一、代码执行成因

调用Runtime但是没有对传入参数没有过滤和限制:

jsp调用命令行的只需要在<%%> 中编写使用Runtime类即可调用命令行

Windows 用“&”,linux用“;”号来分隔多条命令

'cmd.exe /c type "E:\jsp\WebGoat-5.3_RC1\tomcat\webapps\webgoat\lesson_plans\English\BasicAuthentication.html"'

注入数据"%26dir *

二、实例子操作

http://localhost:8080/WebGoat/attack?Screen=11&menu=1100

抓去数据包

数据执行的语句:

'cmd.exe /c type "C:\WebGoat-5.4\tomcat\webapps\WebGoat\lesson_plans\English\BasicAuthentication.html"'

闭合:

"%26 dir *

执行的结果:

'cmd.exe /c type "C:\WebGoat-5.4\tomcat\webapps\WebGoat\lesson_plans\English\AccessControlMatrix.html"& dir *"'

尝试其他的查看:

"%26 netstat -a

返回相关信息

防护方法:

找到系统的相关命令执行的源码:

执行系统的关键源代码 private String exec(WebSession s, String[] command) { System.out.println("Executing OS command: " + Arrays.asList(command)); ExecResults er = Exec.execSimple(command); // the third argument (index 2) will have the command injection in it if ((command[2].indexOf("&") != -1 || command[2].indexOf(";") != -1) && !er.getError()) { makeSuccess(s); }  return (er.toString()); }  ----------------------------------------------------------------------------------  过滤危险字符        private String exec(WebSession s, String command)        {              System.out.println("Executing OS command: " + command);              if ((command.indexOf("&") != -1 || command.indexOf(";") != -1)                || command.indexOf("\"") != -1) || command.indexOf("|") != -1))//添加的过滤的特殊符合              {                      return "";              }                ExecResults er = Exec.execSimple(command);              if ((command.indexOf("&") != -1 || command.indexOf(";") != -1) && !er.getError())              {                      makeSuccess(s);              }              return (er.toString());        }

保存Java文件,重起tomcat环境

将修改的.java文件编译成.class文件替换对应的原来的.class文件防护就OK。

参考前面怎么本地编译.java文件。

url:https://mp.weixin.qq.com/s?__biz=MzIyNjk0ODYxMA==&mid=2247483687&idx=1&sn=8551b072266d69f6eb323d2a76f9b77f&chksm=e869e24adf1e6b5c25d5fe542826d6af754903b53889f020839fe1985770021b3290b8504c97&token=699917353&lang=zh_CN#rd

三、防护总结:

1、安全产品waf等等过滤相关的关键词。

2、系统的代码层面,可以关键点执行的相关函数过滤相关的关键词,或者是全局过滤中过滤敏感字符和命令执行相关的代码。

分黑名单(不允许的一些字符)、白名单(运行的字符)。

参数编码

Unix 中

可以通过编码方式,避免命令的注入

/bin/bash script.sh\;rm *

/bin/bash –c “sh script.sh\; rm *”

四、注意了解:

相关命令执行文章:https://www.cnblogs.com/unixcs/p/10475131.html

参考文献:https://blog.csdn.net/weixin_43706468/article/details/88819431

​公众号:

thelostworld:

个人知乎​:https://www.zhihu.com/people/fu-wei-43-69/columns

​个人:https://www.jianshu.com/u/bf0e38a8d400

你可能感兴趣的:(2020-02-12Jsp挖掘(6)-JSP命令执行漏洞)