Sign in to follow this  
Followers 0
Graywalker

Get URL from Broswer (all tabs) using PID - remotely?

6 posts in this topic

#1 ·  Posted (edited)

Task : If Java.exe opens and it's parent process is a browser, get the URL of the web page it has open (hopefully the URL that started java).

I would love a way to do it remotely, but can live with having to run a monitor on the local machine.

What I have now is a function that uses WMI to query win32_process and get the processes and parent process id, searches that for Java and puts that info into a report.

I want to include the URL of the webpage that launched java. What I have so far is below, but it doesn't let me tie the Process ID to the window I get the URL from. It also will not get Firefox windows. The Shell method only gets Shell windows. I have nine windows open, but it only picks up two - internet explorer and file system explorer - missing both Firefox windows and all their tabs.

So, the question is - how to get a browser object when I start with only a Process ID and get the URL from that object? On a remote computer?

$oShell = ObjCreate("shell.application") ; Get the windows shell object

$oShellWindows = $oshell.windows ; Get open windows

If IsObj($oShellWindows) Then
    $string = ""
    For $Window in $oShellWindows ; count all windows
        $Wpid = ObjName($Window,3)
        $string = $string & $Window.LocationName & " ; " & $Window.FullName & " ; " & $Wpid & @CRLF
        If StringInStr($Window.FullName, "iexplore") Then
            MsgBox(0,"Internet Explorer", "You browsing the URL " & $Window.Document.Location.href)
        EndIf

    Next
    MsgBox(0,"Shell Windows", "You have the following " & $oShellWindows.Count & " windows open:" & @CRLF & $string);
EndIf

Fantasy code I would like to work:

