-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPSmRemoteNG.psm1
821 lines (558 loc) · 26.4 KB
/
PSmRemoteNG.psm1
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
# verify that we are running in Windows PowerShell or 32-bit PowerShell Core
if ( $PSEdition -eq 'Core' -and [System.Environment]::Is64BitProcess ) {
throw 'This module requires Windows PowerShell or the 32-bit version of PowerShell Core.'
}
# find mRemoteNG install location
$mRNGDirectory = 'HKLM:\SOFTWARE\mRemoteNG', 'HKLM:\SOFTWARE\WOW6432Node\mRemoteNG' |
ForEach-Object { Get-ItemProperty -Path $_ -Name InstallDir -ErrorAction SilentlyContinue } |
Select-Object -First 1 -ExpandProperty InstallDir
# check for supported version of mRemoteNG
try {
$MRNGVersion = [System.Diagnostics.FileVersionInfo]::GetVersionInfo( "$mRNGDirectory\mRemoteNG.exe" ).FileVersionRaw
} catch {
throw ( 'Failed to load mRemoteNG version. {0}' -f $_.Exception.Message )
}
[version] $FirstUnsupportedVersion = '1.77'
if ( $MRNGVersion -ge $FirstUnsupportedVersion ) {
throw ( 'The installed version of mRemoteNG must be less than v{0}.' -f $FirstUnsupportedVersion )
}
# load assemblies
try {
[void][System.Reflection.Assembly]::LoadFile( "$mRNGDirectory\mRemoteNG.exe" )
[void][System.Reflection.Assembly]::LoadFile( "$mRNGDirectory\BouncyCastle.Crypto.dll" )
} catch {
throw $Messages.AssemblyLoadError
}
<#
.SYNOPSIS
Import an mRemoteNG root connections node from a confCons.xml file.
.PARAMETER EncryptionKey
The encryption key for the confCons.xml file.
.EXAMPLE
$RootNode = Import-MRNGRootNode -Path .\confCons.xml -EncryptionKey ( Read-Host 'Encryiption Key' -AsSecureString )
#>
function Import-MRNGRootNode {
[OutputType([mRemoteNG.Tree.Root.RootNodeInfo])]
param(
[Parameter(Mandatory)]
[System.IO.FileInfo]
$Path,
[securestring]
$EncryptionKey = ( ConvertTo-SecureString -String 'mR3m' -AsPlainText -Force )
)
# resolve the path
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath( $Path )
# load the XML
$DataProvider = [mRemoteNG.Config.DataProviders.FileDataProvider]::new( $Path )
$XmlString = $DataProvider.Load()
# create a deserializer
if ( 'mRemoteNG.Config.Serializers.Xml.XmlConnectionsDeserializer' -as [type] ) {
[Func[mRemoteNG.Tools.Optional[securestring]]]$Auth = { $EncryptionKey }
$Deserializer = [mRemoteNG.Config.Serializers.Xml.XmlConnectionsDeserializer]::new($Auth)
return ( $Deserializer.Deserialize( $XmlString ) ).RootNodes.Item(0)
} else {
[Func[securestring]]$Auth = { $EncryptionKey }
$Deserializer = [mRemoteNG.Config.Serializers.XmlConnectionsDeserializer]::new($XmlString, $Auth)
return ( $Deserializer.Deserialize() ).RootNodes.Item(0)
}
}
<#
.SYNOPSIS
Create an empty mRemoteNG root connections node.
.PARAMETER EncryptionKey
The encryption key to use for this connections file.
.EXAMPLE
$RootNode = New-MRNGRootNode
#>
function New-MRNGRootNode {
[OutputType([mRemoteNG.Tree.Root.RootNodeInfo])]
param(
[securestring]
$EncryptionKey = ( ConvertTo-SecureString -String 'mR3m' -AsPlainText -Force )
)
# convert the encryption key to plaintext
$EncryptionKeyBSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR( $EncryptionKey )
$EncryptionKeyText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto( $EncryptionKeyBSTR )
$RootNode = [mRemoteNG.Tree.Root.RootNodeInfo]::new('Connection')
# set a password?
if ( $EncryptionKeyText -ne $RootNode.DefaultPassword ) {
$RootNode.Password = $true
$RootNode.PasswordString = $EncryptionKeyText
}
$RootNode
}
<#
.SYNOPSIS
Create an mRemoteNG container node.
.EXAMPLE
New-MRNGContainer -Name 'Test Container' -Parent $RootNode -Protocol SSH2
.EXAMPLE
$Container = New-MRNGContainer -Name 'Test Container'
$RootContainer.AddChild( $Container )
$Container2 = New-MRNGContainer -Name 'Child Container'
$Container.AddChild( $Container2 )
#>
function New-MRNGContainer {
[OutputType([mRemoteNG.Container.ContainerInfo])]
[CmdletBinding()]
param()
dynamicparam {
$DPDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$SkipParameters = 'Inheritance', 'ConstantID', 'IsContainer'
[mRemoteNG.Container.ContainerInfo].GetProperties() |
Where-Object { $SkipParameters -notcontains $_.Name } |
ForEach-Object {
$Attribute = New-Object System.Management.Automation.ParameterAttribute
$Attribute.ParameterSetName = '__AllParameterSets'
$Collection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$Collection.Add($Attribute)
$Parameter = New-Object System.Management.Automation.RuntimeDefinedParameter( $_.Name, $_.PropertyType, $Collection )
$DPDictionary.Add( $_.Name, $Parameter )
}
$Attribute = New-Object System.Management.Automation.ParameterAttribute
$Attribute.ParameterSetName = '__AllParameterSets'
$Collection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$Collection.Add($Attribute)
$Parameter = New-Object System.Management.Automation.RuntimeDefinedParameter( 'Inheritance', [hashtable], $Collection )
$DPDictionary.Add( 'Inheritance', $Parameter )
return $DPDictionary
}
process {
$ContainerInfo = [mRemoteNG.Container.ContainerInfo]::new()
# set default port
if ( $PSBoundParameters.Keys -contains 'Protocol' -and $PSBoundParameters.Keys -notcontains 'Port' ) {
$PSBoundParameters.Port = switch ( $PSBoundParameters.Protocol ) {
'RDP' { 3389 }
'VNC' { 5900 }
'SSH1' { 22 }
'SSH2' { 22 }
'Telnet' { 23 }
'Rlogin' { 513 }
'RAW' { 23 }
'HTTP' { 80 }
'HTTPS' { 443 }
'IntApp' { 0 }
}
}
# set connection properties
$PSBoundParameters.Keys |
Where-Object { $_ -notmatch 'Parent|Children|Inheritance' } |
Where-Object { [mRemoteNG.Container.ContainerInfo].GetProperties().Name -contains $_ } |
ForEach-Object { $ContainerInfo.$_ = $PSBoundParameters.$_ }
# configure inheritance
if ( $PSBoundParameters.Keys -contains 'Inheritance' ) {
# process EverythingInherited first
if ( $PSBoundParameters.Inheritance.Keys -contains 'EverythingInherited' ) {
$ContainerInfo.Inheritance.EverythingInherited = $true
}
# process all other inheritence
$PSBoundParameters.Inheritance.Keys |
Where-Object { $_ -ne 'EverythingInherited' } |
ForEach-Object {
$ContainerInfo.Inheritance.$_ = $PSBoundParameters.Inheritance.$_
}
}
# if children is specified we append
if ( $PSBoundParameters.Keys -contains 'Children' ) {
$PSBoundParameters.Children |
ForEach-Object {
$ContainerInfo.AddChild($_)
Write-Verbose ( 'Child ''{0}'' added to ''{1}''.' -f $_.Name, $ContainerInfo.Name )
}
}
# if parent is specified we append, otherwise we output
if ( $PSBoundParameters.Keys -contains 'Parent' ) {
$PSBoundParameters.Parent.AddChild( $ContainerInfo )
Write-Verbose ( 'Container ''{0}'' added to ''{1}''.' -f $ContainerInfo.Name, $PSBoundParameters.Parent.Name )
}
$ContainerInfo
}
}
<#
.SYNOPSIS
Create an mRemoteNG connection node.
.EXAMPLE
New-MRNGConnection -Name 'Test Connection' -HostName '127.0.0.1' -Parent $RootNode -Protocol SSH2 -Inheritance (New-MRNGInheritanceConfiguration -EverythingInherited -Protocol:$false)
.EXAMPLE
$Connection = New-MRNGConnection -Name 'Test Connection' -HostName '127.0.0.1'
$RootContainer.AddChild( $Connection )
#>
function New-MRNGConnection {
[OutputType([mRemoteNG.Connection.ConnectionInfo])]
[CmdletBinding()]
param()
dynamicparam {
$DPDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$SkipParameters = 'Inheritance', 'ConstantID', 'IsContainer'
[mRemoteNG.Connection.ConnectionInfo].GetProperties() |
Where-Object { $SkipParameters -notcontains $_.Name } |
ForEach-Object {
$Attribute = New-Object System.Management.Automation.ParameterAttribute
$Attribute.ParameterSetName = '__AllParameterSets'
$Collection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$Collection.Add($Attribute)
$Parameter = New-Object System.Management.Automation.RuntimeDefinedParameter( $_.Name, $_.PropertyType, $Collection )
$DPDictionary.Add( $_.Name, $Parameter )
}
$Attribute = New-Object System.Management.Automation.ParameterAttribute
$Attribute.ParameterSetName = '__AllParameterSets'
$Collection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$Collection.Add($Attribute)
$Parameter = New-Object System.Management.Automation.RuntimeDefinedParameter( 'Inheritance', [hashtable], $Collection )
$DPDictionary.Add( 'Inheritance', $Parameter )
return $DPDictionary
}
process {
$ConnectionInfo = [mRemoteNG.Connection.ConnectionInfo]::new()
# set default port
if ( $PSBoundParameters.Keys -contains 'Protocol' -and $PSBoundParameters.Keys -notcontains 'Port' ) {
$PSBoundParameters.Port = switch ( $PSBoundParameters.Protocol ) {
'RDP' { 3389 }
'VNC' { 5900 }
'SSH1' { 22 }
'SSH2' { 22 }
'Telnet' { 23 }
'Rlogin' { 513 }
'RAW' { 23 }
'HTTP' { 80 }
'HTTPS' { 443 }
'IntApp' { 0 }
}
}
# set connection properties
$PSBoundParameters.Keys |
Where-Object { $_ -notmatch 'Parent|Inheritance' } |
Where-Object { [mRemoteNG.Connection.ConnectionInfo].GetProperties().Name -contains $_ } |
ForEach-Object { $ConnectionInfo.$_ = $PSBoundParameters.$_ }
# configure inheritance
if ( $PSBoundParameters.Keys -contains 'Inheritance' ) {
# process EverythingInherited first
if ( $PSBoundParameters.Inheritance.Keys -contains 'EverythingInherited' ) {
$ConnectionInfo.Inheritance.EverythingInherited = $true
}
# process all other inheritence
$PSBoundParameters.Inheritance.Keys |
Where-Object { $_ -ne 'EverythingInherited' } |
ForEach-Object {
$ConnectionInfo.Inheritance.$_ = $PSBoundParameters.Inheritance.$_
}
}
# if parent is specified we append, otherwise we output
if ( $PSBoundParameters.Keys -contains 'Parent' ) {
$PSBoundParameters.Parent.AddChild( $ConnectionInfo )
Write-Verbose ( 'Connection ''{0}'' added to ''{1}''.' -f $ConnectionInfo.Name, $PSBoundParameters.Parent.Name )
}
$ConnectionInfo
}
}
<#
.SYNOPSIS
Helper function to build an inheritance configuration.
.EXAMPLE
New-MRNGInheritanceConfiguration -EverythingInherited -Icon:$false
# inherits everything except the icon
#>
function New-MRNGInheritanceConfiguration {
[OutputType([hashtable])]
[CmdletBinding()]
param()
dynamicparam {
$DPDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
[mRemoteNG.Connection.ConnectionInfoInheritance].GetProperties() |
Where-Object { $_.Name -ne 'Parent' } |
ForEach-Object {
$Attribute = New-Object System.Management.Automation.ParameterAttribute
$Attribute.ParameterSetName = '__AllParameterSets'
$Collection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
$Collection.Add($Attribute)
$Parameter = New-Object System.Management.Automation.RuntimeDefinedParameter( $_.Name, [switch], $Collection )
$DPDictionary.Add( $_.Name, $Parameter )
}
return $DPDictionary
}
process {
$ReturnHashtable = @{}
$PSBoundParameters.Keys |
Where-Object { [mRemoteNG.Connection.ConnectionInfoInheritance].GetProperties().Name -contains $_ } |
ForEach-Object { $ReturnHashtable.$_ = $PSBoundParameters.$_ }
$ReturnHashtable
}
}
<#
.SYNOPSIS
Exports the mRemoteNG connection file with the encryption parameters specified.
.PARAMETER RootNode
The mRemoteNG root connections node generated by New-MRNGRootNode.
.PARAMETER Path
The path to export the connection file to.
.PARAMETER EncryptionKey
The encryption key to use to secure the connections file. If no password is supplied the default mRemoteNG encryption key is "mR3m".
.PARAMETER EncryptionEngine
The encryption engine to use when encrypting the connection passwords. Choices are 'AES', 'Serpent', and 'Twofish'. The default is 'AES'.
.PARAMETER BlockCipherMode
The block cipher mode to use when encrypting the connection passwords. Choices are 'GCM', 'CCM', and 'EAX'. The default is 'GCM'.
.PARAMETER KeyDerivationIterations
The number of key derivation iterations to perform when encrypting the connection passwords. Valid values are in the range 1,000 to 50,000.
.PARAMETER SortConnections
Should the exported connections be sorted?
.EXAMPLE
Export-MRNGConnectionFile -RootNode $RootNode -Path .\Connections.xml (Get-Credential).Password -SortConnections
#>
function Export-MRNGConnectionFile {
[OutputType([void])]
param(
[Parameter(Mandatory)]
[mRemoteNG.Tree.Root.RootNodeInfo]
$RootNode,
[Parameter(Mandatory)]
[System.IO.FileInfo]
$Path,
[ValidateSet('AES', 'Serpent', 'Twofish')]
[string]
$EncryptionEngine = 'AES',
[ValidateSet('GCM', 'CCM', 'EAX')]
[string]
$BlockCipherMode = 'GCM',
[ValidateRange(1000, 50000)]
[int]
$KeyDerivationIterations = 1000,
[switch]
$SortConnections
)
# should we sort?
if ( $SortConnections ) {
$RootNode.SortRecursive()
}
# resolve the path
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath( $Path )
# get the encryption key as [securestring]
if ( $RootNode.Password ) {
$EncryptionKey = $RootNode.PasswordString |
ConvertTo-SecureString -AsPlainText -Force
} else {
$EncryptionKey = $RootNode.DefaultPassword |
ConvertTo-SecureString -AsPlainText -Force
}
# choose the encryption engine
$Engine = switch ( $EncryptionEngine ) {
'AES' { [Org.BouncyCastle.Crypto.Engines.AesEngine]::new() }
'Serpent' { [Org.BouncyCastle.Crypto.Engines.SerpentEngine]::new() }
'Twofish' { [Org.BouncyCastle.Crypto.Engines.TwofishEngine]::new() }
}
# choose the cipher mode
$Cipher = switch ( $BlockCipherMode ) {
'GCM' { [Org.BouncyCastle.Crypto.Modes.GcmBlockCipher]::new( $Engine ) }
'CCM' { [Org.BouncyCastle.Crypto.Modes.CcmBlockCipher]::new( $Engine ) }
'EAX' { [Org.BouncyCastle.Crypto.Modes.EaxBlockCipher]::new( $Engine ) }
}
$CryptoProvider = [mRemoteNG.Security.SymmetricEncryption.AeadCryptographyProvider]::new( $Cipher )
$SaveFilter = [mRemoteNG.Security.SaveFilter]::new()
# XML serializer - compat for 1.76
if ( 'mRemoteNG.Config.Serializers.Xml.XmlConnectionNodeSerializer26' -as [type] ) {
$ConnectionNodeSerializer = [mRemoteNG.Config.Serializers.Xml.XmlConnectionNodeSerializer26]::new($CryptoProvider, $EncryptionKey, $SaveFilter)
$XmlSerializer = [mRemoteNG.Config.Serializers.Xml.XmlConnectionsSerializer]::new($CryptoProvider, $ConnectionNodeSerializer)
} else {
$XmlSerializer = [mRemoteNG.Config.Serializers.XmlConnectionsSerializer]::new($CryptoProvider)
}
# save the connection file
$FilePathProvider = [mRemoteNG.Config.DataProviders.FileDataProvider]::new( $Path )
$FilePathProvider.Save( $XmlSerializer.Serialize( $RootNode ) )
}
<#
.SYNOPSIS
Convert a mRemoteNG secure string to plain text.
.PARAMETER EncryptedMessage
The mRemoteNG secure string to be converted.
.PARAMETER EncryptionKey
The encryption key that was used when encrypting the ConfCons.xml file. If no password is supplied the default mRemoteNG encryption key is "mR3m".
.PARAMETER EncryptionEngine
The encryption engine to use when decrypting the string. Choices are 'AES', 'Serpent', and 'Twofish'. The default is 'AES'.
.PARAMETER BlockCipherMode
The block cipher mode to use when decrypting the string. Choices are 'GCM', 'CCM', and 'EAX'. The default is 'GCM'.
.PARAMETER KeyDerivationIterations
The number of key derivation iterations to perform when decrypting the string. Valid values are in the range 1,000 to 50,000.
.EXAMPLE
ConvertFrom-MRNGSecureString -EncryptedMessage 'pLs5zen+UvaqFnn2KDn2eTrhO60gjzagqFnI/8n3dF74zCj9lZDGvR1nJ8bxf5OCuHJW8gcWFWOicNIvV4h1'
.EXAMPLE
ConvertFrom-MRNGSecureString -EncryptedMessage 'LkJUc6Q60hsZmX6QImOS+1nvFYQLNNCfP7iEupby8Ey84Dz+3u7lRo93YaL6fJf2GCXOtpXtzgZACxhVKcJh' -EncryptionEngine Serpent -BlockCipherMode EAX
.EXAMPLE
ConvertFrom-MRNGSecureString -EncryptedMessage 'MqE3IQxYiLioTaD86rzRusDmiD2nX2b9uabDASZdRB9+gk7ygLWSqYFVEg5zqa65qe6j3ZPtgeLxKNZiIoGv' -EncryptionKey ( 'password' | ConvertTo-SecureString -AsPlainText -Force )
#>
function ConvertFrom-MRNGSecureString {
[OutputType([string])]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$EncryptedMessage,
[ValidateNotNullOrEmpty()]
[securestring]
$EncryptionKey = ( ConvertTo-SecureString -String 'mR3m' -AsPlainText -Force ),
[ValidateSet('AES', 'Serpent', 'Twofish')]
[string]
$EncryptionEngine = 'AES',
[ValidateSet('GCM', 'CCM', 'EAX')]
[string]
$BlockCipherMode = 'GCM',
[ValidateRange(1000, 50000)]
[int]
$KeyDerivationIterations = 1000
)
# choose the text encoding
$Encoding = [System.Text.Encoding]::UTF8
# convert the $EncryptionKey to plain text
$EncryptionKeyBSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR( $EncryptionKey )
$EncryptionKeyText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto( $EncryptionKeyBSTR )
$EncryptionKeyBytes = [Org.BouncyCastle.Crypto.PbeParametersGenerator]::Pkcs5PasswordToBytes( [char[]]$EncryptionKeyText )
# convert the base64 encoded string to a byte array
$EncryptedMessageBytes = [convert]::FromBase64String( $EncryptedMessage )
# choose the encryption engine
$Engine = switch ( $EncryptionEngine ) {
'AES' { [Org.BouncyCastle.Crypto.Engines.AesEngine]::new() }
'Serpent' { [Org.BouncyCastle.Crypto.Engines.SerpentEngine]::new() }
'Twofish' { [Org.BouncyCastle.Crypto.Engines.TwofishEngine]::new() }
}
# choose the cipher mode
$Cipher = switch ( $BlockCipherMode ) {
'GCM' { [Org.BouncyCastle.Crypto.Modes.GcmBlockCipher]::new( $Engine ) }
'CCM' { [Org.BouncyCastle.Crypto.Modes.CcmBlockCipher]::new( $Engine ) }
'EAX' { [Org.BouncyCastle.Crypto.Modes.EaxBlockCipher]::new( $Engine ) }
}
# generate a new random string
#$SecureRandom = [Org.BouncyCastle.Security.SecureRandom]::new()
# defaults from mRemoteNG source
$NonceBitSize = 128
$MacBitSize = 128
$KeyBitSize = 256
$SaltBitSize = 128
#$MinPasswordLength = 1
# get the salt
$Salt = New-Object byte[] ( $SaltBitSize/8 )
[array]::Copy( $EncryptedMessageBytes, 0, $Salt, 0, $Salt.Length )
# get the key
$KeyGenerator = [Org.BouncyCastle.Crypto.Generators.Pkcs5S2ParametersGenerator]::new()
$KeyGenerator.Init( $EncryptionKeyBytes, $Salt, $KeyDerivationIterations )
$KeyParameter = $KeyGenerator.GenerateDerivedMacParameters($KeyBitSize)
#$KeyBytes = $KeyParameter.GetKey()
# create the cipher reader
$CipherStream = New-Object System.IO.MemoryStream (, $EncryptedMessageBytes )
$CipherReader = New-Object System.IO.BinaryReader ( $CipherStream )
# get the payload
$Payload = $CipherReader.ReadBytes( $Salt.Length )
# get the nonce
$Nonce = $CipherReader.ReadBytes( $NonceBitSize / 8 )
# build the decryption parameters
$Parameters = [Org.BouncyCastle.Crypto.Parameters.AeadParameters]::new( $KeyParameter, $MacBitSize, $Nonce, $Payload )
# initialize the Cipher
$Cipher.Init( $false, $Parameters )
# get the cipher text
$CipherTextBytes = $CipherReader.ReadBytes( $EncryptedMessageBytes.Length - $Nonce.Length )
# read the decoded text byte array
$PlainTextByteArray = New-Object byte[] ( $Cipher.GetOutputSize( $CipherTextBytes.Length ) )
# get the length of the decoded string
$Len = $Cipher.ProcessBytes( $CipherTextBytes, 0, $CipherTextBytes.Length, $PlainTextByteArray, 0 )
# do finial decryption
$Cipher.DoFinal( $PlainTextByteArray, $Len ) > $null
# output UTF8 encoded string
$Encoding.GetString( $PlainTextByteArray )
}
<#
.SYNOPSIS
Convert a plain text string to a mRemoteNG secure string.
.PARAMETER Message
The string to be converted.
.PARAMETER EncryptionKey
The encryption key to use when encrypting the string. It should match what you are using in your ConfCons.xml file. If no password is supplied the default mRemoteNG encryption key is "mR3m".
.PARAMETER EncryptionEngine
The encryption engine to use when encrypting the string. Choices are 'AES', 'Serpent', and 'Twofish'. The default is 'AES'.
.PARAMETER BlockCipherMode
The block cipher mode to use when encrypting the string. Choices are 'GCM', 'CCM', and 'EAX'. The default is 'GCM'.
.PARAMETER KeyDerivationIterations
The number of key derivation iterations to perform when encrypting the string. Valid values are in the range 1,000 to 50,000.
.EXAMPLE
ConvertTo-MRNGSecureString -Message 'SecureP@ssword!'
.EXAMPLE
ConvertTo-MRNGSecureString -Message 'SecureP@ssword!' -EncryptionEngine Serpent -BlockCipherMode EAX
.EXAMPLE
ConvertTo-MRNGSecureString -Message 'SecureP@ssword!' -EncryptionKey ( 'password' | ConvertTo-SecureString -AsPlainText -Force )
#>
function ConvertTo-MRNGSecureString {
[OutputType([string])]
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]
$Message,
[ValidateNotNullOrEmpty()]
[securestring]
$EncryptionKey = ( ConvertTo-SecureString -String 'mR3m' -AsPlainText -Force ),
[ValidateSet('AES', 'Serpent', 'Twofish')]
[string]
$EncryptionEngine = 'AES',
[ValidateSet('GCM', 'CCM', 'EAX')]
[string]
$BlockCipherMode = 'GCM',
[ValidateRange(1000, 50000)]
[int]
$KeyDerivationIterations = 1000
)
# choose the text encoding
$Encoding = [System.Text.Encoding]::UTF8
# convert the $EncryptionKey to plain text
$EncryptionKeyBSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR( $EncryptionKey )
$EncryptionKeyText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto( $EncryptionKeyBSTR )
$EncryptionKeyBytes = [Org.BouncyCastle.Crypto.PbeParametersGenerator]::Pkcs5PasswordToBytes( [char[]]$EncryptionKeyText )
# convert message to byte array
$MessageBytes = $Encoding.GetBytes($Message)
# choose the encryption engine
$Engine = switch ( $EncryptionEngine ) {
'AES' { [Org.BouncyCastle.Crypto.Engines.AesEngine]::new() }
'Serpent' { [Org.BouncyCastle.Crypto.Engines.SerpentEngine]::new() }
'Twofish' { [Org.BouncyCastle.Crypto.Engines.TwofishEngine]::new() }
}
# choose the cipher mode
$Cipher = switch ( $BlockCipherMode ) {
'GCM' { [Org.BouncyCastle.Crypto.Modes.GcmBlockCipher]::new( $Engine ) }
'CCM' { [Org.BouncyCastle.Crypto.Modes.CcmBlockCipher]::new( $Engine ) }
'EAX' { [Org.BouncyCastle.Crypto.Modes.EaxBlockCipher]::new( $Engine ) }
}
# generate a new random string
$SecureRandom = [Org.BouncyCastle.Security.SecureRandom]::new()
# defaults from mRemoteNG source
$NonceBitSize = 128
$MacBitSize = 128
$KeyBitSize = 256
$SaltBitSize = 128
#$MinPasswordLength = 1
# create the salt
$Salt = New-Object byte[] ( $SaltBitSize/8 )
$SecureRandom.NextBytes($Salt)
# create the key
$KeyGenerator = [Org.BouncyCastle.Crypto.Generators.Pkcs5S2ParametersGenerator]::new()
$KeyGenerator.Init( $EncryptionKeyBytes, $Salt, $KeyDerivationIterations )
$KeyParameter = $KeyGenerator.GenerateDerivedMacParameters($KeyBitSize)
#$KeyBytes = $KeyParameter.GetKey()
# create the payload and add the salt
$Payload = New-Object byte[] ( $Salt.Length )
[array]::Copy( $Salt, 0, $Payload, 0, $Salt.Length )
# create the nonce
$Nonce = New-Object byte[] ( $NonceBitSize / 8 )
$SecureRandom.NextBytes( $Nonce, 0, $Nonce.Length )
# build the encryption parameters
$Parameters = [Org.BouncyCastle.Crypto.Parameters.AeadParameters]::new( $KeyParameter, $MacBitSize, $Nonce, $Payload )
# initialize the cipher
$Cipher.Init( $true, $Parameters )
# encrypt the message
$CipherText = New-Object byte[] ($Cipher.GetOutputSize( $MessageBytes.Length ))
$Len = $Cipher.ProcessBytes( $MessageBytes, 0, $MessageBytes.Length, $CipherText, 0 )
$Cipher.DoFinal( $CipherText, $Len ) > $null
# create a binary stream
$CombinedStream = New-Object System.IO.MemoryStream
$BinaryWriter = New-Object System.IO.BinaryWriter ( $CombinedStream )
# write the payload, nonce, and cipher to the binary stream
$BinaryWriter.Write( $Payload )
$BinaryWriter.Write( $Nonce )
$BinaryWriter.Write( $CipherText )
# convert the binary stream to a base64 encoded string
[convert]::ToBase64String( $CombinedStream.ToArray() )
}