Java-removeSemicolonContentInternal分析


0x00 前言

    本篇文章是为了给下一篇代审做基础,这里讲述这个方法的作用,如何创建和如何调试。本人能力有限有不足之处,请见谅


0x01 创建

创建一个maven,这里的jdk版本应设置1.8

Java-removeSemicolonContentInternal分析

设置pom.xml

<dependencies>    <dependency>      <groupId>javax.servlet</groupId>      <artifactId>javax.servlet-api</artifactId>      <version>3.0.1</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-webmvc</artifactId>      <version>5.3.30</version>    </dependency>  </dependencies>
<build> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>80</port> <path>/</path> </configuration> </plugin> </plugins> </build>

刷新maven,依赖类打上

Java-removeSemicolonContentInternal分析

创建一个java目录,还有两个软件包com.spring.config 和com.spring.controller

在com.spring,controller下创建一个hello

定义为控制器,并且写上路径和方法

package com.spring.controller;
import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
@Controllerpublic class hello { @RequestMapping("/test") @ResponseBody    public String test(){ return "hello world";    }}

Java-removeSemicolonContentInternal分析

在com.spring.config下创建一个springmvc

指定com.spring.controller为控制器

package com.spring.config;
import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;
@Configuration@ComponentScan("com.spring.controller")public class springmvc {}

Java-removeSemicolonContentInternal分析

在com.spring.config创建一个SerlvetContains

  • 这个Java函数是一个继承自AbstractDispatcherServletInitializer的类ServletContainers的实现。它包含三个方法:createServletApplicationContext()、getServletMappings()和createRootApplicationContext()。
    createServletApplicationContext()方法创建并返回一个AnnotationConfigWebApplicationContext对象,该对象用于管理

  • Spring MVC应用程序的上下文。它通过调用register()方法注册了一个名为springmvc的配置类。

  • getServletMappings()方法返回一个包含一个字符串元素的数组,该字符串表示Spring MVC应用程序的映射路径。在这个例子中,映射路径是根路径”/”。

  • createRootApplicationContext()方法返回null,表示不创建根上下文。

package com.spring.config;
import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;import org.springframework.web.servlet.support.AbstractDispatcherServletInitializer;
public class SerlvetContains extends AbstractDispatcherServletInitializer { @Override protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext crx = new AnnotationConfigWebApplicationContext(); crx.register(springmvc.class); return crx; }
@Override protected String[] getServletMappings() { return new String[]{"/"}; }
@Override protected WebApplicationContext createRootApplicationContext() { return null; }}

Java-removeSemicolonContentInternal分析

创建一个tomcat服务器 部署包

Java-removeSemicolonContentInternal分析

Java-removeSemicolonContentInternal分析

然后这里的目录结构的jdk也设置为1.8版本

Java-removeSemicolonContentInternal分析

访问test

Java-removeSemicolonContentInternal分析


0x02 调试

    在这个依赖中打断点

org/springframework/spring-web/5.3.30/spring-web-5.3.30.jar!/org/springframework/web/util/UrlPathHelper.class#decodeAndCleanUriString

Java-removeSemicolonContentInternal分析

    debug调试访问

http://localhost:8082/springmvc/test;122

Java-removeSemicolonContentInternal分析

    定位到这个方法

    接受的参数为:uri:/springmvc/test;122

Java-removeSemicolonContentInternal分析

      

跟进方法进行调试

首先

int semicolonIndex = requestUri.indexOf(59);是观察有没有59字符,59字符就是分号,如果有为1,没有为-1进入到下面的if循环返回uri参数

这里的semicolonIndex为15,则是为截取分号前的数:/springmvc/test

Java-removeSemicolonContentInternal分析


因为semicolonIndex为15 不等于-1,则进入else中的for循环

for(sb = new StringBuilder(requestUri); semicolonIndex != -1; semicolonIndex = sb.indexOf(“;”, semicolonIndex)

  • 该函数的功能是将字符串requestUri中的字符59替换为分号,并将替换后的字符串赋值给StringBuilder对象sb。具体步骤如下:

  • 创建一个StringBuilder对象sb,并将字符串requestUri赋值给sb。

  • 使用indexOf方法查找字符串sb中字符59的位置索引,并将结果赋值给变量semicolonIndex。

Java-removeSemicolonContentInternal分析

那么这里的


  • semicolonIndex是15代入


  • sb值为”/springmvc/test;12321321321313213″(这里因为之前的调试值忘记更改,作用都一样),顺序也为下

    Java-removeSemicolonContentInternal分析



这里先解释indexof函数的意思


  • 在这个例子中,requestUri是一个字符串,其值为“/springmvc/test;123”indexOf方法用于查找指定字符在字符串中的索引位置。


    在调用indexOf方法时,传入了两个参数:要查找的字符(分号;)和起始索引位置(15)。这里的起始索引位置表示从字符串的第15个字符开始查找。


    在这个例子中,requestUri字符串的第15个字符是分号;,因此indexOf方法返回的索引位置为15


    需要注意的是,indexOf方法返回的是指定字符在字符串中第一次出现的索引位置。如果指定字符不存在于字符串中,则返回-1。在这个例子中,由于分号存在于字符串中,所以返回的索引位置为15

    Java-removeSemicolonContentInternal分析


    如果我这里是16那么会返回-1


    Java-removeSemicolonContentInternal分析


所以这里这里则是截取第16位,第16位是1而不是/,则返回-1

Java-removeSemicolonContentInternal分析

这里利用substring函数,从0截取到15正好是/springmvc/test

Java-removeSemicolonContentInternal分析

最后返回的则是/springmvc/test

Java-removeSemicolonContentInternal分析



0x03 总结

Spring 在处理url时,removeSemicolonContentInternal方法,主要的功能是

  1. 移除所有的分号

  2. 移除分号后面直到下一个斜杠”/”之间的所有字符


0x04 结尾

    如有写的不好之处请各位见谅






原文始发于微信公众号(Poker安全):Java-removeSemicolonContentInternal分析

版权声明:admin 发表于 2024年2月20日 下午3:44。
转载请注明:Java-removeSemicolonContentInternal分析 | CTF导航

相关文章