$objWMIService = ObjGet("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & $strPCName & "\root\cimv2")
If IsObj($objWMIService) Then
  $colProcesses = $objWMIService.ExecQuery( _
    "select * from win32_process")
For $objProcess In $colProcesses
If $objProcess.ProcessId = $javaparentPID Then
   $Name = $objProcess.LocationName
   $URL = $objProcess.Document.Location.href
Endif

Code that gets the process IDs :

Func _ProcessInfo($strPCName)
Dim $i = 0
Dim $User, $Domain, $objWMIService, $colProcesses, $procnum
Dim $Aprocesses[1][9]
Dim $pi, $ppi, $parent, $parentfound
$objWMIService = ObjGet("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & $strPCName & "\root\cimv2")
; Gather process information into an array
If IsObj($objWMIService) Then
  $colProcesses = $objWMIService.ExecQuery( _
    "select * from win32_process")
  For $objProcess In $colProcesses
   $i = $i + 1
   ReDim $Aprocesses[$i + 1][9]
   $Aprocesses[$i][1] = $objProcess.ProcessId
   $Aprocesses[$i][2] = $objProcess.Caption
   $Aprocesses[$i][3] = $objProcess.HandleCount
   $Aprocesses[$i][4] = $objProcess.ExecutablePath
   $Aprocesses[$i][5] = $objProcess.ParentProcessId
   $Aprocesses[$i][6] = $objProcess.CreationDate
   If $objProcess.GetOwner($User, $Domain) = 0 Then
    $Aprocesses[$i][7] = $Domain & _
      "\" & $User
   Else
    $Aprocesses[$i][7] = ""
   EndIf
  Next
  $Aprocesses[0][0] = $i
  $procnum = $i
  ;_ArrayDisplay($Aprocesses)
  ; Process the information about the Processes - LOL!
  $parentfound = 0
  For $pi = 1 To $procnum
   ; only looking for Java, so :
   If StringInStr($Aprocesses[$pi][2], "java") Then
    $parent = $Aprocesses[$pi][5]
    For $ppi = 1 To $procnum
     If $Aprocesses[$ppi][1] = $parent Then
      $parentfound = 1
      ExitLoop
     EndIf
    Next
    If $parentfound = 1 Then
     ; It has a parent process still running!
     $line = $strPCName & "," & $Aprocesses[$pi][1] & "," & $Aprocesses[$pi][2] & "," & $Aprocesses[$pi][3] & "," & _
       $Aprocesses[$pi][4] & "," & $Aprocesses[$pi][5] & "," & $Aprocesses[$pi][6] & "," & _
       $Aprocesses[$ppi][6] & "," & $Aprocesses[$ppi][2] & "," & $Aprocesses[$ppi][4] & "," & _
       $Aprocesses[$ppi][7]
     FileWriteLine($LogFile, $line)
    Else
     ; parent process left. No, little java, its not your fault... :)
     $line = $strPCName & "," & $Aprocesses[$pi][1] & "," & $Aprocesses[$pi][2] & "," & $Aprocesses[$pi][3] & "," & $Aprocesses[$pi][4] & "," & _
       $Aprocesses[$pi][5] & "," & $Aprocesses[$pi][6] & ",Parent already closed or not found" & ",Owner = ," & $Aprocesses[$pi][7]
     FileWriteLine($LogFile, $line)
    EndIf
   EndIf
   $parentfound = 0
   $ppi = 0
  Next
Else
  FileWriteLine($LogFile, $strPCName & ",Unable to query WMI on endpoint")
EndIf
EndFunc   ;==>_ProcessInfo
Edited by Graywalker

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

MsgBox(0,"Internet Explorer", "You browsing the URL " & $Window.LocationURL)

works just as well as

MsgBox(0,"Internet Explorer", "You browsing the URL " & $Window.Document.Location.href)

Something odd - I am on Windows 7 64bit - I have an open IE window.

$oIE = ObjGet("","InternetExplorer.Application")
$url = $oIE.LocationURL
$name = $oIE.LocationName

Returns an error on $url = $oIE.LocationURL of "==> Variable must be of type "Object".:"

Edited by Graywalker

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Wow... I guess it is complicated... lol!

So, I've discovered a few ways to get the URL from the Window based on the window title :

AutoItSetOption("WinTitleMatchMode", 2)
If WinExists("Internet Explorer") Then
$wtext = WinGetText("Internet Explorer")
$wtexta = StringSplit($wtext, @LF)
For $l = 1 To $wtexta[0]
  If StringInStr($wtexta[$l], "http") Then
   $url = $wtexta[$l]
   ExitLoop
  EndIf
Next
EndIf
MsgBox(0, "URL", $url)
Endif

OR

AutoItSetOption("WinTitleMatchMode", 2)
#include <IE.au3>
$awins = WinList("Internet Explorer")
For $w = 1 to $awins[0][0]
  $oBrowser = _IEAttach($awins[$w][1],"HWND")
  If IsObj($oBrowser) Then
  $bURL = $oBrowser.LocationURL
  $bName = $oBrowser.LocationName
  MsgBox(0,"Browser Info",$bName & @CRLF & $bURL)
  EndIf
Next

But neither lets me a) Get it remotely :) Tie it directly to the Process ID - and both rely on the Window Title having "Internet Explorer" in it. (edit to clarify - that means that if there are multiple windows of Internet Explorer open, I can't be sure which URL is the one that opened Java.)

Not happy.... any ideas?

... and why THIS doesn't work, I don't know!!

$oBrowser = _IEAttach($objProcess.Handle, "HWND")
If IsObj($oBrowser) Then
  $bURL = $oBrowser.LocationURL
  $bName = $oBrowser.LocationName
  MsgBox(0, "Browser Info", $bName & @CRLF & $bURL)
Else
  MsgBox(0, "NOT an Object!", "Dummy!!")
EndIf

... actually, I suppose I do... I think the "Handle" returned by Win32_Process is actually the Process ID, not a "HWND" handle.

Edited by Graywalker

Share this post


Link to post
Share on other sites

More info! Getting closer....

The following code gets the PID with the window - because once you have the handle, you can use the handle instead of the window title.

However, with tabbed browsing, the WinGetText($handle) only gets the tab that is in focus. Since most of my enterprise is on IE 7, this won't be much of an issue... BUT... it could get that way. Also, _IEAttach seems to have the same issue with tabs.

Any ideas how to get the URLs of tabs?

If StringInStr($Aprocesses[$i][4], "iexplore") Then
   $Pid = ""
   $wtext = ""
   $url = ""
   If WinExists("Internet Explorer") Then
    $awins = WinList("Internet Explorer")
    ;_ArrayDisplay($awins)
    For $w = 1 To $awins[0][0]
     $Pid = WinGetProcess($awins[$w][1])
     ;If $Pid = $Aprocesses[$i][1] Then
      $wtext = WinGetText($awins[$w][1])
      $wtexta = StringSplit($wtext, @LF)
      For $l = 1 To $wtexta[0]
       If StringInStr($wtexta[$l], "http") Then
        $url = $wtexta[$l]
        ExitLoop
       EndIf
      Next
     ;EndIf
     MsgBox(0,"Array Info",$Pid & @CRLF & $url)
    Next
   EndIf

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Got the URLs of the Tabs!

The following code seems to be mostly working - on a local machine. It doesn't work with Firefox, but that is not absolutely necessary at this point, although I would LOVE it if someone provided a solution to that - and doing it from a central computer to other computers on the local network.

The code lists all windows with "Internet Explorer" in the title bar, then tries to get an object from each - if it is not an object on it's own, it tries to see if it is embedded in the previous object. Either way, it tries to get the Title and the URL of the window - hopefully Internet Explorer. Unfortunatly, if someone changed the IE window title to "Internut Exploder", then this won't work too well. :)

