Skip to content

Commit

Permalink
use UnicodeUnescaper().translate() instead of unsecapeJava, use jacks…
Browse files Browse the repository at this point in the history
…on instead of gson
  • Loading branch information
godfuzz3r committed Sep 5, 2023
1 parent 7ae7549 commit fb85891
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 34 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.godfuzz3r.burp</groupId>
<artifactId>ununicode</artifactId>
<version>1.0</version>
<version>1.1</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
Expand Down Expand Up @@ -60,11 +60,11 @@
<version>[1.10.0,)</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>[2.10.1,)</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
</dependencies>
</project>
59 changes: 59 additions & 0 deletions src/UnUnicode/BurpPrettyPrinter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package UnUnicode;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.core.util.Separators;

import java.io.IOException;


public class BurpPrettyPrinter extends DefaultPrettyPrinter {

public BurpPrettyPrinter() {
_arrayIndenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
_objectIndenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
}

public BurpPrettyPrinter(DefaultPrettyPrinter base) {
super(base);
}

@Override
public BurpPrettyPrinter createInstance() {
if (getClass() != BurpPrettyPrinter.class) {
throw new IllegalStateException("Failed `createInstance()`: " + getClass().getName()
+ " does not override method; it has to");
}
return new BurpPrettyPrinter(this);
}

@Override
public BurpPrettyPrinter withSeparators(Separators separators) {
this._separators = separators;
this._objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSeparator() + "";
return this;
}

@Override
public void writeEndArray(JsonGenerator g, int nrOfValues) throws IOException {
if (!_arrayIndenter.isInline()) {
--_nesting;
}
if (nrOfValues > 0) {
_arrayIndenter.writeIndentation(g, _nesting);
}
g.writeRaw(']');
}

@Override
public void writeEndObject(JsonGenerator g, int nrOfEntries) throws IOException {
if (!_objectIndenter.isInline()) {
--_nesting;
}
if (nrOfEntries > 0) {
_objectIndenter.writeIndentation(g, _nesting);
}
g.writeRaw('}');
}
}
52 changes: 23 additions & 29 deletions src/UnUnicode/UnUnicode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@
import java.awt.Component;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.apache.commons.text.StringEscapeUtils;

import burp.IBurpExtenderCallbacks;
import burp.IExtensionHelpers;
import burp.IMessageEditorController;
import burp.IMessageEditorTab;
import burp.IRequestInfo;
import burp.ITextEditor;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import org.apache.commons.text.translate.UnicodeUnescaper;

public class UnUnicode implements IMessageEditorTab{
private ITextEditor txtInput;
private JPanel panel = new JPanel(new BorderLayout(0, 0));

private static IBurpExtenderCallbacks callbacks;
private static IExtensionHelpers helpers;

public byte[] concatHttp(byte[] headers, byte[] content) throws IOException {
Expand All @@ -37,25 +37,14 @@ public byte[] concatHttp(byte[] headers, byte[] content) throws IOException {
return outputStream.toByteArray();
}


public static boolean isJson(String content) {
Gson gson = new Gson();
try {
gson.fromJson(content, Object.class);
Object jsonObjType = gson.fromJson(content, Object.class).getClass();
if(jsonObjType.equals(String.class)){
return false;
}
return true;
} catch (com.google.gson.JsonSyntaxException ex) {
return false;
}
}

public String prettifyJson(String json) {
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().serializeNulls().create();
JsonElement je = JsonParser.parseString(json);
return gson.toJson(je);
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
try{
Object jsonObject = mapper.readValue(json, Object.class);
return mapper.writer(new BurpPrettyPrinter()).writeValueAsString(jsonObject);
}catch (Exception e) {
return json;
}
}

public UnUnicode(IMessageEditorController controller, boolean editable, IExtensionHelpers helpers, IBurpExtenderCallbacks callbacks)
Expand All @@ -67,6 +56,7 @@ public UnUnicode(IMessageEditorController controller, boolean editable, IExtensi
panel.add(txtInput.getComponent(), BorderLayout.CENTER);
callbacks.customizeUiComponent(panel);
UnUnicode.helpers = helpers;
UnUnicode.callbacks = callbacks;
}

@Override
Expand Down Expand Up @@ -97,15 +87,19 @@ public void setMessage(byte[] content, boolean isRequest)
byte[] headers = Arrays.copyOfRange(content, 0, bodyOffset);
byte[] body = Arrays.copyOfRange(content, bodyOffset, content.length);

String unescaped = StringEscapeUtils.unescapeJava(new String(body));
if (isJson(unescaped)){
unescaped = prettifyJson(unescaped);
}
// if not json, just returns old string
String prettified = prettifyJson(new String(body));

// do all the magic
String unescaped = new UnicodeUnescaper().translate(prettified);

try{
byte[] out = concatHttp(headers, unescaped.getBytes());
txtInput.setText(out);
} catch (Exception e) {
e.printStackTrace();
StringWriter errorWriter = new StringWriter();
e.printStackTrace(new PrintWriter(errorWriter));
callbacks.printError(errorWriter.toString());
}
}

Expand Down

0 comments on commit fb85891

Please sign in to comment.