当前位置: 首页 > news >正文

【java代码审计】命令注入

1 成因

开发者在某种开发需求时,需要引入对系统本地命令的支持来完成某些特定的功能,此时若未对用户的输入做严格的过滤,就可能发生命令注入。

2 造成命令注入的类或方法

  • Runtime类:提供调用系统命令的功能

    ①Runtime.getRuntime():获得JVM运行时的环境

    ②Runtime.getRuntime().exec(cmd)执行用户输入的cmd命令

    存在命令注入的代码如下:

    protected void doGet (HttpServletRequest req, HttpServletRequest resp) throws ServletException, IOException{String cmd = req.getParameter("cmd");Process process = Runtime.getRuntime().exec(cmd);InputStream in = process.getInputStream();ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();byte[] b = new byte[1024];//获取1M的一个缓冲区int i = -1;while((i=in.read(b)) != -1)//判断是否读完{byteArrayOutputStream.write(b,0,i);}PrintWriter Out = resp.getWriter();out.print(new String(byteArrayOutputStream.toByteArray()));
    }
    
  • ProcessBuilder:可以创建操作系统进程

    //利用指定的操作系统程序和参数构造一个进程生成器。
    ProcessBuilder(String… command) //设置此进程生成器的操作系统程序和参数。 
    command(List<String> command) 
    command(String… command) //设置此进程生成器的工作目录。
    directory(File directory) //返回此进程生成器环境的字符串映射视图。 environment方法获得运行进程的环境变量,得到一个Map,可以修改环境变量 
    environment() //使用此进程生成器的属性启动一个新进程。
    start() 
    
  • Groovy

    execute():可执行shell命令,eg:

    def command = "git log"
    def proc = command.execute()//执行git log的命令
    proc.waitFor()
    def status = proc.exitValue()
    

    result = sh(script: "shell command", returnStdout: true).trim()

    GroovyShell()

    //直接执行Groovy代码
    GroovyShell shell = new GroovyShell();shell.evaluate("\'calc\'.execute()");
    //通过加载本地脚本
    //1.
    GroovyShell shell = new GroovyShell();
    Script script = shell.parse(new File("src/main/java/ysoserial/vulndemo/GroovyTest.groovy"));
    script.run();
    //2.
    GroovyShell shell = new GroovyShell();
    shell.evaluate(new File("src/main/java/ysoserial/vulndemo/GroovyTest.groovy"));
    //通过加载远程脚本
    GroovyShell shell = new GroovyShell();shell.evaluate(new URI("http://127.0.0.1:8888/GroovyTest.groovy"));
    

3 连接符进行命令注入

在这里插入图片描述

如下代码不存在命令注入漏洞

protected ByteArrayOutputStream ping(String url) throws IOException{Process process = Runtime.getRuntime().exec("ping " + url);InputStream in = process.getInputStream();ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();byte[] b = new byte[1024];int i = -1;while((i = in.read(b)) != -1){byteArrayOutputStream.write(b,0,i);}return byteArrayOutputStream;
}

若注入www.baidu.com&ipconfig时,java环境会把他当做完整的字符串,不会被当作两条命令执行。

对于Java环境中的命令注入,连接符的使用存在一些局限。

4 正确处理可控参数

public Process exec(String command) throws IOException {return exec(command, null, null);}
public Process exec(String command, String[] envp) throws IOException {return exec(command, envp, null);}
public Process exec(String cmdarray[]) throws IOException {return exec(cmdarray, null, null);}
public Process exec(String[] cmdarray, String[] envp, File dir)throws IOException {return new ProcessBuilder(cmdarray).environment(envp).directory(dir).start();}

当传入的参数类型为字符串时,会先经过StringTokenizer的处理,主要是针对空格以及换行符等空白字符进行处理,后续会分割出一个cmdarray数组保存分割后的命令参数,其中cmdarray的第一个元素为所要执行的命令。经过处理后的参数www.baidu.com&ipconfig成为ping命令的参数,因此此时的连接符&并不生效,从而无法注入系统命令。

http://www.lryc.cn/news/24210.html

相关文章:

  • 速锐得适配北汽EX系列电动汽车CAN总线应用于公务分时租赁
  • 已解决ERROR: Failed building wheel for opencv-python-headless
  • 每日获取安全资讯的网站,国内外共120个
  • HUN工训中心:开关电路和按键信号抖动
  • WordPress 主题 SEO 标题相关函数和过滤器教程wp_get_document_title()
  • Qt 事件机制
  • 【Python】Numpy--np.linalg.eig()求对称矩阵的特征值和特征向量
  • 医疗床头卡(WIFI方案)
  • [YOLO] yolo博客笔记汇总(自用
  • Linux 常用 API 函数
  • 【转载】bootstrap自定义样式-bootstrap侧边导航栏的实现
  • 奇瑞x华为纯电智选车来了,新版ADS成本将大幅下降
  • 机器学习的特征归一化Normalization
  • 程序员看过都说好的资源网站,看看你都用过哪些?
  • Win11的两个实用技巧系列之设置系统还原点的方法、安全启动状态开启方法
  • 【Linux】项目的自动化构建-make/makefile
  • 【Redis学习2】Redis常用数据结构与应用场景
  • 踩了大坑:https 证书访问错乱
  • 大数据技术之Hive(四)分区表和分桶表、文件格式和压缩
  • 环形缓冲区(c语言)
  • 创建自助服务知识库的指南
  • 分层测试(1)分层测试是什么?【必备】
  • 开源ZYNQ AD9361软件无线电平台
  • 第四阶段-12关于Spring Security框架,RBAC,密码加密原则
  • JPA——Date拓展之Calendar
  • 一文吃透 Spring 中的 AOP 编程
  • Apple主推的智能家居是什么、怎么用?一篇文章带你从零完全入门 HomeKit
  • SpringCloud系列知识快速复习 -- part 1(SpringCloud基础知识,Docker,RabbitMQ)
  • 2023上半年北京/上海/广州/深圳NPDP产品经理认证报名
  • 面试半年,总结了1000道2023年Java架构师岗面试题