AutoItSetOption("WinTitleMatchMode", 2)
AutoItSetOption("WinDetectHiddenText", 1)

$awins = WinList("Internet Explorer")
$mainhwnd = ""
For $w = 1 To $awins[0][0]
  FileWriteLine($logfile, "Windows array " & $w & "," & $awins[$w][0] & "," & $awins[$w][1])
  $oBrowser = _IEAttach($awins[$w][1], "HWND")
  If IsObj($oBrowser) Then
   $oldmainhwnd = $mainhwnd
   $mainhwnd = $awins[$w][1]
   $bURL = _IEPropertyGet($oBrowser, "locationurl");$oBrowser.Document.Location.href
   $bName = _IEPropertyGet($oBrowser, "title");$oBrowser.Location
   $Pid = WinGetProcess($awins[$w][1])
   FileWriteLine($logfile, $bName & "," & $bURL & "," & $Pid)
  Else
   If $oldmainhwnd = $mainhwnd Then
    ;Do Nothing, same old stuff.
   Else
    For $w2 = 1 To $awins[0][0]
     $oBrowser = _IEAttach($mainhwnd, "Embedded", $w2)
     If IsObj($oBrowser) Then
      $bURL = _IEPropertyGet($oBrowser, "locationurl");$oBrowser.Document.Location.href
      $bName = _IEPropertyGet($oBrowser, "title");$oBrowser.Location
      $Pid = WinGetProcess($awins[$w][1])
      FileWriteLine($logfile, $bName & "," & $bURL & "," & $Pid & ", Using HWND " & $mainhwnd & ", $w2 = " & $w2 & ", old hwnd = " & $oldmainhwnd)
     Else
      ; Do nothing - maybe exitloop??
     EndIf
    Next
    $oldmainhwnd = $mainhwnd
   EndIf
  EndIf
Next

If anyone shows interest, I will post the whole script to look for java and get the URLs that called it when I get that script completed. This piece was a monster all by itself.

Edited by Graywalker

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Nothing? Really?

Okay, how about a way to get the IE History remotely using AutoIT? Maybe the last 10 URLs visited? I hope that would include the currently open windows....

