This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
/
ProjectSettingStore.java
207 lines (176 loc) · 8.23 KB
/
ProjectSettingStore.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.coreyd97.BurpExtenderUtilities;
import burp.IBurpExtenderCallbacks;
import burp.IHttpRequestResponse;
import burp.IHttpService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
class ProjectSettingStore implements IHttpRequestResponse {
private final Preferences preferenceController;
private final IBurpExtenderCallbacks callbacks;
private final IHttpService httpService;
private final byte[] requestBytes;
private final String extensionIdentifier;
private String serializedValue;
private HashMap<String, Object> preferences;
private HashMap<String, Type> preferenceTypes;
private HashMap<String, Object> preferenceDefaults;
public ProjectSettingStore(Preferences preferenceController, IBurpExtenderCallbacks callbacks,
String extensionIdentifier) throws MalformedURLException, UnsupportedEncodingException {
this.preferenceController = preferenceController;
this.callbacks = callbacks;
this.httpService = callbacks.getHelpers().buildHttpService("PROJECT-EXTENSION-PREFERENCE-STORE-DO-NOT-DELETE", 65535, true);
this.extensionIdentifier = URLEncoder.encode(extensionIdentifier, "UTF-8");
this.requestBytes = callbacks.getHelpers().buildHttpRequest(
new URL(httpService.getProtocol(), httpService.getHost(), httpService.getPort(), "/" + this.extensionIdentifier));
this.preferences = new HashMap<>();
this.preferenceTypes = new HashMap<>();
this.preferenceDefaults = new HashMap<>();
}
public ProjectSettingStore(Preferences preferenceController, IBurpExtenderCallbacks callbacks,
String domain, String extensionIdentifier) throws MalformedURLException, UnsupportedEncodingException {
this.preferenceController = preferenceController;
this.callbacks = callbacks;
this.httpService = callbacks.getHelpers().buildHttpService(domain, 65535, true);
this.extensionIdentifier = URLEncoder.encode(extensionIdentifier, "UTF-8");
this.requestBytes = callbacks.getHelpers().buildHttpRequest(
new URL(httpService.getProtocol(), httpService.getHost(), httpService.getPort(), "/" + this.extensionIdentifier));
this.preferences = new HashMap<>();
this.preferenceTypes = new HashMap<>();
this.preferenceDefaults = new HashMap<>();
}
public void registerSetting(String settingName, Type type) {
this.registerSetting(settingName, type, null);
}
public void registerSetting(String settingName, Type type, Object defaultValue) {
if(this.preferenceTypes.containsKey(settingName)){
throw new RuntimeException("Setting " + settingName + " has already been registered in the project settings store!");
}
this.preferenceTypes.put(settingName, type);
if(this.preferences.get(settingName) == null){
this.preferences.put(settingName, defaultValue);
}else{
try {
String existingSerializedValue = (String) this.preferences.get(settingName);
Object deserializedValue = this.preferenceController.getGsonProvider()
.getGson().fromJson(existingSerializedValue, type);
this.preferences.put(settingName, deserializedValue);
preferenceController.logOutput("Deserialized existing value.");
} catch (Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
preferenceController.logError(sw.toString());
preferenceController.logError("Could not deserialize the loaded value for setting " +
"\"" + settingName + "\" to type \"" + type + "\". Falling back to the default value.");
this.preferences.put(settingName, defaultValue);
}
}
this.preferenceDefaults.put(settingName, defaultValue);
preferenceController.logOutput("Project setting \"" + settingName + "\" registered with type " + type.getTypeName()
+ " and default value: " + (defaultValue != null ? defaultValue : "null"));
}
void setSetting(String setting, Object value){
this.preferences.put(setting, value);
saveToProject();
}
Object getSetting(String settingName){
return this.preferences.get(settingName);
}
void loadSettingsFromJson(String json){
//Initially load the stored values as key, serialized value pairs.
//When settings are registered, we will get their value and convert into the requested type.
//We can then update the entry with the converted type.
Gson gson = this.preferenceController.getGsonProvider().getGson();
HashMap<String, String> tempPreferences = gson.fromJson(json, new TypeToken<HashMap<String, Object>>(){}.getType());
if(this.preferences == null){
this.preferences = new HashMap<>();
}
if(tempPreferences != null) {
for (String key : tempPreferences.keySet()) {
Object value = tempPreferences.get(key);
this.preferences.put(key, gson.toJson(value));
}
}
if(this.serializedValue != null){
//If we already have a serialized value, overwrite its entries with any from the new one to combine them
HashMap<String, String> currentJson = gson.fromJson(serializedValue, new TypeToken<HashMap<String, String>>(){}.getType());
currentJson.putAll(tempPreferences);
json = gson.toJson(currentJson);
}
this.serializedValue = json;
}
void loadFromSiteMap(){
//Load existing from sitemap
IHttpRequestResponse[] existingItems = callbacks.getSiteMap(
this.httpService.toString() + "/" + extensionIdentifier);
//If we have an existing item
if(existingItems.length != 0){
//Pick the first one
IHttpRequestResponse existingSettings = existingItems[0];
//If it has a response body (settings json)
if(existingSettings.getResponse() != null){
//Load it into our current store item.
loadSettingsFromJson(new String(existingSettings.getResponse()));
}
}
}
public void resetSetting(String settingName){
Gson gson = this.preferenceController.getGsonProvider().getGson();
Object defaultValue = this.preferenceDefaults.getOrDefault(settingName, null);
String jsonDefaultValue = gson.toJson(defaultValue);
Object newInstance = gson.fromJson(jsonDefaultValue, this.preferenceTypes.get(settingName));
setSetting(settingName, newInstance);
}
public void saveToProject(){
this.serializedValue = this.preferenceController.getGsonProvider().getGson().toJson(this.preferences);
this.callbacks.addToSiteMap(this);
}
@Override
public byte[] getRequest() {
return this.requestBytes;
}
@Override
public void setRequest(byte[] message) {}
@Override
public byte[] getResponse() {
if(serializedValue == null) return "".getBytes();
return serializedValue.getBytes();
}
@Override
public void setResponse(byte[] message) {
if(message == null){
this.serializedValue = null;
}else {
//Parse the value and load the setting elements.
loadSettingsFromJson(new String(message));
}
}
@Override
public String getComment() {
return null;
}
@Override
public void setComment(String comment) {}
@Override
public String getHighlight() {
return null;
}
@Override
public void setHighlight(String color) {}
@Override
public IHttpService getHttpService() {
return this.httpService;
}
@Override
public void setHttpService(IHttpService httpService) {}
public Type getSettingType(String settingName) {
return this.preferenceTypes.get(settingName);
}
}