Java code audit - Iron Man download system

Beginner java code audit, along with their cousin footsteps and walk along the audit process, we chose not to use a Java framework java system, as entry.

The purpose of the audit process familiar with the code, looking for loopholes ideas, entry record.

Ready to work

In order to verify the vulnerabilities audit results, or to build up the system, otherwise empty rumor. For convenience, use  JspStudy 2016 an integrated environment, select  tomcat 8.0jdk 1.8 build. View code uses  IDEA, of course, can also be used  jd-gui, decompile  class, but  IDEA automatically to decompile, more convenient.

It is noteworthy that, after using the system comes with the installation function building, open the page error, hindsight may be automatically import sql file path program written died when the root directory and deploy their own position is not the same as a result.

Again through the  install/index.html pages re-installed, the display database already installed. The reason is that  WEB-INF/classes/liuxing_db.properties in  db_an=yes turned into  db_an=no, indicates that the database has been installed, will not install it again.

Finally found using the installation prompts in the second manual installation method can be properly installed system, artificial data into the database on the line.

In order to audit the code when global search convenient, you can use the  jad batch decompile  class file, using a command such as:

jad -r -d /path/to/store/java -s java -8 /path/to/classes/files/*/.class

Finally, I would decompile out java files, stored in a unified the  WEB-INF/java original directory in the directory, and class files  WEB-INF/classes corresponding to the directory.

Vulnerabilities found

Non-code audit framework, according to the  foreground - background, serious - low-risk, non-interactive - need to interact , try to follow the code flow and ease of use found in high-risk types of vulnerabilities based.

A: re-install Vulnerability

Like said in the preparatory work, even though I use the system comes installed function failed, but the  db_an argument becomes  no, although  /install reinstall the page directory is not deleted, but it does use the system comes installed feature does not exist to reinstall vulnerability .

Follow  /install/index.html page, find the  install.jsp file, and then based on  form action, find  install_setup.jspthe page

According to another  install_setup.jsp on the page  import statement

import="liuxing.util.Install,java.util.*"

Find the installation of the main logic source code, in the  WEB-INF/java/liuxing/util/Install.java time of installation will determine  db_an the value, yes you can install, no not install; after installation will be set to value  no, although not delete the install page, but has been unable to re-installed.

So, when using the system comes with the install page to install the system, there is no re-installation loopholes; if you use the manual importing sql file to install the system, but it did not put  db_an a value written  nowhen not delete the install directory files, re-install loopholes exist.

Two. SQL Injection Vulnerability

First, try the search function, enter  so.jsp, find the search parameter values passed in  Ruanjianguanli the method so

Into the  WEB-INF/java/liuxing/guanli/Ruanjianguanli.java file, find so method; discovered is called  ruanjianDao.so() function,

ruanjianDao what is it then?

在 Ruanjianguanli 类的构造函数里,可以找到,ruanjianDao 是 RuanjianMySQL 的一个实例,那么再接着往下跟

然后打开 WEB-INF/java/liuxing/dao/RuanjianMySQL.java 文件,搜索 so 方法,并发现最终是采用预编译来执行数据库操作,这里不存在 SQL 注入漏洞。

public pageModel so(int pageNo, int pageSize, String tiaojian, int lei, String name)

可想而知,有一处用了预编译,说明就有很多处用了预编译方式来执行 SQL 语句,基本都没有 SQL 注入漏洞。然后全局搜索 createStatement 关键字,看看有没有用拼接的SQL语句的。

如上图,最后也发现几个可以注入的地方,但是都需要登录后台,在 delete 语句中,可以用时间盲注,比如:

delete from user where id in('222') or if(ascii(substr(database(),1,1))>107,0,sleep(3));

语句来测试

利用:

但是后台都登录了,也没必要去纠结这个 SQL 注入漏洞了,进了后台应该有更容易利用的漏洞。

三. XSS 漏洞

由于系统较简单,前台能够产生 xss 的地方较少,会员注册后,登录;可以在修改邮箱处,用 Payload

""><script>alert(0)</script>

触发了前端会员的 xss。

 

而且在后台管理员修改用户资料处,尝试修改该用户资料,也会触发此 XSS。

代码层面,查看 denlu1.jsp 页面代码,跟踪 Userguanli 的 Xiugaiyonghu2 方法,里面传入了要修改成的邮箱和用户 ID

最终到 WEB-INF/java/liuxing/dao/UserDaoMySQL.java 文件中的 Xiugaiyonghu2 方法中,直接将前端传过来的 youxiang 值写入数据库中,没有任何安全防护,从而导致了存储型 xss。

四. 访问控制

没有登录管理员帐户,访问 /admin/admin.jsp 页面,会被重定向到 /buzai.htm 页面。打开 admin.jsp 页面,看看里面的跳转逻辑,搜索关键字:

RequestDispatcher

getRequestDispatcher

sendRedirect

setHeader

forward

没有发现跳转语句,判断是全局 Filter 的权限认证。查看 WEB-INF/web.xml,发现针对所有 jsp 文件的 AuthFilter 过滤器

顺着 filter-class,找到 WEB-INF/java/liuxing/util/AuthFilter.java 文件,分析 doFilter 函数:

关键语句在于以下两个 if 判断:

如果路径不是指定的四个安装系统相关的路径,会尝试从数据库中的 lujing 表中查询存储在数据库中的 web 路径。

这里一个坑被我踩到了,没看代码的情况下,认为找到路径时 bl 为 true,没找到路径时 bl 为 false,结果最后解释不通了。

重新进入 WEB-INF/java/liuxing/util/LuJing.java 文件翻看代码,发现原来是没查到路径才返回 true,找到返回 false。

所以对于数据库中不存在的管理员路径 /admin/admin.jsp,返回 true。如果这时候访问者没有 session,或者读不到 admin 的 session,就会返回 true,然后就被重定向到 /buzai.htm 页面了。

所以,这里也不存在越权访问页面什么的漏洞。程序用 session 中的 user 和 admin 属性区分普通用户和管理员用户,猜想也没有垂直越权之类的漏洞,没有仔细查看。根据 session 中的 id 属性区分普通用户,平行越权也放弃了查找。所以不存在访问控制方面的漏洞。

五. 后台任意文件上传漏洞

在"其它管理"—"添加友情链接"处、"软件管理"—"软件发布"页面,都可以上传文件,在 web.xml 中或者顺着 jsp 页面调用寻找,都能够找到具体的逻辑代码

内部代码看起来都是一样的,以 WEB-INF/java/liuxing/util/shangchuan2.java 文件为例,关键代码如下:

其中会判断上传的文件名,要符合正则表达式 ".+\\\\(.+)$",才能够正常上传。即形似 xxx\\xx 的文件名,估计是为了匹配 Windows 路径中的 \\,比如 C:\\a.jpg。定义了内部禁止的后缀名 ".exe", ".com", ".cgi", ".asp",这就是唯一的过滤方式了。

继续往下看,写文件时,关键的一句代码:

item.write(new File((new StringBuilder(String.valueOf(getServletContext().getRealPath("/")))).append("wen/").append(date).append(men).toString()));

即将文件存放在 /wen 目录下,保存为 date+men 形式的文件名,两者都是可以控制的,直接修改写 shell 了。

吐槽一下,不清楚为什么要过滤 asp,不过滤 jsp? 其实正常上传 jsp 文件,注意 filename 参数的正则匹配就好了

六. 寻找其它漏洞

1、CSRF 漏洞

全系统都没有针对 CSRF 漏洞进行防御,应该存在不少 CSRF 漏洞,不一一分析了。

2、其它

命令执行漏洞,因系统没使用什么框架,所以全局搜索关键字 getRuntime,没发现存在执行系统命令的情况,不存在命令执行漏洞。XXE、反序列化等按照系统的需求,连相应功能估计也没有,所以放弃了。

附:

源码下载地址:

http://down.chinaz.com/soft/25711.htm

参考文章:

铁人下载系统代码审计:

http://www.codersec.net/2016/12/%E9%93%81%E4%BA%BA%E4%B8%8B%E8%BD%BD%E7%B3%BB%E7%BB%9F%E4%BB%A3%E7%A0%81%E5%AE%A1%E8%AE%A1/

JAVA 代码审计之铁人下载系统 v1.0

http://foreversong.cn/archives/1005

JAVA 代码审计的一些Tips(附脚本)

https://xianzhi.aliyun.com/forum/topic/1633)

Guess you like

Origin www.cnblogs.com/Fluorescence-tjy/p/11222118.html