POWERSHELL : scan nach log4j

aus gegebenen anlass hier ein kleines Powershell wie man nach jar files schauen kann. Das Array mit den Pfaden kann beliebig erweitert werden.

$SearchPath = @(${env:ProgramFiles} , ${env:ProgramFiles(x86)} , ${env:JAVA_HOME} , 'D:\Program Files' , 'D:\Program Files (86)' , 'D:\Tools')
$FilesFound = @()
$FileSearch = 'log4*.jar'
foreach ($Path in $SearchPath)
{
    $FilesFound += (Get-ChildItem -Path ${Path} -Filter ${FileSearch} -Recurse -ErrorAction SilentlyContinue)
}
if ($FilesFound) 
{
Write-Output ('{0} found on system , please check files' -f ${FileSearch})
Write-Output '------------------------------'
$FilesFound
}
else
{
Write-Output ('{0} not found on system' -f ${FileSearch})
}


Hier gibts das alles nochmal in Professionell ;-)
https://hochwald.net/post/powershell-based-log4j-vulnerabilities-scanner/


https://logging.apache.org/log4j/1.2/
https://www.lunasec.io/docs/blog/log4j-zero-day/
https://www.randori.com/blog/cve-2021-44228/
https://www.bsi.bund.de/DE/Service-Navi/Presse/Pressemitteilungen/Presse2021/211211_log4Shell_WarnstufeRot.html

Hier gibts noch einen Scanner für das Problem :
https://github.com/hillu/local-log4j-vuln-scanner

Windows : pslogrotate.ps1 um Logfiles zu rotieren

Wir haben hier einige Anwendungen unter Windows im Einsatz die unsere Platten zuschreiben mit Logfiles. Aus meinen Linux Systemen kenne ich logrotate und pslogrotate ist eine light Version von diesem.

pslogrotate baut auf powershell 5 auf und ist mit einem json einfach zu configurieren. Definieren kann mal die Vorhaltezeit der Logs in Tagen und ob die Logs komprimiert werden sollen. Auch der Dienst kann auf restart gesetzt werden. Das bedeutet mein Script beendet den Dienst roliert das Logfile und startet den Dienst wieder.

Gerne bin ich für Feature Requests offen , diese dann bitte in Github direkt stellen.

Hier gehts zu pslogrotate.ps1 : https://github.com/Mokkujin/powershell/tree/main/pslogrotate

vault-ssh-otp unter windows ( ohne sshpass )

Vault ist ein Passwort Manager und mehr für den professionellen Einsatz. Die Sicherheitsfeatures sind beeindruckend und das alles in einem OpenSource Tool das keinen Cent kostet. Unter anderem bietet das Tool die Möglichkeit OneTimePasswords (otp) zu erstellen. Unter Linux sind die otp einfach zu nutzen unter Windows leider nicht ganz so einfach.

Um eine ähnlich komfortabele Lösung für Windows Benutzer zu bieten habe ich hier ein kleines Powershell Skript geschrieben.
Es wird putty vorausgesetzt anderenfalls umschreiben ;-)

Hier gehts zu Vault von Hashicorp : https://www.vaultproject.io/

Eine Anmeldung mit einem Token sieht dann so aus :
vault-ssh-windows token
Eine Anmeldung mit user/pass ist auch möglich:
vault-ssh-windows userpass


