|
| 1 | +import std/[sequtils,strformat,strutils, sugar] |
| 2 | + |
| 3 | + |
| 4 | +proc toEvenHex(bytes: seq[uint8], sizeNeeded: int8): seq[string] = |
| 5 | + let |
| 6 | + hex = bytes.toSeq.mapIt(it.toHex.toLower) |
| 7 | + remainder = sizeNeeded - hex.len.mod(sizeNeeded) |
| 8 | + padding = @["90"].cycle(remainder) |
| 9 | + writeLine(stderr, &"[*] Shellcode is {hex.len} bytes, adding {remainder} NOPs to make UUIDs") |
| 10 | + |
| 11 | + return concat(padding, hex) # add NOP sled to make the sequence evenly devisible |
| 12 | + |
| 13 | + |
| 14 | +proc toUUIDs*(bytes: seq[uint8]): seq[string] = |
| 15 | + let hex = bytes.toEvenHex(16) |
| 16 | + result = collect: |
| 17 | + for i in countup(0, hex.len-16, 16): |
| 18 | + let uuidGroups = hex[i ..< i+16].distribute(4) |
| 19 | + &"{uuidGroups[0].join}-{uuidGroups[1].join}-{uuidGroups[2].join}{uuidGroups[3].join}" |
| 20 | + |
| 21 | +proc fromUUID*(uuid: string): seq[uint8] = |
| 22 | + let bytes = uuid |
| 23 | + .filterIt(it != '-') |
| 24 | + .distribute(16) |
| 25 | + .mapIt(fromHex[uint8](join(it))) |
| 26 | + result = collect(newSeq): |
| 27 | + for b in bytes: b |
| 28 | + |
| 29 | +proc toMAC*(bytes: seq[uint8]): seq[string] = |
| 30 | + let hex = bytes.toEvenHex(8) |
| 31 | + for i in countup(0, hex.len-8, 8): |
| 32 | + let mac = hex[i ..< i+8].join("-") |
| 33 | + result.add(mac) |
| 34 | + |
| 35 | +proc fromMAC*(mac: string): seq[uint8] = |
| 36 | + result = mac.split(".").mapIt(cast[uint8](it.parseUInt)) |
| 37 | + |
| 38 | +proc toIPv6*(bytes: seq[uint8]): seq[string] = |
| 39 | + let hex = bytes.toEvenHex(16) |
| 40 | + result = collect: |
| 41 | + for i in countup(0, hex.len-16, 16): |
| 42 | + let uuidGroups = hex[i ..< i+16].distribute(4) |
| 43 | + join(uuidGroups.mapIt(join(it)),":") |
| 44 | + #let uuid = &"{uuidGroups[0].join(":")}:{uuidGroups[1].join(":")}:{uuidGroups[2].join(":")}{uuidGroups[3].join(":")}" |
| 45 | + |
| 46 | +proc fromIPv6*(ipv6: string): seq[uint8] = |
| 47 | + let bytes = ipv6 |
| 48 | + .filterIt(it != ':') |
| 49 | + .distribute(16) |
| 50 | + .mapIt(fromHex[uint8](join(it))) |
| 51 | + result = collect: |
| 52 | + for b in bytes: b |
| 53 | + |
| 54 | +proc toIPv4*(bytes: seq[uint8]): seq[string] = |
| 55 | + let |
| 56 | + remainder = 4 - bytes.len.mod(4) |
| 57 | + buffer = @[144'u8].cycle(remainder) # 144d = 90h (NOP) |
| 58 | + alignedBytes = concat(buffer, bytes) |
| 59 | + for i in countup(0, alignedBytes.len-4, 4): |
| 60 | + result.add(alignedBytes[i ..< i+4].join(".")) |
| 61 | + |
| 62 | +proc fromIPv4*(ipv4: string): seq[uint8] = |
| 63 | + result = ipv4.split(".").mapIt(cast[uint8](it.parseUInt)) |
0 commit comments