Invoke-WebRequest -Login

This topic contains 5 replies, has 2 voices, and was last updated by  Curtis Smith 1 month ago.

  • Author
    Posts
  • #77674

    Wei-Yen Tan
    Participant

    I am trying to loginto a web application called Thruk.. it is using the OMD version.

    I am using invoke-webrequest to use a websession to try and connect to a local thruk omd instance. I am trying to correlate the forms the best i can from the examples but when I try and submit the web session the output just keeps going back to the login page.

    This is the following code I run:

    
    add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
                return true;
            }
     }
    "@
    [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
    
    
    $login = Invoke-WebRequest "https://icinga/bcmonitoring/thruk/cgi-bin/login.cgi?bcmonitoring/thruk/" -SessionVariable fb
    $login.Forms[0].Fields.loginuser = $cred.UserName
    $login.Forms[0].Fields.password = $cred.GetNetworkCredential().Password
    $UAstring = "Firefox"
    $mainPage = Invoke-WebRequest ("https://icinga/bcmonitoring/thruk/cgi-bin/login.cgi?bcmonitoring/thruk/") -WebSession $fb -Body $login -Method Post -UserAgent $UAstring 
    

    This is the output:

    The above is just an excerpt. Any help will be most appreciated...

  • #77676

    Curtis Smith
    Participant

    Here's what I came up with using the Thrunk Demo page.

    #Initialize Session
    $R=Invoke-WebRequest https://demo.thruk.org/thruk/cgi-bin/login.cgi -SessionVariable thrunksession
    
    #Set Login and Password
    $R.Forms[0].Fields["login"]="test"
    $R.Forms[0].Fields["password"]="test"
    
    #Authenticate
    $R=Invoke-WebRequest -Uri ("https://demo.thruk.org/thruk/cgi-bin/" + $R.Forms[0].Action) -WebSession $thrunksession -Method POST -Body $R.Forms[0].Fields
    $R.ParsedHtml.body.innerHTML
    
    #Logout
    $R=Invoke-WebRequest -Uri ("https://demo.thruk.org/thruk/cgi-bin/login.cgi?logout") -WebSession $thrunksession -Method POST
    $R.ParsedHtml.body.innerText
  • #77683

    Wei-Yen Tan
    Participant

    Thanks @Curtis!

    I compared and contrasted your code and saw that the difference that you had was

    $R.Forms[0].Fields["login"]="test"

    where I put it as loginuser

    Thats because that is what i see in the output:

    
    
    Id Method Action    Fields                                                     
    -- ------ ------    ------                                                     
       post   login.cgi {[referer, ], [loginuser, ], [password, ], [submit, Login]}
    
    

    I am curious why is that powershell understood login instead of login user. Is this how it is designed?

  • #77691

    Curtis Smith
    Participant

    loginuser is the ID of the field, but the name of the filed is login.

  • #77692

    Wei-Yen Tan
    Participant

    Oh, so i am assuming you were looking at the inputfields property?

    innerHTML : 
    innerText : 
    outerHTML : 
    outerText : 
    tagName   : INPUT
    id        : loginuser
    class     : logininput
    name      : login
    
  • #77709

    Curtis Smith
    Participant

    I actually used the developer tools of IE and/or Chrome to look at the form html on the page.

You must be logged in to reply to this topic.