Jump to content
Sign in to follow this  
Graywalker

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

Recommended Posts

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

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

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

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

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By HariKara
      I have a script that seems to launch perfectly fine with IECreate, however, I want to launch the Browser and a specific URL with the RunAs command. I fairly new to AutoIT and wanted to know if someone can point me in the right direction.
      Local $surl = "http://somewebsite.com/DODA/admin/job.aspx"
      Local $oIE = _IECreate($sURL)
      The above launches the website correctly with the correct URL, however, I have tried the below and this fails to load and browser?
      #include <AutoItConstants.au3>
      ;========================
      $oSleep = "200"
      Local $surl = "http://somewebsite.com/DODA/admin/job.aspx"
      Local $oIE = RunAs($args("username"), $args("domain"), $args("logonpassword"), "", "C:\Program Files (x86)\Internet Explorer\iexplore.exe http://somewebsite.com/DODA/admin/job.aspx")
      _IELoadWait($oIE)
       
      I get the following error back?
      --> IE.au3 T3.0-2 Error from function _IELoadWait, $_IESTATUS_InvalidDataType
      --> IE.au3 T3.0-2 Error from function _IEGetObjById, $_IESTATUS_InvalidDataType
      --> IE.au3 T3.0-2 Error from function _IEGetObjById, $_IESTATUS_InvalidDataType
    • By nacerbaaziz
      hello sirs
      please i want to use the WinHttp to get the google drive file title using the api
      i searched in the forom but i didn't found any Google drive api UDF
      for that i liked to ask you for that
      i had read the google drive api documentation but i couldn't do it
      please help me
      here is the api doc
      note i've got the apikey and i have the file id
      what i want is to get the title of the file (the file name)
      because i want to download the files from the google drive using the autoit
      please help me for that
      am sorry because i didn't gave you any example or what i tried but all what tried was  failed
      thanks in advance
    • By nacerbaaziz
      hello sirs
      i've some questions about StringRegExpReplace i hope you can help me
       
      i tried to make a function that give me the host of the url and other give me the url with out host
      for example i've this link
      https://www.example.com/vb/result.php
      i need the first give me the
      example.com
      and the other give me 
      /vb/result.php
      i find that
      $s_source = "https://www.google.com/vb/index.php" Local $s_Host = StringRegExpReplace($s_Source, '.*://(.*?)/.*', '\1') Local $s_Page = StringRegExpReplace($s_source, '.*://.*?(/.*)', '\1') msgBox(64, $s_Host, $s_Page)  
      but i found some problems i need your help to correct it
      first: when i get the host if the url has www i want to remove it
      second: if the url with out host did not have other things 
      i need the result to be ""
      e.g
      https://www.example.com
      the first i want it
      example.com
      and the second i want it to be ""
      i hope that you can help me
      thanks in advance
    • By iAmNewbe
      I created an application to catch keyboard programmed keys.  Some of the keys open URL's via ShellExecute and then they open in the default browser.

      The problem I am having is that when the browser is opened in this manner 3rd party applications such as Logitech Setpoint for Logitech mouse and keyboards and programs like auto password form filling stop working with the browser.  In the mouse specific situation the Cruise Up and Cruise Down buttons, normally back and forward, do not work. If I open the browser manually via the browsers icon and then press the keyboard buttons to open the web page everything works normally.

      I have two questions:

      1:  Why does 
      ShellExecute("http://www.autoitscript.com") when the browser is not open and then is launched, via the above line, cause third party apps to not be able to interface with the browser?


      2:  What is a better way to launch URL's if not via ShellExecute?


      The application is NOT using #RequireAdmin and is not 64bit.
×
×
  • Create New...