Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjusted script to work better with Tamper Protection #109

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions Powershell/InstallHuntress.powershellv2.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2023 Huntress Labs, Inc.
# Copyright (c) 2024 Huntress Labs, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -70,7 +70,7 @@
##############################################################################

# These are used by the Huntress support team when troubleshooting.
$ScriptVersion = "Version 2, major revision 7, 2024 April 16"
$ScriptVersion = "Version 2, major revision 7, 2024 Dec 26"
$ScriptType = "PowerShell"

# variables used throughout this script
Expand Down Expand Up @@ -456,8 +456,12 @@
# if Huntress was installed before this script started and Rio is missing then we log that, but continue with this script
if ($svc -eq $HuntressEDRServiceName) {
if ($isHuntressInstalled) {
LogMessage "ERROR: The $svc service is not installed. You may need to wait 20 minutes, reboot, or reinstall the agent (if this machine is indeed Huntress EDR compatible)"
LogMessage "See more about compatibility here: https://support.huntress.io/hc/en-us/articles/4410699983891-Supported-Operating-Systems-System-Requirements-Compatibility"
if ($svc -eq "HuntressRio") {
LogMessage "Warning: Huntress Process Insights (aka Rio) is installed automatically by the Huntress portal. It can take up to 24 hours to show up"
LogMessage "See more about compatibility here: https://support.huntress.io/hc/en-us/articles/4410699983891-Supported-Operating-Systems-System-Requirements-Compatibility"
} else {
LogMessage "ERROR: The $svc service is not installed. You may need to reboot or reinstall the agent - if these fail send HuntressAgent.log and HuntressPoShInstaller.log to Huntress support"
}
} else {
LogMessage "New install detected. It may take 24 hours for Huntress EDR (Rio) to install!"
}
Expand Down Expand Up @@ -550,6 +554,7 @@
$linesFromLog = Get-Content $Path | Select-Object -last 10
ForEach ($line in $linesFromLog) {
if ($line -like "*bad status code: 401*") {
LogMessage "Agent appears to be orphaned: $($line)"
return $true
}
}
Expand Down Expand Up @@ -692,6 +697,17 @@
} else {
LogMessage "No registry keys found, uninstallation complete"
}

# if Huntress services still exist, then delete
$services = @($(Get-WmiObject -Class Win32_Service -Filter "Name='HuntressRio'"),
Fixed Show fixed Hide fixed
$(Get-WmiObject -Class Win32_Service -Filter "Name='HuntressAgent'"),
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
$(Get-WmiObject -Class Win32_Service -Filter "Name='HuntressUpdater'"))
Fixed Show fixed Hide fixed
foreach ($service in $services) {
if ( $service ) {
LogMessage "Service $($service.Name) detected post uninstall, attempting to remove"
$service.delete() 2>$null
}
}
}

# grab the currently installed agent version AB
Expand Down Expand Up @@ -809,10 +825,16 @@
LogMessage "Script type: '$ScriptType'"
LogMessage "Script version: '$ScriptVersion'"

# if Huntress was already installed, pull version info
# if Huntress was already installed, pull version info and TP status
LogMessage "Script cursory check, is Huntress installed already: $($isHuntressInstalled)"
if ($isHuntressInstalled){
LogMessage "Agent version $(getAgentVersion) found"
$checkTP = (Get-Service "HuntressAgent").ServiceHandle
if ( $checkTP -eq $NULL) {

Check notice

Code scanning / Psscriptanalyzer (reported by Codacy)

$null should be on the left side of equality comparisons. Note

$null should be on the left side of equality comparisons.
LogMessage "Warning: Tamper Protection detected, you may need to disable TP or run this as SYSTEM to repair, upgrade, or reinstall this agent. `n"
} else {
LogMessage "Pass: Tamper Protection not detected, or this script is running as SYSTEM `n"
}
}

# Log OS details
Expand Down Expand Up @@ -843,7 +865,14 @@
checkFreeDiskSpace
LogMessage "Installer location: '$InstallerPath'"
LogMessage "Installer log: '$DebugLog'"

LogMessage "Administrator access: $(testAdministrator)"
$userContext = whoami
if ($userContext -eq "nt authority\system") {
LogMessage "Pass: Run under the SYSTEM user."
} else {
LogMessage "Warning: Not run under the SYSTEM user, you may have issues with Huntress Tamper Protection"
}

# Log machine uptime
try
Expand Down Expand Up @@ -929,7 +958,8 @@

# if the agent is orphaned, switch to the full uninstall/reinstall (reregister flag)
if ( !($reregister)) {
if (isOrphan) {
$orphanStatus = isOrphan
if ( $orphanStatus -eq $true ) {
$err = 'Huntress Agent is orphaned, unable to use the provided flag. Switching to uninstall/reinstall (reregister flag)'
LogMessage "$err"
$reregister = $true
Expand Down Expand Up @@ -991,11 +1021,10 @@
}
StopHuntressServices
} else {
LogMessage "Checking for HuntressAgent service..."
if ( Confirm-ServiceExists($HuntressAgentServiceName) ) {
$err = "The Huntress Agent is already installed. Exiting with no changes. Suggest using -reregister or -reinstall flags"
LogMessage "$err"
LogMessage 'Huntress Agent is already installed. Suggest using the -reregister or -reinstall flags'
LogMessage "Checking for HuntressAgent install..."
$agentPath = getAgentPath
if ( (Test-Path $agentPath) -eq $true) {
LogMessage "The Huntress Agent is already installed in $($agentPath). Exiting with no changes. Suggest using -reregister or -reinstall flags"
copyLogAndExit
}
}
Expand Down
Loading