-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathencryption.nim
More file actions
37 lines (31 loc) · 1.21 KB
/
Copy pathencryption.nim
File metadata and controls
37 lines (31 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
{.push raises: [].}
import std/random
import winim
type
USTRING = object
Length, MaximumLength: DWORD
Buffer: PVOID
proc makeRandomBytes(i: int): seq[uint8] =
for k in 0 .. i:
result.add(rand(255).uint8)
proc rc4EncryptNTAPI(data, key: PTR): NTSTATUS
{.discardable, stdcall, dynlib: "Advapi32", importc: "SystemFunction032"}
proc rc4Encrypt*(key, payload: seq[uint8], winapi: bool): string {.raises: [OSError].} =
if winapi:
var
winPayload = payload # need a deep copy because of in-place memory modification
key = USTRING(
Length: cast[int32](key.len),
MaximumLength: cast[int32](key.len),
Buffer: key.addr # can't use `ptr` because it is a generic
)
data = USTRING(
Length: cast[int32](winPayload.len),
MaximumLength: cast[int32](winPayload.len),
Buffer: winPayload.addr
)
status = rc4EncryptNTAPI(data.addr, key.addr)
if not status.NT_SUCCESS:
echo &"[-] Encryption failed with code: {status}"
raise newException(OSError, "Encryption failed using NTAPI")
result = $data