1+ // This file is part of the OWASP O2 Platform (http://www.owasp.org/index.php/OWASP_O2_Platform) and is released under the Apache 2.0 License (http://www.apache.org/licenses/LICENSE-2.0)
2+ using System ;
3+ using System . Collections . Generic ;
4+ using System . Windows . Forms ;
5+ using System . Text ;
6+ using System . Reflection ;
7+ using System . Management ;
8+ using System . Diagnostics ;
9+ using O2 . Kernel . ExtensionMethods ;
10+ using O2 . DotNetWrappers . DotNet ;
11+ using O2 . DotNetWrappers . ExtensionMethods ;
12+ using O2 . XRules . Database . Utils ;
13+ using OpenQA . Selenium ;
14+ using OpenQA . Selenium . Chrome ;
15+ //O2File:API_Win32_Handle_Hijack.cs
16+ //O2Ref:Selenium\net40\WebDriver.dll
17+ //Installer:Selenium_Installer.cs!Selenium\net40\WebDriver.dll
18+
19+ //O2Ref:System.Management.dll
20+
21+ namespace O2 . XRules . Database . APIs
22+ {
23+ public class API_Chrome_Hijack
24+ {
25+ public ChromeDriver ChromeDriver { get ; set ; }
26+ public ChromeOptions ChromeOptions { get ; set ; }
27+ public ChromeDriverService ChromeDriverService { get ; set ; }
28+ public Process ChromeDriverProcess { get ; set ; }
29+ public Process ChromeProcess { get ; set ; }
30+ public Panel Panel_Chrome { get ; set ; }
31+ public Panel Panel_ChromeDriver { get ; set ; }
32+ public ascx_Simple_Script_Editor WebDriver_Script_Me { get ; set ; }
33+ public string ChromeDriver_Exe { get ; set ; }
34+ public string WebDriver_Folder { get ; set ; }
35+ public string ChromeDriverDownloadLink { get ; set ; }
36+
37+ public API_Chrome_Hijack ( )
38+ {
39+ ChromeDriverDownloadLink = @"http://chromedriver.googlecode.com/files/chromedriver_win_26.0.1383.0.zip" ;
40+ WebDriver_Folder = @"Selenium\net40\WebDriver.dll" . assembly ( ) . location ( ) . parentFolder ( ) ;
41+ ChromeDriver_Exe = WebDriver_Folder . pathCombine ( "chromedriver.exe" ) ;
42+ ChromeOptions = new ChromeOptions ( ) ;
43+ ChromeDriverService = ChromeDriverService . CreateDefaultService ( ) ;
44+ }
45+
46+ public API_Chrome_Hijack ensureChromeDriverExists ( )
47+ {
48+ if ( ChromeDriver_Exe . fileExists ( ) . isFalse ( ) )
49+ {
50+ var downloadedZipFile = ChromeDriverDownloadLink . download ( ) ;
51+ downloadedZipFile . unzip_File ( WebDriver_Folder ) ;
52+ }
53+ return this ;
54+ }
55+
56+ public API_Chrome_Hijack open_ChromeDriver ( )
57+ {
58+ ChromeDriver = new ChromeDriver ( ChromeDriverService , ChromeOptions ) ;
59+
60+ //resolve driverServiceProcess
61+ var fieldInfo = ( FieldInfo ) typeof ( DriverService ) . field ( "driverServiceProcess" ) ;
62+ ChromeDriverProcess = ( Process ) fieldInfo . GetValue ( ChromeDriverService ) ;
63+
64+ ChromeDriverProcess . waitFor_MainWindowHandle ( ) ;
65+ ChromeProcess = ChromeDriverProcess . getProcessWithParentHandle ( ) ;
66+
67+ return this ;
68+ }
69+
70+ }
71+
72+
73+ public static class API_Chrome_Test_ExtensionMethods
74+ {
75+ public static ChromeDriver add_Chrome ( this Panel targetPanel )
76+ {
77+ return new API_Chrome_Hijack ( )
78+ . open_ChromeDriver ( )
79+ . hijack_Chrome ( targetPanel , true )
80+ . ChromeDriver ;
81+ }
82+
83+ public static API_Chrome_Hijack add_Chrome_To_Panel ( this API_Chrome_Hijack chromeHijack , Panel targetPanel )
84+ {
85+ var chrome_Panel = targetPanel . add_GroupBox ( "Chrome" ) . add_Panel ( ) ;
86+ var chromeDriver_Panel = chromeHijack . Panel_Chrome . parent ( ) . insert_Below ( 150 , "Chrome WebDriver" ) ;
87+
88+ return chromeHijack . hijack_Chrome ( chrome_Panel , false )
89+ . hijack_ChromeDriver ( chromeDriver_Panel ) ;
90+ }
91+
92+ public static API_Chrome_Hijack hijack_Chrome ( this API_Chrome_Hijack chromeHijack , Panel targetPanel , bool hijackJustViewer )
93+ {
94+ if ( chromeHijack . notNull ( ) && chromeHijack . ChromeProcess . notNull ( ) && targetPanel . notNull ( ) )
95+ {
96+ chromeHijack . Panel_Chrome = targetPanel ;
97+ var hijackGui = chromeHijack . Panel_Chrome . add_Handle_HijackGui ( false ) ;
98+ if ( hijackJustViewer )
99+ {
100+ hijackGui . hijackProcessWindow ( chromeHijack . ChromeProcess ,
101+ ( mainWindowHandle ) =>
102+ {
103+ // this doesn't work well now since the main chrome window (with the address bar) still gains focus)
104+ // need to look at how to capture and filter events from the child to the parent (and vice versa)
105+ //
106+ var targetWindow = mainWindowHandle . child_Windows ( ) . second ( ) ;
107+ return targetWindow ;
108+
109+ return mainWindowHandle ; // for now hijack the whole thing
110+ } ) ;
111+
112+ }
113+ else
114+ hijackGui . hijackProcessMainWindow ( chromeHijack . ChromeProcess ) ;
115+ }
116+ return chromeHijack ;
117+ }
118+
119+ public static API_Chrome_Hijack hijack_ChromeDriver ( this API_Chrome_Hijack chromeHijack , Panel targetPanel )
120+ {
121+ if ( chromeHijack . notNull ( ) && chromeHijack . ChromeDriverProcess . notNull ( ) && targetPanel . notNull ( ) )
122+ {
123+ chromeHijack . Panel_ChromeDriver = targetPanel ;
124+ chromeHijack . Panel_ChromeDriver . add_Handle_HijackGui ( false )
125+ . hijackProcessMainWindow ( chromeHijack . ChromeDriverProcess ) ;
126+ }
127+ return chromeHijack ;
128+ }
129+
130+ public static API_Chrome_Hijack add_WebDriver_ScriptMe_To ( this API_Chrome_Hijack chromeHijack , Panel targetPanel )
131+ {
132+
133+ chromeHijack . WebDriver_Script_Me = chromeHijack . ChromeDriver . script_Me ( targetPanel ) ;
134+ return chromeHijack ;
135+ }
136+
137+ public static Win32_Handle_Hijack hijack_Process_Into_Panel ( this API_Chrome_Hijack chromeHijack , Panel targetPanel , Process targetProcess )
138+ {
139+ var hijackGui = targetPanel . add_Handle_HijackGui ( false )
140+ . hijackProcessMainWindow ( targetProcess ) ;
141+ return hijackGui ;
142+ }
143+ }
144+
145+ public static class Process_Extra_ExtensionMethods
146+ {
147+ public static Process getProcessWithParentHandle ( this Process process )
148+ {
149+ if ( process . isNotNull ( ) )
150+ {
151+ var selectQuery = "Select ProcessId from Win32_Process Where ParentProcessId = {0}" . format ( process . Id ) ; ;
152+ foreach ( var proc in new ManagementObjectSearcher ( selectQuery ) . Get ( ) )
153+ {
154+ var procId = ( int ) ( UInt32 ) proc [ "ProcessId" ] ;
155+ var foundProcess = procId . process_WithId ( ) ;
156+ "foundProcess: {0}" . debug ( foundProcess ) ;
157+ return foundProcess ;
158+ }
159+ "Failed to find process with id: {0} name: {0}" . error ( process . Id , process . ProcessName ) ;
160+ }
161+ return null ;
162+ }
163+ }
164+ }
165+
0 commit comments