java代码审计-Java 不安全的反射 unsafe reflection

 

攻击者能够建立一个在开发者意料之外的、不可预测的控制流程,贯穿应用程序始终。

这种形式的攻击能够使得攻击者避开身份鉴定,或者访问控制检测,或者使得应用程序以一种意料之外的方式运行。

如果攻击者能够将文件上传到应用程序的classpath或者添加一个classpath的新入口,那么这将导致应用程序陷入完全的困境。

无论是上面哪种情况,攻击者都能使用反射将新的、多数情况下恶意的行为引入应用程序。

应对措施:
开发者可以定制一份白名单,通过关键字关联需要实例化的类,http请求中传递是不是实际的类名,而是关键字,开发者得到关键字后在白名单中寻找需要的信息,进行实例化。

一开始看到上面的解释我也是感觉比较抽象,很难理解。让我们先看下面这一段代码:


 1 public static Map<String, Object> beanToMap(Object obj) {
 2 
 3     if (obj == null) {
 4     return null;
 5   }
 6   Map<String, Object> map = new HashMap<String, Object>();
 7     try {
 8       BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
 9       PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
10       for (PropertyDescriptor property : propertyDescriptors) {
11         String key = property.getName();
12         // 过滤class属性
13         if (!key.equals("class")) {
14           // 得到property对应的getter方法
15           Method getter = property.getReadMethod();
16           Object value = getter.invoke(obj);
17           map.put(key, value);
18         }
19 
20       }
21     } catch (Exception e) {
22 
23       logger.error("transBean2Map Error ", e);
24     }
25   return map;
26 }
原文地址:https://www.cnblogs.com/jayus/p/11384864.html