概述
Access Control: Database说白了就是权限控制。在访问数据库(sql和nosql)需要加入当前用户的权限控制。不然会被fortify扫描出来,认为客户端可能不挟持和假冒,从而导致数据被泄露。 但是这个并不是任何时候都需要的,有的接口本来就是可以任意访问的。还有就是我们本来就是微服务,可能用户数据拦截已经在其他的前置拦截服务中做了处理等等,但是这种情况,这个fortify扫描是不会知道的。
这就导致,每一次做安全扫描都会有一堆问题需要改。这里就探讨一下如何解决这个问题。
解决方式
其实最本质的方法就是在代码中从springboot框架中获取当前用户,这样就能解决,但是这个方法有时候我们没有或者并不需要。
那么就只有另一种方法,迷惑它。让fortify觉得我们已经对参数做了处理,并不是原来的数据了。
通过查找资料和多次测试。我找到了两种方式。这里不讨论代码的可读性和其他,就只保证改完了之后能用还不被扫描出来。当然这只是我测试出来的方法。
第一种:反射代理
通过反射的方式就是利用java的特性,让fortify找不到具体的实现,这样就只能被认为没有问题。
import java.lang.reflect.Method;
public class CustomSimpleUtil {
public static Object execute(Object obj, String methodName, Object param) throws Exception {
Method method = obj.getClass().getMethod(methodName);
ReflectionUtils.makeAccessible(method);
Object result = method.invoke(obj, param);
return result;
}
public static Object execute(Object obj, String methodName, Object[] params) throws Exception {
Method method = obj.getClass().getMethod(methodName, paramClasses);
ReflectionUtils.makeAccessible(method);
Object result = method.invoke(obj, params);
return result;
}
}
第二种:就是绕行
这种方法原理就是通过假装处理了输入的参数来骗过fortify。就是将原来的参数包装起来在转换回去。
先建一个包装类:
import lombok.Data;
import java.util.List;
@Data
public class DetourDto {
private Object originData;
private Integer type;
public DetourDto(Object originData){
if(originData instanceof Integer){
type = 1;
}
else if(originData instanceof Long){
type = 2;
}
else if(originData instanceof Short){
type = 3;
}
else if(originData instanceof Double){
type = 4;
}
else if(originData instanceof Float){
type = 5;
}
else if(originData instanceof List){
type = 6;
}
else if(originData instanceof String []){
type = 7;
} else {
type = 8;
}
}
public Object getDetourValue(){
if(type ==1){
return (Integer)originData;
}else if(type == 2){
return (Long)originData;
}else if(type == 3){
return (Short)originData;
}else if(type == 4){
return (Double)originData;
}else if(type == 5){
return (Float)originData;
}else if(type == 6){
return (List)originData;
}else if(type == 7){
return (String[])originData;
}else{
return originData;
}
}
}
这个包装类,可以减少if判断,避免高并发时浪费性能。
在建一个工具类,用于创建包装类,获取包赚后的参数
import com.xxxx.DetourDto;
import java.lang.reflect.Method;
import java.util.function.Function;
import java.util.function.Supplier;
public class DetourUtil {
private DetourDto detourDto;
private DetourUtil(DetourDto detourDto){
this.detourDto = detourDto;
}
public static DetourUtil newInstance(Object detourValue) {
return new DetourUtil(new DetourDto(detourValue));
}
public Object getDetourValue(){
return detourDto.getDetourValue();
}
}
最后再通过,强转得到新的参数:
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
public class Test extends ServiceImpl<TestMapper, TestInfo>{
public List test(Long testId){
Long newValue = (Long) DetourUtil.newInstance(testId).getDetourValue();
return getBaseMapper().getDetail(newValue);
}
}
经过测试,以上方式可以绕过fotify的检测。