The script I wrote is not going to work, because of the way the User environments work... I can get the running processes of a user, but I can't, with an admin account, see the currently open windows on the user's account using _IEwhatever UDF or the WinList function. (running script using scheduled task as admin)

So... how to get the URLs???

Edited by Graywalker

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • BlazerV60
      By BlazerV60
      Hello all,
      I've been trying to figure out how to launch Google Chrome in the background (hidden) but it doesn't seem possible. I've tried the following methods:
      ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "", "", "", @SW_HIDE) Also I've tried:
      ShellExecute(@ComSpec, "/c start chrome.exe","","",@SW_HIDE) Lastly I tried:
      RunWait('"'&@ProgramFilesDir&'\Google\Chrome\Application\chrome.exe" --silent-launch',@ScriptDir)  
      But all of them launch my chrome browser without hiding it. Does anyone know a workaround for this or if AutoIT just can't Chrome? 
      Bonus points if you know how to make it launch chrome hidden and make it go to https://www.autoitscript.com
       
      Thank you,
      Brian
    • BOUNCER
      By BOUNCER
      I'm trying to get the output data from https://www.guilded.gg but it uses a web app that generates that output on the fly and this doesn't show up in the page source.
      Only in web inspector of each browser does this data show up. How do I read this data in autoit (equivalent to the web inspector)
       
      thank you
    • ur
      By ur
      We can select elements in IE using their IDs as below.
      Local $oDiv = _IEGetObjById($oIE, "x-auto-16-input") But to one button in the webpage, there is no ID to it.
      In selenium we have option to select this element using the CssSelector and clicked the button using below code in c# selenium.
      driver.FindElement(By.CssSelector("button.x-btn-text")).Click(); What is the alternative for this in AUtoIT?
    • whiskas
      By whiskas
      Hi all,
      I just upgraded to windows 10 and am using Edge as my browser now. Updated my scripts, but I can not unhide a minimized Edge window with the script anymore.
      WinSetState ( "titlle","", @SW_MAXIMIZE ) works fine, but not if it is minimized. Tried same with SW_SHOW, doesn't work.
      WinExists("title","") -> returns 1, so it is finding my window.
       
              WinWait($title,$text,$timeout)
              If Not WinActive($title,$text) Then WinActivate($title,$text)
              WinWaitActive($title,$text,$timeout)
      Doesn't work also, so I think I tried all but cant get the window back
      Any help would be appreciated
       
    • 5ervant
      By 5ervant
      Go create a quick account in Zapier and go to https://zapier.com/app/dashboard and make some Zaps!
      Just need to click the specific instance of an .open-menu button and then click its a.run (anchor) element.
      Doing this IE.au3 script just causing to click its container element:
      $target = 1; Target the first instance #include <IE.au3> $oIE = _IEAttach("Dashboard - Zapier") $count = 0; $tags = _IETagNameGetCollection($oIE, "div") For $tag in $tags $class_value = $tag.GetAttribute("class") If $class_value = "open-menu" Then $count += 1 if $count = $target Then MsgBox(0, "Instance: ", $count) ; $tag.fireEvent("onmousedown") ; _IEAction($tag, "click") ; $tag.fireEvent("onmouseup") ; Or this but not working ; $tag.Click EndIf EndIf Next I also tried to do it using FF.au3
      $target = 1; Target the first instance #Include <Array.au3> #Include <FF.au3> $count = 0; If _FFConnect(Default, Default, 3000) Then $aArray = _FFXPath( "//div[@class='open-menu']", "", 7 ) ; _ArrayDisplay($aArray) For $tag in $aArray $count += 1 if $count = $target Then MsgBox(0, "Instance: ", $count) _FFClick($tag) EndIf Next EndIf Error: _FFClick ==> No match: $sElement: [number]
      Could some help me how to click such buttons on such kind of a dynamic page?