Skip to content

Commit

Permalink
adding a generic formater and fix string repeat bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Summitt committed Jul 6, 2023
1 parent d17d573 commit 2852453
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
31 changes: 28 additions & 3 deletions NonHTTPProxy/src/josh/nonHttp/PythonMangler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Properties;


import org.python.core.PyBoolean;
import org.python.core.PyByteArray;
import org.python.core.PyObject;
Expand All @@ -25,6 +26,7 @@
import josh.utils.events.PythonOutputEvent;
import josh.utils.events.PythonOutputEventListener;


public class PythonMangler {
private String pyCode;
private PythonInterpreter interpreter;
Expand Down Expand Up @@ -168,7 +170,30 @@ public String setPyCode(String code){
return this.pyCode;

}
public byte [] preIntercept(byte [] input, boolean isC2S){
public byte [] formatOnly(byte [] input, boolean isC2S){

byte[]original = input;
try{
PyObject someFunc = interpreter.get("formatOnly");

//this means that the pre Intercept feature has not been implemented.
if(someFunc == null)
return input;
PyObject result = someFunc.__call__(new PyByteArray(input), new PyBoolean(isC2S));
PyByteArray array = (PyByteArray) result.__tojava__(Object.class);

byte[] out = new byte [array.__len__()];
for(int i=0; i < array.__len__(); i++){
out[i] = (byte)array.get(i).__tojava__(Byte.class);
}

return out;
}catch(Exception ex){
System.out.println(ex);
return original;
}
}
public byte [] preIntercept(byte [] input, boolean isC2S){

byte[]original = input;
try{
Expand All @@ -187,7 +212,7 @@ public String setPyCode(String code){

return out;
}catch(Exception ex){
ex.printStackTrace();
System.out.println(ex);
return original;
}
}
Expand All @@ -208,7 +233,7 @@ public String setPyCode(String code){
}
return out;
}catch(Exception ex){
ex.printStackTrace();
System.out.println(ex);
return original;
}

Expand Down
41 changes: 30 additions & 11 deletions NonHTTPProxy/src/josh/nonHttp/SendData.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLSocket;


import org.xbill.DNS.A6Record;

import josh.nonHttp.events.ProxyEvent;
Expand All @@ -37,6 +38,7 @@
import josh.utils.events.SendClosedEvent;
import josh.utils.events.SendClosedEventListener;


public class SendData implements Runnable {
public InputStream in;
public DataOutputStream out;
Expand Down Expand Up @@ -273,10 +275,13 @@ public void run() {
tmp = pm.mangle(tmp, isC2S);
SendPyOutput(pm);
// Check if we updated the data
if (!Arrays.equals(tmp, original))
this.Name = this.Name + " - Updated by Python (mangle)";
else
if (!Arrays.equals(tmp, original)){
if(!this.Name.contains("mangle")){
this.Name = this.Name + " - Updated by Python (mangle)";
}
}else{
this.Name = this.Name.replace(" - Updated by Python (mangle)", "");
}

} else {
List<String> mtch = regexMatch();
Expand Down Expand Up @@ -316,9 +321,11 @@ public void run() {
tmp = this.replace(tmp, match, replace);
}
}
if (!Arrays.equals(tmp, original))
this.Name = this.Name + " - Updated by Match And Replace Rules";
else
if (!Arrays.equals(tmp, original)){
if(!this.Name.contains("Match")){
this.Name = this.Name + " - Updated by Match And Replace Rules";
}
}else
this.Name = this.Name.replace(" - Updated by Match And Replace Rules", "");
}
}
Expand All @@ -334,9 +341,11 @@ public void run() {
SendPyOutput(pm);
}
//TODO: This logic is not totally right. The data could be altered by other tools before this step.
if (!Arrays.equals(tmp, original))
this.Name = this.Name + " - Formated by Python";
else
if (!Arrays.equals(tmp, original)){
if(!this.Name.contains("Formated")){
this.Name = this.Name + " - Formated by Python";
}
}else
this.Name = this.Name.replace(" - Formated by Python", "");
// This will block until the the request if forwarded by the user from the
// interceptor
Expand All @@ -357,11 +366,21 @@ public void run() {

} else {
// Data was not manually intercepted so we treat it like a normal event.
NewDataEvent(tmp, original, this.Name);
if (SERVER.isPythonOn()) {
byte [] updated = pm.formatOnly(original, isC2S);
NewDataEvent(updated, original, this.Name);
}else{
NewDataEvent(tmp, original, this.Name);
}
}
} else {
// Manual Intercepts was not enabled so we treat it like a normal event.
NewDataEvent(tmp, original, this.Name);
if (SERVER.isPythonOn()) {
byte [] updated = pm.formatOnly(original, isC2S);
NewDataEvent(updated, original, this.Name);
}else{
NewDataEvent(tmp, original, this.Name);
}
}

// Write the data back to the socket
Expand Down

0 comments on commit 2852453

Please sign in to comment.