11#! /bin/bash
22
3- # Version 0.4.1
3+ # Version 0.4.6
44
55# This sends any bro/zeek logs less than three days old to the rita/aihunter server.
66# Any logs that already exist on the target system are not retransferred.
77
88# Before using this, run these on the rita/aihunter server (use zeek in place of bro if necesssary):
99# sudo adduser dataimport
1010# sudo passwd dataimport
11- # sudo mkdir -p /opt/bro /remotelogs/ /home/dataimport/.ssh/
11+ # sudo mkdir -p /opt/zeek /remotelogs/ /home/dataimport/.ssh/
1212# add the dataimport user's ssh public key to /home/dataimport/.ssh/authorized_keys in the rita/aihunter server
13- # sudo chown -R dataimport /opt/bro /remotelogs/ /home/dataimport/.ssh/
13+ # sudo chown -R dataimport /opt/zeek /remotelogs/ /home/dataimport/.ssh/
1414# sudo chmod go-rwx -R /home/dataimport/.ssh/
1515
1616export PATH=" /sbin:/usr/sbin:$PATH " # Note that cron does _NOT_ include /sbin in the path, so attempts to locate the "ip" binary fail without this fix
@@ -58,13 +58,14 @@ status () {
5858
5959usage () {
6060 cat << HEREDOC >&2
61- Usage: $0 [--all] [--dest where_to_ssh] [--localdir /local/top/dir/] [--remotedir /remote/top/dir/] [--rsyncparams '--aparam --anotherparam']
61+ Usage: $0 [--all] [--dest where_to_ssh] [--localdir /local/top/dir/] [--remotedir /remote/top/dir/] [--numdays days_to_transfer] [-- rsyncparams '--aparam --anotherparam']
6262
6363Options:
6464 --all Sync all Zeek log types instead of the default subset.
6565 --dest SSH destination target (e.g hostname, IP, user@hostname, user@ip)
6666 --localdir Location of Zeek logs on local system. (default: searches common locations)
6767 --remotedir Location of Zeek logs on remote system. (default: /opt/zeek/remotelogs/<sensorname>/)
68+ --numdays Number of 24 hour periods, going backwards from now, to send. Must be an integer, minimum value 3 . Default 3
6869 --rsyncparams Allows specifying parameters for rsync. Enclose in a pair of single quotes.
6970
7071 Suggestions:
@@ -91,7 +92,7 @@ require_util () {
9192
9293
9394# Check that we have basic tools to continue
94- require_util awk cut date egrep find grep hostname ip nice rsync sed ssh sort tr || fail " Missing a required utility"
95+ require_util awk cut date egrep find flock grep hostname mktemp nice rsync sed ssh sort tr || fail " Missing a required utility"
9596
9697# ionice is not stricly required; if it exists we'll use it to give all other processes on the system first access to the disk, effectively eliminating the chance that we cause dropped packets from disk contention.
9798if type -path ionice > /dev/null 2> /dev/null ; then
103104# Default log types to send
104105log_type_regex=' (conn|dns|http|ssl|x509|known_certs|capture_loss|notice|stats)'
105106
107+ timeout_secs=7080
108+
106109# Parse command line flags
107110while [ -n " $1 " ]; do
108111 if [ " z$1 " = " z--all" ]; then
@@ -122,6 +125,12 @@ while [ -n "$1" ]; do
122125 aih_location=" ${default_user_on_aihunter} @${2} "
123126 fi
124127 shift
128+ elif [ " z$1 " = " z--numdays" -a -n " $2 " ]; then
129+ numdays=" $2 "
130+ if [ " $numdays " != " 3" ]; then
131+ timeout_secs=600000 # Just under a week
132+ fi
133+ shift
125134 elif [ " z$1 " = " z--rsyncparams" -a -n " $2 " ]; then
126135 rsyncparams=" $2 "
127136 shift
@@ -139,6 +148,10 @@ if [ -z "$rsyncparams" ]; then
139148 rsyncparams=" -q "
140149fi
141150
151+ if [ -z " $numdays " -o " z$numdays " = " z0" -o " z$numdays " = " z1" -o " z$numdays " = " z2" ]; then
152+ numdays=" 3"
153+ fi
154+
142155# Where should we send the bro/zeek logs?
143156if [ -z " $aih_location " ]; then
144157 if [ -s /etc/rita/agent.yaml ]; then
154167if [ -s /etc/rita/agent.yaml -a -n " ` grep ' ^[^#]*Name' /etc/rita/agent.yaml 2> /dev/null | sed -e ' s/^.*Name:*\W*//' ` " ]; then
155168 # Manually setting the hostname to use in agent.yaml is preferred...
156169 my_id=` grep ' ^[^#]*Name' /etc/rita/agent.yaml 2> /dev/null | sed -e ' s/^.*Name:*\W*//' | tr -dc ' a-zA-Z0-9_^+=' | cut -c -52`
157- else
170+ elif type -path ip > /dev/null 2>&1 ; then
158171 # ...but if no name is forced, we use the short hostname + the primary IP, which should be unique.
159172 # Following is short form of the hostname, then "__", then the primary IP ipv4 address (one for the default route) of the system.
160173 # The tr command strips off spaces or odd characters in hostname
161174 my_id=` hostname -s | tr -dc ' a-zA-Z0-9_^+=' ` " __" ` ip route get 8.8.8.8 | awk ' {print $NF;exit}' | tr -dc ' a-zA-Z0-9_^+=' `
162175 my_id=` echo " $my_id " | cut -c -52`
176+ elif type -path ifconfig > /dev/null 2>&1 && type -path route > /dev/null 2>&1 ; then
177+ # No "ip" command (macos) so we try ifconfig and route
178+ default_route_interface=" $( route get default | grep ' interface: ' | sed ' s/[^:]*: \(.*\)/\1/' ) "
179+ my_id=$( hostname -s | tr -dc ' a-zA-Z0-9_^+=' ) " __" $( ifconfig " $default_route_interface " | grep ' inet ' | sed ' s/.*inet \([0-9.]*\) .*/\1/' | tr -dc ' a-zA-Z0-9_^+=' ) # '
180+ my_id=` echo " $my_id " | cut -c -52`
181+ else
182+ fail " Unable to locate ip, ifconfig, or route to identify local IP address."
163183fi
164184
165185extra_ssh_params=' '
@@ -221,6 +241,8 @@ if [ -z "$local_tld" ]; then
221241 fi
222242fi
223243
244+ send_candidate_file=$( mktemp -q -t send_candidate_list.$( date +%Y%m%d%H%M%S) .XXXXXX < /dev/null)
245+
224246ids_name=' zeek'
225247if [[ $local_tld == * " bro" * ]]; then
226248 ids_name=' bro'
@@ -237,28 +259,35 @@ threeda=`date '+%Y-%m-%d' --date='3 days ago'`
237259
238260status " Sending logs to rita/aihunter server $aih_location , My name: $my_id , local dir: $local_tld , remote dir: $remote_top_dir "
239261
262+ # Strictly speaking this isn't required - rsync will create directories on the target system as needed.
263+ # With that said, it's helpful to test that raw ssh will work before attempting to transfer files.
240264status " Preparing remote directories"
241265ssh $extra_ssh_params " $aih_location " " mkdir -p ${remote_top_dir} /$today / ${remote_top_dir} /$yesterday / ${remote_top_dir} /$twoda / ${remote_top_dir} /$threeda / ${remote_top_dir} /current/"
242266
243267cd " $local_tld " || fail " Unable to change to $local_tld "
244- send_candidates= ` find . -type f -mtime -3 -iname ' *.gz' | egrep " $log_type_regex " | grep -v ' /\.' | sort -u`
245- if [ ${ # send_candidates} -eq 0 ]; then
268+ find . -type f -mtime -" $numdays " -iname ' *.gz' | egrep " $log_type_regex " | grep -v ' /\.' | sort -u > " $send_candidate_file "
269+ if [ ! -s " $send_candidate_file " ]; then
246270 echo
247- printf " WARNING: No logs found, if your log directory is not $local_tld please use the flag: --localdir [bro_zeek_log_directory]"
271+ echo " WARNING: No logs found, if your log directory is not $local_tld please use the flag: --localdir [bro_zeek_log_directory]"
248272 echo
249-
250273fi
274+
251275status " Transferring files to $aih_location "
252- flock -xn " $HOME /rsync_log_transport.` echo $aih_location | sed -e ' s/@/_/g' ` .lck" timeout --kill-after=60 7080 $nice_me rsync $rsyncparams -avR -e " ssh $extra_ssh_params " $send_candidates " $aih_location :${remote_top_dir} /" --delay-updates --chmod=Do+rx,Fo+r
276+ # By placing the destination name and the number of days requested in the lock file name, we allow
277+ # simultaneous transfers to multiple remote hosts and simultaneous transfers of the last 3 days and
278+ # $numdays days.
279+ flock -xn " $HOME /rsync_log_transport.` echo $aih_location | sed -e ' s/@/_/g' ` .$numdays .lck" timeout --kill-after=60 " $timeout_secs " $nice_me rsync $rsyncparams -avR -e " ssh $extra_ssh_params " --files-from=" $send_candidate_file " " $local_tld " " $aih_location :${remote_top_dir} /" --delay-updates --chmod=Do+rx,Fo+r
253280retval=$?
254281if [ " $retval " == " 1" ]; then
255282 status " Unable to obtain lock and run a new copy of rsync as the previous rsync appears to still be running."
283+ rm -f " $send_candidate_file "
256284elif [ " $retval " == " 124" -o " $retval " == " 129" ]; then
257285 status " Rsync was forcibly terminated as it was running too long."
258286elif [ " $retval " == " 0" ]; then
259287 status " Rsync finished transferring without error."
288+ rm -f " $send_candidate_file "
260289fi
261290
262291# Note: after we added a user option to set the destination dir, we remove the --temp-dir option as this dir may not be on the same mount point as the destination dir.
263292# rsync will put temporary files in a .~tmp~ directory under each destination subdir.
264- # Originally: --temp-dir="/opt/bro /tmp/$my_id/"
293+ # Originally: --temp-dir="/opt/zeek /tmp/$my_id/"
0 commit comments