1
0
-1

Hi,

I am currently testing the Open-Audit application within the FirstWave Virtual Machine. My goal is to use PowerShell to interact with its API. The server's IP address is 10.10.10.10. Unfortunately, I couldn't establish API access using the default admin user.

The documentation mentions that the API uses cookies and can be accessed by making a POST request to the specified URL. I haven't been able to achieve this yet. Has anyone had prior experience with this? I can successfully log in using Postman with the header and body values provided in the documentation. However, I'm unable to log in using the PowerShell script generated by Postman below.


$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept""application/json")
$headers.Add("Cookie""omk=eyJhdXRoX2RhdGEiOiJhZG1pbiIsImV4cGlyZXMiOjE2OTQ4MDE0MTR9--d2a401a37a0b1a2e0b03a6e083a8097497838ae2")



$multipartContent = [System.Net.Http.MultipartFormDataContent]::new()
$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "username"
$stringContent = [System.Net.Http.StringContent]::new("admin")
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)

$stringHeader = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
$stringHeader.Name = "password"
$stringContent = [System.Net.Http.StringContent]::new("password")
$stringContent.Headers.ContentDisposition = $stringHeader
$multipartContent.Add($stringContent)

$body = $multipartContent

$response = Invoke-RestMethod 'http://10.10.10.10/omk/open-audit/login' -Method 'POST' -Headers $headers -Body $body
$response | ConvertTo-Json

Error message is like this;


Invoke-RestMethod : {"error":1,"message":"Authentication failed!"}
At line:22 char:13
+ $response = Invoke-RestMethod 'http://10.10.10.10/omk/open-audit/login ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Best Regards,


    CommentAdd your comment...

    2 answers

    1.  
      1
      0
      -1

      Just looking at your two scripts (one for login and the other for device list) the cookie names are different. You need to re-use the cookie you receive after logging in for subsequent requests.

      1. Erhan Arda

        Hello,
        I believe I've finally managed to achieve something with PowerShell. Perhaps it could be useful for others as well, so I wanted to share a working script here. Thank you.

        # Authentication
        $authUri = 'http://10.10.10.10/omk/open-audit/login'
        $authHeaders = @{
            'Accept' = 'application/json'
        }
        $authBody = @{
            'username' = 'admin'
            'password' = 'password'
        }
        
        # Create a session variable to store cookies
        $session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
        
        try {
            # Send the authentication request and store cookies in the session
            Invoke-RestMethod -Uri $authUri -Method Post -Headers $authHeaders -Body $authBody -WebSession $session
        
            # Retrieve and display cookies
            $cookies = $session.Cookies.GetCookies($authUri)
            
            if ($cookies.Count -gt 0) {
                Write-Host "Authentication successful. Cookies obtained:"
                foreach ($cookie in $cookies) {
                    Write-Host "Name: $($cookie.Name), Value: $($cookie.Value)"
                }
            } else {
                Write-Host "Authentication failed. No cookies obtained."
            }
        }
        catch {
            Write-Host "An error occurred during authentication: $($_.Exception.Message)"
        }
        
        # Define the URL for the device list
        $deviceListUri = 'http://10.10.10.10/omk/open-audit/devices'
        
        try {
            # Send a GET request to the device list URL using the existing session
            $deviceListResponse = Invoke-RestMethod -Uri $deviceListUri -Method Get -WebSession $session
        
            # Process the device list response (you can adjust this part as needed)
            if ($deviceListResponse) {
                # Assuming the response is in JSON format, you can parse it
                $deviceList = $deviceListResponse | ConvertFrom-Json
        
                # Now you can work with the $deviceList data
                # For example, you can loop through the devices and display their properties
                foreach ($device in $deviceList) {
                    Write-Host "Device Name: $($device.Name)"
                    Write-Host "Device ID: $($device.Id)"
                    # Add more properties as needed
                }
            } else {
                Write-Host "Device list response is empty or invalid."
            }
        }
        catch {
            Write-Host "An error occurred while retrieving the device list: $($_.Exception.Message)"
        }
        
        # Define the file path where you want to save the output
        $filePath = "C:\Out\output.txt"
        
        try {
            # Convert the hashtable to JSON format and write it to the file
            $jsonContent = $deviceListResponse.data | ConvertTo-Json -Depth 10
            $jsonContent | Set-Content -Path $filePath -Force
        }
        catch {
            Write-Host "An error occurred while writing to the file: $($_.Exception.Message)"
        }



      2. Mark Unwin

        Thanks so much for posting your solution. I'm sure others will benefit.

      CommentAdd your comment...
    2.  
      1
      0
      -1

      Hello again,


      I've successfully obtained the "Authenticated as user admin" message using the following code:

      $uri = 'http://10.10.10.10/omk/open-audit/login'
      $headers = @{
          'Accept' = 'application/json'
          'Cookie' = 'omk=eyJhdXRoX2RhdGEiOiJhZG1pbiIsImV4cGlyZXMiOjE2OTQ4MDIwNjF9--d9dd534b9a25a7ec33858c43089ce65f4f0f1d10'
      }
      $body = @{
          'username' = 'admin'
          'password' = 'password'
      }

      $response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $body

      if ($response -ne $null) {
          Write-Host $response
      } else {
          Write-Host "Request failed."
      }


      However, when I attempt to retrieve the Devices list using the following code with a GET request:


      $uri = 'http://10.10.10.10/omk/open-audit/devices'
      $headers = @{
          'Accept' = 'application/json'
          'Cookie' = 'omk=eyJhdXRoX2RhdGEiOiJhZG1pbiIsImV4cGlyZXMiOjE2OTQ4MDIyMzJ9--0afb8775ad9e1b5b7672069cf1b8c2fce9045179'
      }

      $response = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers

      if ($response -ne $null) {
          Write-Host $response
      } else {
          Write-Host "Request failed."
      }


      I still receive the error message: {"error":1,"message":"Not Authenticated, please login"}.

      Any assistance or insights into resolving this authentication issue would be greatly appreciated.

      Thank you!

        CommentAdd your comment...