root/trunk/mwsubmitter/src/mwsubmitter

Revision 680, 7.2 kB (checked in by chengyu, 2 years ago)

Add multiple files submission for convenient.

Line 
1 #!/bin/bash
2 #
3 ############################################################################
4 #
5 # Submit found samples using G.O.T.E.K. client
6 # Copyright (C) 2006 Artmeis: Chinese Honeynet Project
7 #
8 # Version 0.1.1
9
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23 #
24 ############################################################################
25 #
26 # Usage: mwsubmitter [-c config-file] [-D] -b
27 #                mwsubmitter [-c config-file] [-t cycle] -m
28 #        mwsubmitter -s server-address [-p port] [-D] -u user-name -k key-file {sample-dir|sample-file[s]}
29 #        mwsubmitter -v
30 #        mwsubmitter -h
31 #
32 #        -s G.O.T.E.K. server address.
33 #                -p G.O.T.E.K. server port.
34 #        -c Use <config-file> as monitor directory set, use $HOME/.mwsubmitter/config
35 #                       as default. Read MwSubmitter Manual for more information about MwSubmitter's
36 #                       config-file.
37 #                -D Delete samples after successful submit.
38 #                -b Use batch mode.
39 #                -t Scan cycle in monitor mode.
40 #                -m Use monitor mode, implicitly contain -D option.
41 #        -u User name for submit authentication.
42 #        -k Correspond key file for submit authentication.
43 #
44 ###########################################################################
45 #
46 # Bugs & Suggestions
47 #
48 #   Please write to songchengyu@icst.pku.edu.cn
49 #                       or cs@mwcollect.org
50 #
51 ###########################################################################
52 #
53 #! /bin/bash
54
55 version="0.1.1"
56
57 submit_sample() {
58         local sample_dir="$1"
59         local file
60         local line
61         local new_name
62
63         #if there are samples
64         if [ -n "$(ls -1 "$sample_dir")" ]; then
65                
66                 echo "Sumbit samples to server $server"
67                 echo "Delete duplicate samples..."
68                
69                 ls -1 "$sample_dir" | while read line
70                 do
71                         if [[ "$line" != *[[:upper:]]* ]]; then
72                                 continue
73                         fi
74                         new_name=`echo "$line" | tr 'A-Z' 'a-z'`
75                         mv "$sample_dir/$line" "$sample_dir/$new_name"
76                 done
77                
78                 echo "Submitting..."
79                 for file in "$sample_dir"/*; do
80                         if [ ! -s "$file" ]; then
81                         echo "Clean empty file $file"
82                                 rm -f "$file"
83                         continue
84                 fi
85
86                         submit_file "$file"
87                 done
88         fi
89         echo "Finished"
90 }
91
92 submit_file (){
93         local file="$1"
94         local new_file
95
96         echo -n "Submitting $file ... "
97        
98         if [ -z $port ]; then
99                 "$gotekc_path" --key "$key" --user "$name" --server "$server" --file "$file" 2>&1 > /dev/null
100         else
101                 "$gotekc_path" --key "$key" --user "$name" --server "$server" --port "$port" --file "$file" 2>&1 > /dev/null
102         fi
103         result=$?
104
105     if (($result == 0 || $result == 2)); then
106         echo "Succeed"
107
108         if (($doDelete == 1)); then
109                 rm -f "$1"
110                 fi
111     else
112         echo "Failed"
113
114         if ((monitor == 1)); then
115             if [ -z "$(echo "$file" | grep '#_#')" ]; then
116                 new_file="$file""#_#"`date +%G%m%d%H%M`
117                 mv "$file" "$new_file"
118             fi
119         fi
120     fi
121 }
122
123 process (){
124         local dir
125        
126         while read server port; do
127                 if [ -z "$(echo $server | grep ^#)" ]; then
128                         break
129                 fi
130         done
131
132         while read name key dir; do
133                 if [ -n "$(echo $name | grep ^#)" ]; then
134                         continue
135                 fi
136
137                 if [ ! -d "$dir" ]; then
138                     echo "$PROGRAM: scan directory does not exist"
139                     return 1
140                 fi
141                
142                 if [ ! -e "$key" ]; then
143                     echo "$PROGRAM: key file for user $name not found"
144                     return 1
145                 fi
146                
147                 submit_sample "$dir"
148         done
149 }
150
151 usage() {
152         echo "Usage: $PROGRAM [-c config-file] [-D] -b"
153         echo "       $PROGRAM [-c config-file] [-t cycle] -m"
154         echo "       $PROGRAM -s server-address [-p port] [-D] -u user-name -k key-file {dir|file[s]}"
155         echo "       $PROGRAM -v"
156         echo "       $PROGRAM -h"
157         echo -e "\n\t-s G.O.T.E.K. server address."
158         echo -e "\t-p G.O.T.E.K. server port."
159         echo -e "\t-c Use <config-file> as monitor directory set, use $HOME/.mwsubmitter/config. \n\t   as default. Read MwSubmitter Manual for more information about MwSubmitter's \n\t   config-file."
160         echo -e "\t-D Delete samples after successful submit."
161         echo -e "\t-b Use batch mode."
162         echo -e "\t-t Scan cycle in monitor mode."
163         echo -e "\t-m Use monitor mode, implicitly contain -D option."
164         echo -e "\t-u User name for submit authentication."
165         echo -e "\t-k Correspond key file for submit authentication."
166         echo -e "\t-v Version."
167         echo -e "\t-h Show this message."
168         echo -e "\nReport bugs to <songchengyu@icst.pku.edu.cn>"
169 }
170
171 version (){
172         echo -e "MwSubmitter v$version\n"
173         echo "Copyright (C) 2006 Artemis: Chinese Honeynet Project"
174         echo "This program is distributed in the hope that it will be useful,"
175         echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
176         echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
177         echo "GNU General Public License for more details."
178         echo ""
179 }
180
181 ###########################################################################3
182
183 # Check and generate working directory
184
185 PROGRAM=${0##*/}
186 home_dir=${HOME:?'$HOME not defined.'}
187 if [ ! -d "$home_dir"/.mwsubmitter ]; then
188     mkdir "$home_dir"/.mwsubmitter
189 fi 
190
191
192 if (($# == 0)); then
193         usage
194         exit 0
195 fi
196
197 while getopts ":s:p:c:bt:mu:k:Dvh" opt; do
198         case $opt in
199                 s  )    server="$OPTARG" ;;
200
201                 p  )    port="$OPTARG" ;;
202
203                 c  )    cfg_file="$OPTARG" ;;
204
205                 D  )    doDelete=1 ;;
206
207                 b  )    batch=1 ;;
208
209                 t  )    cycle=$OPTARG ;;
210
211                 m  )    monitor=1 ;;
212                
213                 u  )    name="$OPTARG" ;;
214
215                 k  )    key="$OPTARG" ;;
216                
217                 v  )    version
218                                 exit 0 ;;
219                                        
220                 h  )    usage
221                                 exit 0 ;;
222                
223                 \? )    usage
224                                 exit 1
225         esac
226 done
227
228 shift $(($OPTIND - 1))
229
230 batch=${batch:-0}
231 monitor=${monitor:-0}
232 cycle=${cycle:-1200}
233 cfg_file=${cfg_file:-"$HOME/.mwsubmitter/config"}
234 gotekc_path="/usr/local/bin/gotekc"
235 doDelete=${doDelete:-0}
236
237 echo "MwSubmitter v$version"
238 echo "Copyright (c) 2006 Artemis: Chinese Honeynet Project"
239 echo ""
240
241 if [ ! -x $gotekc_path ]; then
242         echo "$PROGRAM: G.O.T.E.K. client not found, please reinstall gotekc."
243         exit 1
244 fi
245
246 if (($monitor == 1)); then
247         if [ ! -e $cfg_file ]; then
248                 echo "$PROGRAM: config-file $cfg_path not found."
249                 exit 1
250         fi
251
252         doDelete=1
253         while : ; do
254                 process < $cfg_file
255                 date
256                 sleep $cycle
257         done
258 fi
259
260 if (($batch == 1)); then
261     if [ ! -e $cfg_file ]; then
262         echo "$PROGRAM: config-file $cfg_path not found."
263         exit 1
264     fi
265
266         process < $cfg_file
267         exit 0
268 fi
269
270 if [ -z $server ]; then
271     echo "$PROGRAM: server address not specified."
272     exit 1
273 fi
274
275 if [ -z $name ]; then
276         echo "$PROGRAM: user name not specified."
277         exit 1
278 fi
279 if [ -z "$key" ]; then
280         echo "$PROGRAM: key file not specified"
281         exit 1
282 fi
283
284 if [ ! -e "$key" ]; then
285         echo "$PROGRAM: key file not found"
286         exit 1
287 fi
288
289 if [ -z "$*" ]; then
290         echo "$PROGRAM: no submitter dir or file specified"
291         exit 1
292 fi
293
294 if [ -d "$1" ]; then
295         submit_sample "$1"
296         exit 0
297 fi
298
299 for file in "$@"; do
300         if [ ! -e "$file" ]; then
301                 echo "$PROGRAM: $file does not exist"
302                 continue
303         fi
304         if [ ! -s "$file" ]; then
305                 echo "Ignore empty file $file"
306                 continue
307         fi
308         submit_file "$file"
309 done   
Note: See TracBrowser for help on using the browser.