# vault-ssh.ps1 for windows tested on Powershell 5.1
# you need an puuty installation, NOT ONLY THE EXE FILE !
# read parameter
param (
    [string]$VR,
    [string]$VU,
    [string]$VI,
    [string]$VUU,
    [string]$LM="t",
    [int]$d=0
)
# function
function show_help {
  Write-Host ""
  Write-Host "-VR Vault Role"
  Write-Host "-VU User for target host"
  Write-Host "-VI IP address for target host"
  Write-Host "-LM define the login method ( use u / t for userpass / token default ist token)"
  Write-Host "      if you use userpass -VUU is required"
  Write-Host "        -VUU vault username"
  Write-Host "-d 1 activate debug mode (default is -d 0)"
  Write-Host ""
  Write-Host "examples :"
  Write-Host "---------------------------------------------------------------"
  Write-Host "                            TOKEN"
  Write-Host "---------------------------------------------------------------"
  write-Host "vault-ssh.ps1 -VR admin -VU root -VI 192.168.2.1"
  Write-Host "---------------------------------------------------------------"
  Write-Host "                            USERPASS"
  Write-Host "---------------------------------------------------------------"
  write-Host "vault-ssh.ps1 -VR admin -VU root -VI 192.168.2.1 -LM u -VUU YOUR_USERNAME"
  Write-Host " "
  
  exit 0
}
# check parameter
if ([string]::IsNullOrWhitespace($VR) -or [string]::IsNullOrWhitespace($VU) -or [string]::IsNullOrWhitespace($VI)) {
  Write-Host -ForegroundColor Yellow "not enough parameter !"
  show_help
}
# get vault address
try {
    $VAULT_SRV=$env:VAULT_ADDR
} catch {
    Write-Host -ForegroundColor Red "Environment Variable VAULT_ADDR not found please set the Enviroment with 'setx VAULT_ADDR ADDRESS_TO_VAULT_HA'"
    exit 99
}
# get vault master #
try {
  # /sys/leader
  $VAULT_LEADER=Invoke-RestMethod -Method Get -Uri "$VAULT_SRV/v1/sys/leader" | ConvertTo-Json | ConvertFrom-Json
  [string]$VAULT_LEADER_ADDR=$VAULT_LEADER.leader_address
  # debug output
  if ( $d -eq 1 ) { Write-Host -ForegroundColor Yellow "Leader : $VAULT_LEADER_ADDR " }
  if ( $VAULT_LEADER_ADDR -ne $VAULT_SRV ) {
    # debug output
    if ( $d -eq 1 ) { Write-Host -ForegroundColor Red "$VAULT_SRV is not the leader connect to $VAULT_LEADER_ADDR instead" $msg }
    $VAULT_SRV=$VAULT_LEADER_ADDR
  } else {
    # debug output
    if ( $d -eq 1 ) { Write-Host -ForegroundColor Yellow "VAULT_ADDR is the leader address" }
  }
} catch {
  Write-Host -ForegroundColor Red "could not get vault master !"
  Exit 99
} 
# check vault health 
try {
    $VAULT_REQ=Invoke-WebRequest -Uri "$VAULT_SRV/v1/sys/health" -Method GET -UseBasicParsing
    $VAULT_STATUS=$VAULT_REQ.StatusCode
    switch ($VAULT_STATUS) {
        # check status code 
        200 { 
              $msg="VAULT is initialized, unsealed, and active"
              $vok="Green"
            }
        429 { 
              $msg="VAULT is unsealed and standby"
              $vok="Yellow"
            }
        472 { 
              $msg="VAULT is in data recovery mode replication secondary and active"
              $vok="Yellow"
            }
        473 { 
              $msg="VAULT is in performance standby"
              $vok="Yellow"
            }
        501 { $msg="VAULT is not initialized"
              $vok="Red"
            }
        503 { 
              $msg="VAULT ist sealed"
              $vok="Red"
            }
    }
} catch {
    Write-Host -ForegroundColor Red "Could not check vault status ! is the address correct ? $VAULT_SRV"
    Exit 97
}
# debug output 
if ( $d -eq 1 ) { Write-Host -ForegroundColor $vok $msg }
switch ($LM) {
  "t" {
      # get vault-token 
      try {
        $VAULT_TOKEN=$env:VAULT_TOKEN
        if ([string]::IsNullOrWhitespace($VAULT_TOKEN)) {
          # if no enviroment set ask for token
          $VAULT_TOKEN=Read-Host -AsSecureString "Need your vault token ! "
          }
        } catch {
          Write-Host -ForegroundColor Red "an error occured - vault token"
          Exit 98
        }
        # no token no cookies 
        if ([string]::IsNullOrWhitespace([System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($VAULT_TOKEN)))) { Write-Host -Foreground Red "no token given ... how do you think does an authentification work ?" ; exit 90}
        # get ssh otp from vault 
        $vhead = @{
          'X-Vault-Token' = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($VAULT_TOKEN))
        }
        $VAULT_OTP_REQ=Invoke-RestMethod -Uri $VAULT_SRV/v1/ssh/creds/$VR -Method Post -headers $vhead -Body "{ `"ip`": `"$VI`" }" | ConvertTo-Json | ConvertFrom-Json
        $VAULT_OTP=$VAULT_OTP_REQ.data.key
        # run putty 
        try {
          putty.exe -ssh $VU@$VI -pw $VAULT_OTP
        } catch {
          Write-Host -ForegroundColor Red "putty.exe not find please define the path in enviroment variables"
          Exit 96
        }
      }
    "u" {
      $VUP=Read-Host -AsSecureString "please enter you vault password "
      $VUP_E = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($VUP))
      try {
        # login vault 
        $VLogIn=Invoke-RestMethod -Uri $VAULT_SRV/v1/auth/userpass/login/$VUU -Method Post -Body "{ `"username`": `"$VUU`" , `"password`": `"$VUP_E`"}" | ConvertTo-Json | ConvertFrom-Json
      } catch {
        Write-Host -ForegroundColor Red "Wrong Password ?"
        Exit 92
      }
      $vhead = @{
        # set token to client_token get from login 
        'X-Vault-Token' = $VLogIn.auth.client_token
      }
      try {
        # get otp password 
        $VAULT_OTP_REQ=Invoke-RestMethod -Uri $VAULT_SRV/v1/ssh/creds/$VR -Method Post -headers $vhead -Body "{ `"ip`": `"$VI`" }" | ConvertTo-Json | ConvertFrom-Json
      } catch {
        Write-Host -ForegroundColor Red "could not get client_token"
        Exit 93
      }
      $VAULT_OTP=$VAULT_OTP_REQ.data.key
       # run putty 
       try {
        putty.exe -ssh $VU@$VI -pw $VAULT_OTP
       } catch {
        Write-Host -ForegroundColor Red "putty.exe not find please define the path in enviroment variables"
        Exit 96
      }
  }
}


Quellen :
https://www.hashicorp.com/
“Das einzig sichere System müsste ausgeschaltet, in einem versiegelten und von Stahlbeton ummantelten Raum und von bewaffneten Schutztruppen umstellt sein.”
Gene Spafford (Sicherheitsexperte)