-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathBincoder GUI.ps1
372 lines (317 loc) · 14.7 KB
/
Bincoder GUI.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#requires -version 5
<#
.SYNOPSIS
Base64 encode/decode clipboard contents to/from specified file either via GUI or command line options
.DESCRIPTION
Useful to transfer binary files to/from a remote Citrix or RDSH session when client drive mapping, cloud file services, email attachments, etc are blocked
.PARAMETER filename
The name of the file to encode the contents of if -decode not specified or the name of the file to write the decoded content to if -decode is used
.PARAMETER decode
Take the base64 encoded contents of the clipboard, decode it and write to the file specified in -filename otherwise base64 encode the contents of the file specified by -filename
.PARAMETER nochecksum
Do not compute and display a checksum of the file
.EXAMPLE
& '.\Bincoder GUI.ps1'
Run the script so a GUI is displayed to allow selection of the file and then press the "encode" button on one system and then the "decode" button on the other system
.EXAMPLE
& '.\Bincoder GUI.ps1' -filename c:\temp\tools.zip -encode
Base64 encode the contents of the file c:\temp\tools.zip and place on the windows clipboard
.EXAMPLE
& '.\Bincoder GUI.ps1' -filename c:\temp\tools.zip -decode
Take the contents of the windows clipboard, from a prior invocation of this script without -encode, decode the base64 text and write to the file c:\temp\tools.zip
.NOTES
Modification history:
30/11/19 @guyrleech First public release
#>
[CmdletBinding()]
Param
(
[string]$filename ,
[switch]$decode ,
[switch]$noChecksum
)
[string]$mainwindowXAML = @'
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Bincoder_GUI"
mc:Ignorable="d"
Title="Base64 Encoder/Decoder via Clipboard" Height="235.385" Width="616.538">
<Grid Margin="0,0,22.5,88.269">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="37*"/>
<ColumnDefinition Width="96*"/>
<ColumnDefinition Width="454*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="txtFileName" HorizontalAlignment="Left" Height="36.346" Margin="27.346,24.923,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="429.231" Grid.Column="1" Grid.ColumnSpan="2"/>
<Label Content="File" HorizontalAlignment="Left" Height="36.346" Margin="23.846,24.923,0,0" VerticalAlignment="Top" Width="53.5" Grid.ColumnSpan="2"/>
<Button x:Name="btnEncode" Content="_Encode" HorizontalAlignment="Left" Height="36.347" Margin="27.346,71.203,0,0" VerticalAlignment="Top" Width="166.154" Grid.Column="1" Grid.ColumnSpan="2"/>
<Button x:Name="btnDecode" Content="_Decode" HorizontalAlignment="Left" Height="36.347" Margin="137.922,71.203,0,0" VerticalAlignment="Top" Width="166.154" Grid.Column="2"/>
<Button x:Name="btnFileChooser" Content="..." HorizontalAlignment="Left" Height="36.346" Margin="365.846,24.923,0,0" VerticalAlignment="Top" Width="57.308" Grid.Column="2"/>
</Grid>
</Window>
'@
Function Load-GUI
{
Param
(
[Parameter(Mandatory=$true)]
$inputXaml
)
$form = $null
$inputXML = $inputXaml -replace 'mc:Ignorable="d"' , '' -replace 'x:N' ,'N' -replace '^<Win.*' , '<Window'
[xml]$xaml = $inputXML
if( $xaml )
{
$reader = New-Object -TypeName Xml.XmlNodeReader -ArgumentList $xaml
try
{
$form = [Windows.Markup.XamlReader]::Load( $reader )
}
catch
{
Throw "Unable to load Windows.Markup.XamlReader. Double-check syntax and ensure .NET is installed.`n$_"
}
$xaml.SelectNodes( '//*[@Name]' ) | ForEach-Object `
{
Set-Variable -Name "WPF$($_.Name)" -Value $Form.FindName($_.Name) -Scope Global
}
}
else
{
Throw "Failed to convert input XAML to WPF XML"
}
$form
}
Function Invoke-Operation
{
[CmdletBinding()]
Param
(
[string]$filename ,
[switch]$decode ,
$form
)
if( $decode )
{
## if filename is not absolute then make it so
if( $filename -notmatch '^\\\\' -and $filename -notmatch '^[a-z]:\\' )
{
$filename = Join-Path -Path (Get-Location -PSProvider FileSystem | Select-Object -ExpandProperty Path) -ChildPath $filename
Write-Verbose -Message "File name is now `"$filename`""
}
if( ! ( Test-Path -Path $filename -ErrorAction SilentlyContinue ) `
-or ( $form -and [Windows.MessageBox]::Show( "`"$filename`" already exists. Overwrite?" , 'Decoding Error' , 'YesNo' ,'Exclamation' ) -eq 'Yes' ) )
{
if( $form )
{
$oldCursor = $form.Cursor
$form.Cursor = [Windows.Input.Cursors]::Wait
}
[string]$clipboard = Get-Clipboard -Format Text -TextFormatType Text -Raw
[byte[]]$transmogrified = [System.Convert]::FromBase64String( $clipboard )
[string]$message = $null
if( $transmogrified.Count )
{
$fileStream = New-Object System.IO.FileStream( $filename , [System.IO.FileMode]::Create , [System.IO.FileAccess]::Write )
if( $fileStream )
{
$fileStream.Write( $transmogrified , 0 , $transmogrified.Count )
$fileStream.Close()
if( ! $noChecksum )
{
$hash = Get-FileHash -Path $filename
if( $hash )
{
$message = "`n$($hash.Algorithm) checksum $($hash.hash)"
}
}
}
elseif( $form )
{
[void][Windows.MessageBox]::Show( "Failed to create file `"$filename`"" , 'Decoding Error' , 'Ok' , 'Error' )
}
}
else
{
if( $form )
{
[void][Windows.MessageBox]::Show( "Failed to decode clipboard contents ($([int]($clipboard.Length / 1KB))KB)" , 'Decoding Error' , 'Ok' , 'Error' )
}
}
$transmogrified = $null
$clipboard = $null
$fileProperties = Get-ItemProperty -Path $filename
[int]$bytesWritten = 0
if( $fileProperties )
{
$bytesWritten = $fileProperties.Length
}
$message = "Wrote $([int]($bytesWritten / 1KB))KB to $filename" + $message
Write-Verbose -Message $message
if( $form )
{
$form.Cursor = $oldCursor
[void][Windows.MessageBox]::Show( $message , 'Decoding Complete' , 'Ok' , $(if( $bytesWritten ) { 'Information' } else { 'Error' } ) )
}
}
}
else ## encode
{
[byte[]]$data = [System.IO.File]::ReadAllBytes( $filename )
if( $data -and $data.Count )
{
[System.Convert]::ToBase64String( $data ) | Set-Clipboard
[string]$message = "$([int]($data.Count / 1KB))KB from `"$filename`" encoded and put on clipboard"
if( ! $noChecksum )
{
$hash = Get-FileHash -Path $filename
$message += "`n$($hash.Algorithm) checksum $($hash.hash)"
}
Write-Verbose -Message $message
if( $form )
{
[void][Windows.MessageBox]::Show( $message , 'Encoding Complete' , 'Ok' ,'Information' )
}
}
else
{
if( $form )
{
[void][Windows.MessageBox]::Show( "No data read from `"$($WPFtxtFileName.Text)`"" , 'Encoding Error' , 'Ok' ,'Exclamation' )
}
else
{
Write-Error -Message "No data read from `"$($WPFtxtFileName.Text)`""
}
}
$data = $null
}
}
Add-Type -AssemblyName System.Windows.Forms
$mainForm = $null
if( $PSBoundParameters[ 'filename' ] )
{
Invoke-Operation -filename $filename -decode:$decode
}
else
{
Add-Type -AssemblyName Presentationframework
$mainForm = Load-GUI -inputXaml $mainwindowXAML
if( ! $mainForm )
{
return
}
$mainForm.Title += " on $env:COMPUTERNAME"
$WPFbtnEncode.Add_Click({
$_.Handled = $true
if( [string]::IsNullOrEmpty( $WPFtxtFileName.Text ) )
{
[void][Windows.MessageBox]::Show( "Must specify source file name" , 'Encoding Error' , 'Ok' ,'Exclamation' )
}
else
{
Invoke-Operation -filename $WPFtxtFileName.Text -form $mainForm
}
})
$WPFbtnDecode.Add_Click({
$_.Handled = $true
if( [string]::IsNullOrEmpty( $WPFtxtFileName.Text ) )
{
[void][Windows.MessageBox]::Show( "Must specify destination file name" , 'Decoding Error' , 'Ok' ,'Exclamation' )
}
else
{
Invoke-Operation -filename $WPFtxtFileName.Text -decode -form $mainForm
}
})
$WPFbtnFileChooser.Add_Click({
$_.Handled = $true
$fileBrowser = New-Object -TypeName System.Windows.Forms.OpenFileDialog
if( $fileBrowser )
{
$fileBrowser.InitialDirectory = Get-Location -PSProvider FileSystem | Select-Object -ExpandProperty Path
$file = $fileBrowser.ShowDialog()
if( $file -eq 'OK' )
{
$WPFtxtFileName.Text = $fileBrowser.FileName
}
}
})
$result = $mainForm.ShowDialog()
}
# SIG # Begin signature block
# MIINKgYJKoZIhvcNAQcCoIINGzCCDRcCAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUTHMa4JASthihvhvU5zzs3FmM
# qF6gggpsMIIFMDCCBBigAwIBAgIQBAkYG1/Vu2Z1U0O1b5VQCDANBgkqhkiG9w0B
# AQsFADBlMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYD
# VQQLExB3d3cuZGlnaWNlcnQ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