Hello
I made script that works, though script itself is not considering all possible cases and was done just in a quick way to work. I used Linux system to deploy OSSEC agent to 500 systems in an hour and I will try explain method I used on Linux host where OSSEC is installed.
Script is using one parameter - client name as is in DNS. I simply used bash to cycle around (like "for host in `cat list_of_hosts`; do ./deploy $host; done").
Script has to be improved in efficiency and error handling, etc. - please feel free use it as a basic idea and adapt it as needed.
#!/bin/bash
HOST=$1
PASSWORD=password_for_admin_uid
SECADMIN=domain\\uid_with_admin privileges
# Is host alive ?
ping -c1 $HOST 2>&1 >/dev/null
# If yes - lets continue
if [ $? -eq 0 ]
then
# If host is already in active agents list - we stop. To force reload client you can comment below
/var/ossec/bin/list_agents -c | egrep -qi "^$HOST-"
if [ $? -eq 0 ]
then
echo "$HOST is already active on this installation"
exit 0
fi
# set IP and capitalize NAME of the HOST
IP=$(host $HOST 2>&1|cut -d\ -f4)
NAME=$(echo $HOST | tr '[a-z]' '[A-Z]')
# Let's check if host has already key - if not create CSV file to import a new host and create new OSSEC ID
# I use for the moment netmask "
0.0.0.0/0" for any - you can use client IP to format mask
OSSECID=$(grep -i " $HOST " /var/ossec/etc/client.keys | cut -d\ -f1)
if [ "$OSSECID" == "" ]
then
echo "
0.0.0.0/0,$NAME" >/var/ossec/$HOST
/var/ossec/bin/manage_agents -f /$1
fi
#Let's get OSSEC ID for the client
OSSECID=$(grep -i " $HOST " /var/ossec/etc/client.keys | cut -d\ -f1)
if [ "$OSSECID" == "" ]
then
echo "Can not get OSSEC ID for client $NAME"
exit 1
fi
#extract key file for the client
egrep "^$OSSECID " /var/ossec/etc/client.keys > $HOST.key
# Check architecture and which "Program Files" shall be used
if [ $(/usr/bin/winexe --user=$SECADMIN%$PASSWORD //$HOST "wmic os get osarchitecture" | grep bit | cut -d- -f1) -eq 32 ]
then
ROOTDIR="Program Files"
else
ROOTDIR="Program Files (x86)"
fi
#Create install directory and copy there client
smbclient //$HOST/c$ $PASSWORD -U $SECADMIN -c "mkdir Install; cd Install; put ossec-agent-win32-2.8.exe"
#Launch unattended client installation
/usr/bin/winexe --user=$SECADMIN%$PASSWORD //$HOST 'cmd /C C:\Install\ossec-agent-win32-2.8.exe /S'
#copy default and properly configured ossec.conf file and extracted host key to right location
smbclient //$HOST/c$ $PASSWORD -U $SECADMIN -c "cd \"$ROOTDIR\"\\ossec-agent; put ossec.conf ossec.conf; put $HOST.key client.keys"
# To be sure - do installation as service and stop/start client
/usr/bin/winexe --user=$SECADMIN%$PASSWORD //$HOST "cmd /C C:\\\"$ROOTDIR\"\\ossec-agent\\ossec-agent.exe install-service"
/usr/bin/winexe --user=$SECADMIN%$PASSWORD //$HOST 'cmd /C net stop "OSSEC HIDS"'
/usr/bin/winexe --user=$SECADMIN%$PASSWORD //$HOST 'cmd /C net start "OSSEC HIDS"'
else
echo Host $HOST is not online
fi
Martynas