-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Invoke-Decode.ps1
70 lines (51 loc) · 1.85 KB
/
Invoke-Decode.ps1
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function Invoke-Decode
{
<#
.SYNOPSIS
Script for Nishang to decode the data encoded by Invoke-Encode, DNS TXT and POST exfiltration methods.
.DESCRIPTION
The script asks for an encoded string as an option, decodes it and writes to a file "decoded.txt" in the current working directory.
Both the encoding and decoding is based on the code by ikarstein.
.PARAMETER EncodedData
The path of the file to be decoded. Use with -IsString to enter a string.
.PARAMETER OutputFilePath
The path of the output file. Default is "decoded.txt" in the current working directory.
.PARAMETER IsString
Use this to specify if you are passing a string ins place of a filepath.
.EXAMPLE
PS > Invoke-Decode -EncodedData C:\files\encoded.txt
.EXAMPLE
PS > Invoke-Decode c08t0Q0oyk9OLS7m5QIA -IsString
Use above to decode a string.
.LINK
http://www.darkoperator.com/blog/2013/3/21/powershell-basics-execution-policy-and-code-signing-part-2.html
https://github.com/samratashok/nishang
#>
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $True)]
[String]
$EncodedData,
[Parameter(Position = 1, Mandatory = $False)]
[String]
$OutputFilePath = ".\decoded.txt",
[Switch]
$IsString
)
if($IsString -eq $true)
{
$data = $EncodedData
}
else
{
$data = Get-Content $EncodedData -Encoding UTF8
}
$dec = [System.Convert]::FromBase64String($data)
$ms = New-Object System.IO.MemoryStream
$ms.Write($dec, 0, $dec.Length)
$ms.Seek(0,0) | Out-Null
$cs = New-Object System.IO.Compression.DeflateStream ($ms, [System.IO.Compression.CompressionMode]::Decompress)
$sr = New-Object System.IO.StreamReader($cs)
$output = $sr.readtoend()
Out-File -InputObject $output -FilePath $OutputFilePath
Write-Host "Decode data written to $OutputFilePath"
}