
Ein minimaler In-Memory-CPU für die agnostische Erstellung von Protokollen on-the-fly
Harald ist eine winzige In-Memory-High-Level-CPU, die eine Reihe von Anweisungen verarbeiten kann, um Application-Layer-Protokolle zu erzeugen, die über ein gegebenes Netzwerkprotokoll (TCP/UDP) verwendet werden können.
Harald konsumiert einen Strom von OPCODES und wendet verschiedene Transformationen auf die Nutzlast an, damit sie für das definierte Application-Layer-Protokoll geeignet ist.
[!TIP] Warum Harald? Vielleicht kennst du Harald Gormsson, König von Dänemark, unter seinem Spitznamen: Blátǫnn, oder wie wir ihn heute aussprechen, Bluetooth. Die Technologie wurde nach Haralds Spitznamen benannt, weil er damals die dänischen und Wikinger-Königreiche verband. Also benenne ich dies nach seinem Vornamen.
Harald verhält sich wie eine CPU. Er folgt einer Reihe von OPCODES, die Aktionen definieren. Ein PROTOKOLL. Harald wird die erforderlichen Operationen ausführen, um das benötigte Protokoll on the fly zu erzeugen.
Sobald das PROTOKOLL bereitgestellt ist, wird Harald über die zu sendende PAYLOAD iterieren, indem er das gesamte PROTOKOLL einmal für jedes Fragment durchläuft. D.h.: Wenn die Nutzlast in mehrere Fragmente aufgeteilt werden musste, verwendet Harald dasselbe Protokoll für jedes Fragment und führt jede im Protokoll definierte Aktion gegen das aktuelle Nutzlastfragment aus.
Harald hält eine Reihe von Registern, die vom Protokoll zum Speichern von Werten verwendet werden. Diese Register können verwendet werden, um verschiedene Operationen durchzuführen, wie Addition, Subtraktion, Kodierung und Dekodierung ... usw.
Harald weist dynamisch ein Array von Ergebnissen zu, in dem das Protokoll die Antworten auf jede Anfrage speichern kann, die beim Senden eines Nutzlastfragments ausgeführt wird.

Harald unterstützt eine Vielzahl von Opcodes, die als Protokoll bereitgestellt werden können. Harald führt eine Reihe von High-Level-Operationen aus, wenn er einen gegebenen Opcode empfängt. Jeder Opcode erwartet eine vordefinierte Menge von Argumenten mit einer vordefinierten Länge.
| OPCODE::Nº | Description | ARGS::Size |
|---|---|---|
| H_F_APPEND::0x01 | Appends the following N Bytes to the end payload | Size::4, NBytes::N |
| H_F_PREPEND::0x02 | Prepends the following N Bytes to the end payload | Size::4, NBytes::N |
| H_F_SPLIT::0x03 | Splits the Payload in fragments of the given size | Size::4 |
| H_F_PAYLOAD_INJECT::0x04 | Injects the Payload fragment into the End Payload | - |
| H_F_SOCK_INIT::0x05 | Initializes the socket connection | IsIPAddress:1, SSL_Enabled::1, TCP/UDP::1, [(HostLen::4,HostName:N)/IPAddress::4], Port::2 |
| H_F_SEND::0x06 | Sends the constructed End Payload | - |
| H_F_RECV::0x07 | Receives up to N Bytes | Size::4 |
| H_F_CLOSE::0x08 | Closes the socket and cleans | - |
| H_F_INJECT::0x09 | Injects the requested registry into the End Payload | Reg::1 |
| H_F_ENCODE_STR::0x0A | Encodes a Payload field to its string representation | IsReg::1, [(Payload::1, Field::1)/Reg::1], Format::1 |
| H_F_DECODE_STR::0xA0 | Decodes the contents of a registry to its numeric representation | Reg::1 |
| H_F_ENCODE_STRB64::0x0B | Encodes a Payload field to its B64string representation | Payload::1, Field::1 |
| H_F_COMPUTE_FRAG_LENGTH::0x0D | Calculates the next fragment to be sent length | - |
| H_F_SEARCH::0x0E | Searches for a given byte pattern of a given N size in a given registry | Reg::1, Size::4, NBytes::N |
| H_F_SEEK::0x0F | Moves the cursor in a given payload by a given number of bytes | Payload::1, Offset::4 |
| H_F_PUSH::0x10 | Pushes a number of bytes or the contents of a registry into another registry | IsReg::1, [Size::4, NBytes::N]/SrcReg::1, DstReg::2 |
| H_F_ADD::0x12 | Adds two registries or a number to a registry | IsReg::1, [Size::4, NBytes::N]/SrcReg::1, DstReg::2 |
| H_F_SUBSTRACT::0x13 | Substracts two registries or a number to a registry | IsReg::1, [Size::4, NBytes::N]/SrcReg::1, DstReg::2 |
| H_F_READ::0x14 | Reads from a given registry up to the length stored in another registry | FromReg::1, SizeReg::1 |
| H_F_STORE::0x15 | Stores a registry value into Harald's Storage | Reg::1 |
Der folgende Opcode-Stream erzeugt die erforderlichen Daten, die gesendet werden müssen, damit eine Kommunikation als HTTP verstanden wird. Er wird initiieren, die Nutzlast so verändern, dass sie wie HTTP-Verkehr aussieht, sie über eine SSL/TLS-Verbindung senden und die Verbindung am Ende schließen.
H_F_SPLIT, 0x00, 0x00, 0x00, 0xFF, // Splits the Payload
H_F_APPEND, 0x00, 0x00, 0x00, 0x46, // Appends...
'P', 'O', 'S', 'T', ' ', '/', ' ', // POST /
'H', 'T', 'T', 'P', '/', '1', '.', '1', '\r', '\n', // HTTP/1.1
'H', 'o', 's', 't', ':', ' ', // Host:
'd', 'o', 'm', 'a', 'i', 'n', '.', 'c', 'o', 'm', '\r', '\n', // domain.com
'C', 'o', 'n', 'n', 'e', 'c', 't', 'i', 'o', 'n', ':', ' ', // Connection:
'c', 'l', 'o', 's', 'e', '\r', '\n', // close
'C', 'o', 'n', 't', 'e', 'n', 't', '-', // Content-
'L', 'e', 'n', 'g', 't', 'h', ':', ' ', // Length:
H_F_COMPUTE_FRAG_LENGTH, // Next Fragment's size
H_F_ENCODE_STR, H_PAY_END_PAYLOAD, H_PAY_FRAGMENT_SIZE, 'u', // FragSize encode
H_F_INJECT, H_REG_RET, // Inject the Fragment Size encoded str
H_F_APPEND, 0x00, 0x00, 0x00, 0x04, '\r', '\n', '\r', '\n', // Append Body begin
H_F_PAYLOAD_INJECT, // Inject the Payload
H_F_SOCK_INIT, 0x01, 0x01, 0x7F, 0x00, 0x00, 0x01, 0x04, 0xD2, // Init Socket
H_F_SEND, // Send the Payload over the socket
H_F_CLOSE // Close the socket and cleanup