Introduction
Pour les professionnels de l’informatique et les fournisseurs de services gérés (MSP), il est essentiel de conserver des informations précises sur l’état de l’antivirus sur les appareils. Le suivi des versions, des états et des statuts de mise à jour des antivirus garantit l’application des politiques de sécurité et la protection des systèmes. Pour les environnements Linux, l’automatisation peut simplifier ce processus. Cet article examine un script Bash conçu pour gérer les exclusions antivirus sur les périphériques, offrant aux équipes informatiques un outil pratique pour maintenir leurs informations de sécurité sur les terminaux.
Contexte
Dans les environnements informatiques, en particulier pour les entreprises MSP qui gèrent divers parcs d’appareils, le statut de l’antivirus est un indicateur clé. Cependant, des divergences apparaissent souvent entre l’état réel du logiciel antivirus sur un appareil et la façon dont il apparaît dans les outils de contrôle. Ce script permet aux professionnels de l’informatique de définir ou de mettre à jour manuellement les informations relatives à l’antivirus, notamment son nom, sa version, son état et son statut de mise à jour. En permettant la personnalisation ou la suppression des exclusions, il rationalise la gestion de la sécurité des terminaux dans les environnements qui s’appuient sur des outils tels que NinjaOne.
Le script :
#!/usr/bin/env bash
#
# Description: Add an antivirus to the device details or override the existing antivirus information.
# By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use.
# Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms.
# Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party.
# Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider.
# Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations.
# Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks.
# Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script.
# EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA).
#
# Preset Parameter: --antivirusName "ReplaceMeWithYourDesiredName"
# Name of the antivirus you would like to appear in the device details.
#
# Preset Parameter: --antivirusVersion "1.0.2"
# Specify the version number of the antivirus.
#
# Preset Parameter: --antivirusStatus "Up-to-Date"
# Specify whether the antivirus definitions are Up-to-Date, Out-of-Date, or Unknown.
#
# Preset Parameter: --antivirusState "ON"
# Specify the current status of the antivirus.
#
# Preset Parameter: --removeOverride
# Remove all existing overrides.
#
# Preset Parameter: --antivirusState
# Append or update an existing override.
#
# Preset Parameter: --help
# Displays a help menu.
#
# Release Notes: Initial Release
# Initialize variables
_arg_antivirusName=
_arg_antivirusVersion=
_arg_antivirusStatus=
_arg_antivirusState=
_arg_removeOverride="off"
_arg_append="off"
# Function to display help message
print_help() {
printf '\n\n%s\n\n' 'Usage: [--antivirusName|-n <arg>] [--antivirusVersion|-v <arg>] [--antivirusStatus|--status <arg>] [--antivirusState|--state <arg>] [--removeOverride|-r] [--append|-a] [--help|-h] '
printf '%s\n' 'Preset Parameter: --antivirusName "ReplaceMeWithYourDesiredName"'
printf '\t%s\n' "Name of the antivirus you would like to appear in the device details."
printf '%s\n' 'Preset Parameter: --antivirusVersion "1.0.2"'
printf '\t%s\n' "Specify the version number of the antivirus."
printf '%s\n' 'Preset Parameter: --antivirusStatus "Up-to-Date"'
printf '\t%s\n' "Specify whether the antivirus definitions are Up-to-Date, Out-of-Date, or Unknown."
printf '%s\n' 'Preset Parameter: --antivirusState "ON"'
printf '\t%s\n' "Specify the current status of the antivirus."
printf '%s\n' 'Preset Parameter: --removeOverride'
printf '\t%s\n' "Remove all existing overrides."
printf '%s\n' 'Preset Parameter: --antivirusState'
printf '\t%s\n' "Append or update an existing override."
printf '\n%s\n' 'Preset Parameter: --help'
printf '\t%s\n' "Displays this help menu."
}
# Function to print an error message and exit
die() {
local _ret="${2:-1}"
echo "$1" >&2
test "${_PRINT_HELP:-no}" = yes && print_help >&2
exit "${_ret}"
}
# Function to parse command line arguments
parse_commandline() {
while test $# -gt 0; do
_key="$1"
case "$_key" in
--antivirusName | --antivirusname | --name | -n)
test $# -lt 2 && die "Missing value for the argument '$_key'." 1
_arg_antivirusName=$2
shift
;;
--antivirusName=*)
_arg_antivirusName="${_key##--antivirusName=}"
;;
--antivirusVersion | --antivirusversion | --version | -v)
test $# -lt 2 && die "Missing value for the argument '$_key'." 1
_arg_antivirusVersion=$2
shift
;;
--antivirusVersion=*)
_arg_antivirusVersion="${_key##--antivirusVersion=}"
;;
--antivirusStatus | --antivirusstatus | --status)
test $# -lt 2 && die "Missing value for the argument '$_key'." 1
_arg_antivirusStatus=$2
shift
;;
--antivirusStatus=*)
_arg_antivirusStatus="${_key##--antivirusStatus=}"
;;
--antivirusState | --antivirusstate | --state)
test $# -lt 2 && die "Missing value for the argument '$_key'." 1
_arg_antivirusState=$2
shift
;;
--antivirusState=*)
_arg_antivirusState="${_key##--antivirusState=}"
;;
--removeOverride | --remove | -r)
_arg_removeOverride="on"
;;
--append | --Append | -a)
_arg_append="on"
;;
--help | -h)
_PRINT_HELP=yes die 0
;;
*)
_PRINT_HELP=yes die "[Error] Got an unexpected argument '$1'" 1
;;
esac
shift
done
}
# Parse the command line arguments
parse_commandline "$@"
# Replace commandline parameters with script form variables
if [[ -n $avName ]]; then
_arg_antivirusName="$avName"
fi
if [[ -n $avVersion ]]; then
_arg_antivirusVersion="$avVersion"
fi
if [[ -n $avStatus ]]; then
_arg_antivirusStatus="$avStatus"
fi
if [[ -n $avState ]]; then
_arg_antivirusState="$avState"
fi
if [[ -n $append && $append == "true" ]]; then
_arg_append="on"
fi
if [[ -n $removeOverride && $removeOverride == "true" ]]; then
_arg_removeOverride="on"
fi
# Ensure that removing an override and adding/updating an override are not done simultaneously
if [[ $_arg_removeOverride == "on" && (-n $_arg_antivirusName || -n $_arg_antivirusState || -n $_arg_antivirusStatus || -n $_arg_antivirusVersion || $_arg_append == "on") ]]; then
_PRINT_HELP=no die "[Error] Cannot remove an override and add an override at the same time." 1
fi
# Check for required antivirus name and escape special characters if necessary
reservedCharacters='[\"]'
if [[ -z $_arg_antivirusName && $_arg_removeOverride != "on" ]]; then
if [[ $_arg_append == "on" ]]; then
_PRINT_HELP=yes die "[Error] Antivirus name was not given. The antivirus name is required when updating or adding a new override!" 1
else
_PRINT_HELP=yes die "[Error] Antivirus name was not given. Antivirus name, state, and status are required when adding a new override!" 1
fi
elif [[ -n $_arg_antivirusName && $_arg_antivirusName =~ $reservedCharacters ]]; then
_arg_antivirusName=${_arg_antivirusName//\\/\\\\}
_arg_antivirusName=${_arg_antivirusName//\"/\\\"}
fi
# Check for required antivirus status
if [[ -z $_arg_antivirusStatus && $_arg_removeOverride != "on" && $_arg_append != "on" ]]; then
_PRINT_HELP=yes die "[Error] Antivirus status was not given. Antivirus name, state, and status are required!" 1
fi
# Validate antivirus status
if [[ -n $_arg_antivirusStatus && $_arg_antivirusStatus != "Up-to-Date" && $_arg_antivirusStatus != "Out-of-Date" && $_arg_antivirusStatus != "Unknown" ]]; then
_PRINT_HELP=no die "[Error] An invalid antivirus status of '$_arg_antivirusStatus' was given. Only the following statuses are valid. 'Up-to-Date', 'Out-of-Date', and 'Unknown'." 1
fi
# Check for required antivirus state
if [[ -z $_arg_antivirusState && $_arg_removeOverride != "on" && $_arg_append != "on" ]]; then
_PRINT_HELP=yes die "[Error] Antivirus state was not given. Antivirus name, state, and status are required!" 1
else
_arg_antivirusState=$(echo "$_arg_antivirusState" | tr '[:lower:]' '[:upper:]')
fi
# Validate antivirus state
if [[ -n $_arg_antivirusState && $_arg_antivirusState != "ON" && $_arg_antivirusState != "OFF" && $_arg_antivirusState != "EXPIRED" && $_arg_antivirusState != "SNOOZED" && $_arg_antivirusState != "UNKNOWN" ]]; then
_PRINT_HELP=no die "[Error] An invalid antivirus state of '$_arg_antivirusState' was given. Only the following states are valid. 'ON', 'OFF', 'EXPIRED', 'SNOOZED', and 'UNKNOWN'." 1
fi
# Validate antivirus version
if [[ -n $_arg_antivirusVersion && $_arg_antivirusVersion =~ [^0-9\.] ]]; then
_PRINT_HELP=no die "[Error] The antivirus version given '$_arg_antivirusVersion' contains an invalid character. Only the following characters are allowed. '0-9' and '.'" 1
fi
# Check if the removeOverride flag is set to "on"
if [[ $_arg_removeOverride == "on" ]]; then
echo "Removing override as requested."
# Check if the override file exists
if [[ ! -f "/opt/NinjaRMMAgent/programdata/customization/av_override.json" ]]; then
echo "No override present."
exit 0
# Try to remove the override file and capture any error output
elif output=$(rm "/opt/NinjaRMMAgent/programdata/customization/av_override.json" 2>&1); then
echo "Succesfully removed override!"
exit 0
# Print an error message if the removal fails
else
echo "[Error] Failed to remove override!"
echo "[Error] $output"
fi
fi
# Check if the customization directory exists
if [[ ! -d "/opt/NinjaRMMAgent/programdata/customization" ]]; then
echo "Creating customization folder at '/opt/NinjaRMMAgent/programdata/customization'."
# Try to create the customization directory and capture any error output
if output=$(mkdir "/opt/NinjaRMMAgent/programdata/customization" 2>&1); then
echo "Folder created."
else
# Print an error message if the creation fails
echo "[Error] Unable to create customization folder." >&2
echo "[Error] $output" >&2
exit 1
fi
fi
# Check if the append flag is set to "on" and the override JSON file exists
if [[ $_arg_append == "on" && -f "/opt/NinjaRMMAgent/programdata/customization/av_override.json" ]]; then
# Extract antivirus names, versions, statuses and states from the JSON file
avNames=$(grep "av_name" "/opt/NinjaRMMAgent/programdata/customization/av_override.json" | tr -s " " | sed s/\"av_name\"://g | sed -e 's/^[[:space:]]*//' | sed 's/[\",]//g' | sed -e 's/[[:space:]]*$//')
avVersions=$(grep "av_version" "/opt/NinjaRMMAgent/programdata/customization/av_override.json" | tr -s " " | sed s/\"av_version\"://g | sed -e 's/^[[:space:]]*//' | sed 's/[\",]//g' | sed -e 's/[[:space:]]*$//')
avStatuses=$(grep "av_status" "/opt/NinjaRMMAgent/programdata/customization/av_override.json" | tr -s " " | sed s/\"av_status\"://g | sed -e 's/^[[:space:]]*//' | sed 's/[\",]//g' | sed -e 's/[[:space:]]*$//')
avStates=$(grep "av_state" "/opt/NinjaRMMAgent/programdata/customization/av_override.json" | tr -s " " | sed s/\"av_state\"://g | sed -e 's/^[[:space:]]*//' | sed 's/[\",]//g' | sed -e 's/[[:space:]]*$//')
# Find the line number of the existing antivirus entry with the given name
existingAV=$(echo "$avNames" | grep -n "$_arg_antivirusName" | sed 's/:.*//g')
# Determine the desired antivirus status
if [[ -n $_arg_antivirusStatus ]]; then
desiredStatus=$_arg_antivirusStatus
elif [[ -n $existingAV ]]; then
desiredStatus=$(echo "$avStatuses" | sed -n "${existingAV}p")
fi
# Determine the desired antivirus state
if [[ -n $_arg_antivirusState ]]; then
desiredState=$_arg_antivirusState
elif [[ -n $existingAV ]]; then
desiredState=$(echo "$avStates" | sed -n "${existingAV}p")
fi
# Check if both status and state are provided
if [[ -z $desiredStatus || -z $desiredState ]]; then
_PRINT_HELP=no die "[Error] Antivirus state or status are missing from the override entry. Please provide both in addition to the antivirus name!" 1
fi
# Update the existing antivirus entry if found
if [[ -n $existingAV ]]; then
echo "Attempting to update override."
# Update antivirus version if provided
if [[ -n $_arg_antivirusVersion ]]; then
modified_json=$(awk -v target="$existingAV" -v value="$_arg_antivirusVersion" 'BEGIN { av_count = 1 }
/av_version/ {
if (av_count == target){
sub( /av_version.*/ , "av_version\": \""value"\"," )
}
av_count++
}{ print }' "/opt/NinjaRMMAgent/programdata/customization/av_override.json")
if echo "$modified_json" >"/opt/NinjaRMMAgent/programdata/customization/av_override.json"; then
echo "Successfully updated the antivirus version!"
else
echo "[Error] Failed to update the antivirus version!" >&2
exit 1
fi
fi
# Update antivirus status if provided
if [[ -n $_arg_antivirusStatus ]]; then
modified_json=$(awk -v target="$existingAV" -v value="$_arg_antivirusStatus" 'BEGIN { av_count = 1 }
/av_status/ {
if (av_count == target){
sub( /av_status.*/ , "av_status\": \""value"\"," )
}
av_count++
}{print}' "/opt/NinjaRMMAgent/programdata/customization/av_override.json")
if echo "$modified_json" >"/opt/NinjaRMMAgent/programdata/customization/av_override.json"; then
echo "Successfully updated the override status!"
else
echo "[Error] Failed to update the override status!" >&2
exit 1
fi
fi
# Update antivirus state if provided
if [[ -n $_arg_antivirusState ]]; then
modified_json=$(awk -v target="$existingAV" -v value="$_arg_antivirusState" 'BEGIN { av_count = 1 }
/av_state/ {
if (av_count == target){
sub( /av_state.*/ , "av_state\": \""value"\"" )
}
av_count++
}{print}' "/opt/NinjaRMMAgent/programdata/customization/av_override.json")
if echo "$modified_json" >"/opt/NinjaRMMAgent/programdata/customization/av_override.json"; then
echo "Successfully updated the override state!"
else
echo "[Error] Failed to update the override state!" >&2
exit 1
fi
fi
exit
fi
# Print a message indicating that the script is attempting to append an override
echo "Attempting to append override."
# Initialize a counter for indexing
i=1
# Initialize the JSON structure for antivirus overrides
avOverrides="{
\"av_override\": [
"
# Loop through each antivirus name
for avName in $avNames; do
# Extract the corresponding antivirus version, status and state for the current index
avVersion=$(echo "$avVersions" | sed -n "${i}p")
avStatus=$(echo "$avStatuses" | sed -n "${i}p")
avState=$(echo "$avStates" | sed -n "${i}p")
# Append the current antivirus entry to the JSON structure
avOverrides+=" {
\"av_name\": \"${avName}\",
\"av_version\": \"${avVersion}\",
\"av_status\": \"${avStatus}\",
\"av_state\": \"${avState}\"
},
"
# Increment the counter for the next iteration
i=$((i + 1))
done
# Close the JSON structure
avOverrides+=" {
\"av_name\": \"${_arg_antivirusName}\",
\"av_version\": \"${_arg_antivirusVersion}\",
\"av_status\": \"${_arg_antivirusStatus}\",
\"av_state\": \"${_arg_antivirusState}\"
}
]
}
"
# Attempt to write the JSON structure containing antivirus overrides to the specified file
if echo "$avOverrides" >"/opt/NinjaRMMAgent/programdata/customization/av_override.json"; then
echo "Succesfully added override."
exit
else
echo "[Error] Failed to add override." >&2
exit 1
fi
fi
# Check if the antivirus state or status arguments are missing
if [[ -z $_arg_antivirusState || -z $_arg_antivirusStatus ]]; then
_PRINT_HELP=no die "[Error] Antivirus name, state and status are required when adding a new override!" 1
fi
# Construct the JSON string for the antivirus override
JSON_STRING="{
\"av_override\": [
{
\"av_name\": \"${_arg_antivirusName}\",
\"av_version\": \"${_arg_antivirusVersion}\",
\"av_status\": \"${_arg_antivirusStatus}\",
\"av_state\": \"${_arg_antivirusState}\"
}
]
}
"
# Attempt to write the JSON string to the specified file
if echo "$JSON_STRING" >"/opt/NinjaRMMAgent/programdata/customization/av_override.json"; then
# If the write operation is successful, print a success message
echo "Succesfully created override."
else
# If the write operation fails, print an error message to standard error
echo "[Error] Failed to create override." >&2
exit 1
fi
Gagnez du temps grâce à plus de 300 scripts du Dojo NinjaOne.
Description détaillée
Ce script automatise l’ajout ou la mise à jour des exclusions antivirus dans un fichier de configuration basé sur JSON. Voici comment cela fonctionne, étape par étape :
1. Initialisation des paramètres
a. Le script commence par définir des variables pour le nom, la version, le statut et l’état de l’antivirus. Des marqueurs comme –removeOverride et –append sont également initialisés pour gérer des actions spécifiques.
2. Analyse de la ligne de commande
a. La fonction parse_commandline traite les arguments fournis lors de l’exécution du script. Par exemple, –antivirusName « MyAntivirus » définit le nom de l’antivirus.
3. Validation
a. Le script s’assure que les paramètres obligatoires sont fournis lorsqu’ils sont requis. Il vérifie la validité des statuts (à jour, obsolète ou inconnu) et des états (activé, désactivé, expiré, remise à plus tard ou inconnu).
4. Suppression des exclusions
a. Si l’option –removeOverride est activée, le script supprime le fichier JSON existant. Si aucun fichier de ce type n’existe, il se termine de manière élégante.
5. Ajout ou mise à jour des exclusions
a. Lorsque l’option –append est utilisée, le script vérifie les entrées existantes dans le fichier JSON. Il met à jour les détails d’une entrée antivirus existante ou en ajoute une nouvelle.
6. Rédaction des exclusions
a. Si l’option –append n’est pas activée, le script écrit une nouvelle structure JSON avec les détails de l’antivirus spécifié. Cette structure est sauvegardée à un emplacement désigné.
Cas d’utilisation potentiels
Scénario : une entreprise MSP qui gère les appareils de ses clients
Imaginez qu’un fournisseur de services de gestion surveille les appareils de plusieurs clients. Une nouvelle mise à jour de l’antivirus entraîne des retards dans la synchronisation de l’état avec leur outil de surveillance. En déployant ce script sur les appareils concernés, le MSP peut temporairement remplacer le statut, en marquant l’antivirus comme « Up-to-Date » (à jour) et « ON » (activé), ce qui garantit que les rapports de conformité restent exacts jusqu’à ce que le problème soit résolu.
Comparaisons
D’autres méthodes de gestion de l’état de l’antivirus consistent à modifier manuellement le fichier JSON ou à s’appuyer sur les outils intégrés aux plateformes de surveillance et de gestion à distance (RMM). Par rapport aux éditions manuelles, ce script réduit les erreurs humaines et accélère le processus. Contrairement aux outils spécifiques à une plateforme, il offre une certaine souplesse pour les environnements Linux où de telles intégrations n’existent pas forcément.
FAQ
- Ce script peut-il fonctionner avec tous les logiciels antivirus ?
Il n’interagit pas directement avec le logiciel antivirus, mais modifie le fichier JSON contenant les exclusions, ce qui le rend indépendant des solutions antivirus spécifiques. - Que se passe-t-il si je l’exécute sans les arguments requis ?
Le script affiche des messages d’erreur détaillés et des instructions d’utilisation pour guider vers une exécution correcte. - Comment annuler les modifications apportées par ce script ?
Utilisez l’option –removeOverride pour supprimer le fichier de configuration. - Puis-je utiliser ce script sur des systèmes autres que Linux ?
Il est conçu pour les environnements Linux car il s’appuie sur Bash et les chemins de fichiers Linux.
Implications
L’utilisation efficace de ce script garantit une représentation précise de l’état des antivirus sur l’ensemble des appareils. Cela permet de réduire les fausses alertes dans la surveillance de la sécurité, ce qui renforce la cybersécurité de l’entreprise. Toutefois, une utilisation incorrecte ou des exclusions non valides peuvent entraîner des divergences, ce qui souligne la nécessité d’une gestion prudente.
Recommandations
- Testez toujours le script sur un appareil qui n’est pas dans un environnement de production afin de vérifier son comportement.
- Utilisez des conventions de dénomination claires et cohérentes pour les solutions antivirus afin d’éviter les conflits.
- Documentez les exclusions appliquées aux appareils pour référence ultérieure.
- Limitez l’accès à ce script afin d’éviter toute modification non autorisée.
Conclusion
Ce script illustre la manière dont l’automatisation améliore l’efficacité et la précision de la gestion informatique. Pour les utilisateurs de NinjaOne ou de plateformes RMM similaires, l’utilisation d’outils tels que ce script avec des fonctions avancées de gestion des terminaux garantit une posture de sécurité solide. NinjaOne simplifie la visibilité et la gestion des terminaux, en fournissant une plateforme unifiée pour rationaliser les opérations informatiques tout en relevant des défis tels que les exclusions antivirus avec précision.