Skip to content

Commit

Permalink
feat: added result of decode ops in logs
Browse files Browse the repository at this point in the history
Renamed "edits" in "edit operations"
  • Loading branch information
mattebit committed Dec 19, 2023
1 parent a7273b6 commit f1b31db
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 6 deletions.
14 changes: 13 additions & 1 deletion doc/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ By using an Edit Operation inside an Operation, you are able to edit the interce
>Note: Save from message is not possible in edit, is should be done in message operations
example:

```json
"edit operations": [
{
"from": "url",
"edit": "client_id",
"value": "fakeclientid"
}
]
```

### Using Edit Operation in Decode Operations

It is possible to edit the decoded content of decode Ops by using edit Operations. A list of edit Operations has to be specified. When using Edit Op. inside Decode Operations, depending on the `type` specified in Decode Operations, you can use different keys.
Expand Down Expand Up @@ -1014,4 +1026,4 @@ Examples: <br>
- Added encode option to edit operation
- Removed "remove match word" from edit operation, just use edit regex with empty substitution
- Added decode regex in Decode Operations
- Added json schema validation in checks for jwts
- Added json schema validation in checks for jwts
5 changes: 5 additions & 0 deletions tool/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@
<artifactId>json-schema-validator</artifactId>
<version>1.0.87</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.11.0</version>
</dependency>
</dependencies>

<properties>
Expand Down
25 changes: 25 additions & 0 deletions tool/src/main/java/migt/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;
import org.apache.commons.text.StringEscapeUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand Down Expand Up @@ -639,6 +640,30 @@ public String toString() {
return "check: " + what + (op == null ? "" : " " + op + ": " + op_val);
}

public String toStringExtended() {
String template = "Check:\n" +
"\tWhat: %s\n" +
"\tIs it a param check? %b\n" +
"\tregex: %s\n" +
"\tWhere: %s\n" +
"\tOp: %s\n" +
"\tOp val: %s\n" +
"\tValue list: %s\n" +
"\tUse variable: %b\n" +
"\tUrl decode: %b\n";

return String.format(template,
StringEscapeUtils.escapeJava(what),
isParamCheck,
StringEscapeUtils.escapeJava(regex),
in,
op,
op_val,
value_list,
use_variable,
url_decode);
}

protected JsonSchema getJsonSchemaFromStringContent(String schemaContent) {
JsonSchemaFactory factory = JsonSchemaFactory.getInstance(SpecVersion.VersionFlag.V4);
return factory.getSchema(schemaContent);
Expand Down
28 changes: 27 additions & 1 deletion tool/src/main/java/migt/DecodeOperation.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package migt;

import com.jayway.jsonpath.JsonPath;
import org.apache.commons.text.StringEscapeUtils;
import org.json.JSONArray;
import org.json.JSONObject;

Expand Down Expand Up @@ -589,6 +590,31 @@ public boolean executeChecks(List<Var> vars) throws ParsingException {
return true;
}

/**
* Returns an extended string representation of this decode operation
*
* @return the extended string representation of this decode operation
*/
public String toStringExtended() {
String template = "Decode operation:\n" +
"\tfrom: %s\n" +
"\tdecode target: %s\n" +
"\tis regex %b\n" +
"\tencodings: %s\n" +
"\ttype: %s\n" +
"\tdecoded content: %s\n" +
"\tdecode operations: %s\n";


return String.format(template, from,
StringEscapeUtils.escapeJava(decode_target),
is_regex,
encodings,
type,
StringEscapeUtils.escapeJava(decoded_content),
decodeOperations);
}

/**
* Used in decode operation to specify where to search for the content to decode
*/
Expand Down Expand Up @@ -682,4 +708,4 @@ public static DecodeOpType fromString(String input) throws ParsingException {
}
}
}
}
}
2 changes: 1 addition & 1 deletion tool/src/main/java/migt/Operation.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public Operation(JSONObject operation_json,
}

// Edit operations
if (operation_json.has("edits")) {
if (operation_json.has("edit operations")) {
editOperations = Tools.parseEditsFromJSON(operation_json.getJSONArray("edits"));
}
}
Expand Down
18 changes: 16 additions & 2 deletions tool/src/main/java/migt/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public void logTest(String log_folder) {
test_log_content += "Variables: \n";

for (Var v : vars) {
test_log_content += v.name=v.value+"\n";
test_log_content += v.name = v.value + "\n";
}

File test_log = new File(test_log_path);
Expand All @@ -304,10 +304,21 @@ public void logTest(String log_folder) {
e.printStackTrace();
}
}

Integer op_count = 0;
for (Operation o : this.operations) {
Integer message_count = 0;
String header = "===================== Session Info =========================\n";
String header = "===================== Operation Info =========================\n";
header += "Checks: ====================================\n";
for (Check c : o.getChecks()) {
header += c.toStringExtended();
}
header += "Decode Operations: =========================\n";
for (DecodeOperation d : o.getDecodeOperations()) {
header += d.toStringExtended();
}

header += "\n\n";
header += "===================== Message info =========================\n";
header += "=\t" + "Intercepted from session: " + o.from_session + "\n";
header += "=\t" + "Message name: " + o.getMessageType() + "\n";
Expand Down Expand Up @@ -348,6 +359,8 @@ public void logTest(String log_folder) {
}

HashSet<Integer> logged_requests = new HashSet<Integer>();

// Save all messages seen by this operation
if (o.log_messages != null) {
for (IInterceptedProxyMessage m : o.log_messages) {
if (!logged_requests.contains(m.getMessageReference())) {
Expand Down Expand Up @@ -389,6 +402,7 @@ public void logTest(String log_folder) {
}
}

// Save edited message
if (o.processed_message != null) {
File log_message = new File(base_path + "_edited.raw");
try {
Expand Down
2 changes: 1 addition & 1 deletion tool/src/main/java/migt/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public static List<MessageType> readMsgTypesFromJson(String input) throws Parsin
JSONObject obj = null;

try {
obj = new JSONObject(input);
obj = new JSONObject(input);
} catch (JSONException e) {
throw new ParsingException("Invalid JSON in message definition file: " + e);
}
Expand Down
12 changes: 12 additions & 0 deletions tool/src/test/java/Checks_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,4 +380,16 @@ void test_check_json_schema_validation_wrong_schema() throws ParsingException {
}
assertEquals(1,0);
}
@Test
void test_print_extended() throws ParsingException {
String check_str = "{\n" +
" \"in\": \"header\",\n" +
" \"check\": \"$.pageInfo.imaninteger\",\n" +
" \"json schema compliant\": \"wrongschema\" " +
"}";

Check c = initCheck_json(check_str);

System.out.println(c.toStringExtended());
}
}
6 changes: 6 additions & 0 deletions tool/src/test/java/DecodeOperation_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,10 @@ void test_parse_w_edits_save() throws ParsingException {

assertEquals(1, dop.editOperations.size());
}

@Test
void test_print_extended() throws ParsingException {
DecodeOperation dop = new DecodeOperation(new JSONObject(input_w_edits_save));
System.out.println(dop.toStringExtended());
}
}

0 comments on commit f1b31db

Please sign in to comment.