Skip to content

Commit 786014c

Browse files
committed
WindowTextExtractor can show passwords for Microsoft Internet Explorer
1 parent ca8a965 commit 786014c

File tree

6 files changed

+106
-4
lines changed

6 files changed

+106
-4
lines changed

WindowTextExtractor/Forms/MainForm.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.Drawing;
88
using System.Text;
9+
using System.Linq;
910
using WindowTextExtractor.Extensions;
1011
using WindowTextExtractor.Utils;
1112

@@ -215,10 +216,21 @@ public bool PreFilterMessage(ref Message m)
215216
if (element.Current.IsPassword)
216217
{
217218
var elementHandle = new IntPtr(element.Current.NativeWindowHandle);
218-
int processId;
219-
NativeMethods.GetWindowThreadProcessId(elementHandle, out processId);
220-
var process = Process.GetProcessById(processId);
221-
if (Environment.Is64BitOperatingSystem && !process.HasExited && !process.IsWow64Process())
219+
var process = Process.GetProcessById(element.Current.ProcessId);
220+
if (process.ProcessName.ToLower() == "iexplore")
221+
{
222+
elementHandle = elementHandle == IntPtr.Zero ? NativeMethods.WindowFromPoint(new Point(cursorPosition.X, cursorPosition.Y)) : elementHandle;
223+
if (elementHandle != IntPtr.Zero)
224+
{
225+
var passwords = WindowUtils.GetPasswordsFromHtmlPage(elementHandle);
226+
if (passwords.Any())
227+
{
228+
txtContent.Text = passwords.Count > 1 ? string.Join(Environment.NewLine, passwords.Select((x, i) => "Password " + (i + 1) + ": " + x)) : passwords[0];
229+
txtContent.ScrollTextToEnd();
230+
OnTextContentChanged();
231+
}
232+
}
233+
} else if (Environment.Is64BitOperatingSystem && !process.HasExited && !process.IsWow64Process())
222234
{
223235
Process.Start(new ProcessStartInfo
224236
{

WindowTextExtractor/NativeConstants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ static class NativeConstants
66
public const int WM_MOUSEMOVE = 0x0200;
77
public const int WM_COPYDATA = 0x004A;
88
public const int STD_OUTPUT_HANDLE = -11;
9+
public const int SMTO_ABORTIFHUNG = 0x2;
910
}
1011
}

WindowTextExtractor/NativeMethods.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
using System;
2+
using System.Text;
23
using System.Runtime.InteropServices;
4+
using System.Drawing;
5+
using mshtml;
36

47
namespace WindowTextExtractor
58
{
69
static class NativeMethods
710
{
11+
public delegate int EnumProc(IntPtr hWnd, ref IntPtr lParam);
12+
813
[DllImport("kernel32.dll", SetLastError = true)]
914
public static extern bool AttachConsole(int processID);
1015

@@ -32,6 +37,21 @@ static class NativeMethods
3237
[DllImport("user32.dll")]
3338
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
3439

40+
[DllImport("user32.dll")]
41+
public static extern int GetClassName(IntPtr hwnd, StringBuilder lpClassName, int nMaxCount);
42+
43+
[DllImport("user32.dll")]
44+
public static extern int EnumChildWindows(IntPtr hWndParent, EnumProc lpEnumFunc, ref IntPtr lParam);
45+
46+
[DllImport("user32.dll")]
47+
public static extern int SendMessageTimeout(IntPtr hwnd, int msg, int wParam, int lParam, int fuFlags, int uTimeout, out int lpdwResult);
48+
49+
[DllImport("user32.dll")]
50+
public static extern IntPtr WindowFromPoint(Point p);
51+
52+
[DllImport("OLEACC.dll")]
53+
public static extern int ObjectFromLresult(int lResult, ref Guid riid, int wParam, ref IHTMLDocument2 ppvObject);
54+
3555
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
3656
[return: MarshalAs(UnmanagedType.Bool)]
3757
internal static extern bool IsWow64Process([In] IntPtr process, [Out] out bool wow64Process);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using mshtml;
5+
6+
namespace WindowTextExtractor.Utils
7+
{
8+
static class WindowUtils
9+
{
10+
public static IList<string> GetPasswordsFromHtmlPage(IntPtr hWnd)
11+
{
12+
var result = new List<string>();
13+
var proc = new NativeMethods.EnumProc(EnumWindows);
14+
NativeMethods.EnumChildWindows(hWnd, proc, ref hWnd);
15+
if (hWnd != IntPtr.Zero)
16+
{
17+
var message = NativeMethods.RegisterWindowMessage("WM_HTML_GETOBJECT");
18+
if (message != 0)
19+
{
20+
var messageResult = 0;
21+
NativeMethods.SendMessageTimeout(hWnd, message, 0, 0, NativeConstants.SMTO_ABORTIFHUNG, 1000, out messageResult);
22+
if (messageResult != 0)
23+
{
24+
IHTMLDocument2 document = null;
25+
var iidIHtmlDocument = new Guid("626FC520-A41E-11CF-A731-00A0C9082637");
26+
NativeMethods.ObjectFromLresult(messageResult, ref iidIHtmlDocument, 0, ref document);
27+
if (document != null)
28+
{
29+
foreach (var element in document.all)
30+
{
31+
var inputElement = element as IHTMLInputElement;
32+
if (inputElement != null && inputElement.type != null && inputElement.type.ToLower() == "password" && !string.IsNullOrEmpty(inputElement.value))
33+
{
34+
result.Add(inputElement.value);
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}
41+
return result;
42+
}
43+
44+
private static int EnumWindows(IntPtr hWnd, ref IntPtr lParam)
45+
{
46+
var result = 1;
47+
var className = new StringBuilder(1024);
48+
NativeMethods.GetClassName(hWnd, className, className.Capacity);
49+
if (string.Compare(className.ToString(), "Internet Explorer_Server") == 0)
50+
{
51+
lParam = hWnd;
52+
result = 0;
53+
}
54+
return result;
55+
}
56+
}
57+
}

WindowTextExtractor/WindowTextExtractor.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
<DependentUpon>Resources.resx</DependentUpon>
114114
</Compile>
115115
<Compile Include="Utils\AssemblyUtils.cs" />
116+
<Compile Include="Utils\WindowUtils.cs" />
116117
<EmbeddedResource Include="Forms\AboutForm.resx">
117118
<DependentUpon>AboutForm.cs</DependentUpon>
118119
</EmbeddedResource>
@@ -150,6 +151,17 @@
150151
<Install>false</Install>
151152
</BootstrapperPackage>
152153
</ItemGroup>
154+
<ItemGroup>
155+
<COMReference Include="MSHTML">
156+
<Guid>{3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}</Guid>
157+
<VersionMajor>4</VersionMajor>
158+
<VersionMinor>0</VersionMinor>
159+
<Lcid>0</Lcid>
160+
<WrapperTool>primary</WrapperTool>
161+
<Isolated>False</Isolated>
162+
<EmbedInteropTypes>True</EmbedInteropTypes>
163+
</COMReference>
164+
</ItemGroup>
153165
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
154166
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
155167
Other similar extension points exist, see Microsoft.Common.targets.
3.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)