• 奇安信-源代码安全缺陷问题解决记录:路径遍历、API误用、配置文件明文


    输入验证-路径遍历

    • pom.xml: 引入commons-io
      <dependency>
           <groupId>commons-iogroupId>
           <artifactId>commons-ioartifactId>
           <version>2.4version>
      dependency>
      
      • 1
      • 2
      • 3
      • 4
      • 5
    • 代码替换
      File file = new File(logpath);
      FileInputStream logfile = new FileInputStream(file);          
      
      • 1
      • 2
      替换为
      import org.apache.commons.io.FileUtils;
      File file = FileUtils.getFile(logpath);
      FileInputStream logfile = FileUtils.openInputStream(file);
      
      • 1
      • 2
      • 3

    API误用-不安全的框架绑定

    • @InitBinder 只对当前Controller生效,因此可以创建BaseController,其他Controller直接继承该类
      import org.springframework.web.bind.WebDataBinder;
      import org.springframework.web.bind.annotation.InitBinder;
      import org.springframework.web.bind.annotation.RestController;
      
      /**
       * 奇安信 Controller.
       * @author lw-rxz
       */
      @RestController
      public class BaseController {
          @InitBinder
          public void initBinder(WebDataBinder binder) {
              binder.setDisallowedFields("test");
          }
      } 
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
    • 针对@RequestBody传参, @InitBinder 是无效的,因此通过@JsonIgnoreProperties(ignoreUnknown = true)解决
      在这里插入图片描述

    密码管理-配置文件中的明文密码

    在这里插入图片描述

    jasypt

    • pom.xml:引入jasypt

      <dependency>
           <groupId>com.github.ulisesbocchiogroupId>
           <artifactId>jasypt-spring-boot-starterartifactId>
           <version>2.0.0version>
       dependency>
      
      • 1
      • 2
      • 3
      • 4
      • 5

      在这里插入图片描述
      BasicTextEncryptor对应jar包org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar

    • 生成密钥,替换配置文件

      import org.jasypt.util.text.BasicTextEncryptor;
      /**
       * 生成密文.
       * @author lw-rxz
       */
      public class JasyptUtils {
          public static void main(String[] args) {
              //PBEWithMD5AndDES
              BasicTextEncryptor encryptor = new BasicTextEncryptor();
              //加密
              encryptor.setPassword("key");
              System.out.println(encryptor.encrypt("%8Y!R-PHSA1LJ9_z"));
              //解密
              System.out.println(encryptor.decrypt("gZdoGpddkg8dCtdlYmjlAulXUo+Cqr6/LgxcUgfmVOE="));
          }
      
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
    jasypt:
      encryptor:
        password: key
    
    • 1
    • 2
    • 3

    也可以自定义的密码串标识,默认为:ENC(…)

     property:
          prefix: "xx@["
          suffix: "]"
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    冲突

    jasypt一开始引入的2.1.1版本,结构导致校验包javax.validation失效,换成2.0.0即可

    <dependency>
        <groupId>javax.validationgroupId>
        <artifactId>validation-apiartifactId>
        <version>2.0.1.Finalversion>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    依然报明文缺陷

    jasypt:
      encryptor:
        password: key #依然报明文缺陷
    
    • 1
    • 2
    • 3

    替换为环境变量或者作为命令行传入:
    在这里插入图片描述
    官网

    --jasypt.encryptor.password=key
    
    • 1
    -Djasypt.encryptor.password=password
    
    • 1

    null引用

    • 主要为了防止空指针,实际上都没有问题,比如baseMapper.selectList(null);但还会提示缺陷,这种可以通过创建常量null

       public static Object NULL = null;
       baseMapper.selectList(XXXNULL );//需要转换一下,比如(Test)NULL
      
      • 1
      • 2
    • 还有一种就是创建空对象,不要使用null

      QueryWrapper queryMapper = null;
      if (name!= null) {
           queryMapper = new  QueryWrapper<>();
           queryMapper.eq("name", name);
      }
      baseMapper.selectList(queryMapper)
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6

      替换为:

      QueryWrapper queryMapper = new  QueryWrapper<>();
      if (name!= null) {
           queryMapper.eq("name", name);
      }
      baseMapper.selectList(queryMapper)
      
      • 1
      • 2
      • 3
      • 4
      • 5

    总结

    除了明文处理,其他几种缺陷感觉非常多此一举,仅仅就是为了不被扫描到缺陷,所以上面很多改动的意义也仅仅是为了通过扫描。

    20230912------------又开始了,发现一份官方文档,可以参考下:
    奇安信漏洞说明
    在这里插入图片描述

  • 相关阅读:
    力扣--找不同
    asp.net core之中间件
    YOLOv7基于自己的数据集从零构建模型完整训练、推理计算超详细教程
    167. 两数之和 II - 输入有序数组、Leetcode的Python实现
    绘制矩阵散点图
    LazySnapping算法详解
    Qt中的信号和槽详解
    Android 监听WebView加载失败
    再来了解机器学习和深度学习_有监督机器学习_无监督机器学习---人工智能工作笔记0018
    MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
  • 原文地址:https://blog.csdn.net/qq_36434219/article/details/127659944