wirefuzz.pl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. #!/usr/bin/perl -w
  2. #Author:William Metcalf <william.metcalf@gmail.com>
  3. #File:wirefuzz.pl
  4. #Copyright (C) 2010-2015 Open Information Security Foundation
  5. #You can copy, redistribute or modify this Program under the terms of
  6. #the GNU General Public License version 2 as published by the Free
  7. #Software Foundation.
  8. #
  9. #This program is distributed in the hope that it will be useful,
  10. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. #GNU General Public License for more details.
  13. #
  14. #You should have received a copy of the GNU General Public License
  15. #version 2 along with this program; if not, write to the Free Software
  16. #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. #02110-1301, USA.
  18. #This script is useful if you want to fuzz and or otherwise try to make suricata explode during decoding/proto parsing using saved pcaps.
  19. #It is simply a reimplimentation of the technique described here, hence the name:
  20. #http://wiki.wireshark.org/FuzzTesting
  21. #
  22. #Options for getting thre required perl modules:
  23. #Ubuntu 9.10
  24. #sudo apt-get install libdevel-gdb-perl libcapture-tiny-perl
  25. #
  26. #RedHatES/CentOS 5
  27. #yum -y install cpanspec perl-Module-Build
  28. #cpanspec --packager OISF -v -s --follow Capture::Tiny
  29. #cpanspec --packager OISF -v -s --follow Devel::GDB
  30. #rpmbuild --rebuild *.src.rpm
  31. #rpm -ivh /usr/src/redhat/RPMS/noarch/perl-Devel-GDB*.rpm
  32. #rpm -ivh /usr/src/redhat/RPMS/noarch/perl-Capture-Tiny*.rpm
  33. #
  34. #Fedora Core 12
  35. #yum -y install perl-Capture-Tiny perl-Devel-GDB
  36. #
  37. #Other debain based versions, try the Ubunutu instructions if this doesn't work try the following.
  38. #sudo apt-get install dh-make-perl
  39. #mkdir fuzzmodules && cd fuzzmodules
  40. #dh-make-perl --cpan Devel-GDB --build
  41. #dh-make-perl --cpan Capture-Tiny --build
  42. #sudo dpkg -i *.deb
  43. #TODO: Figure out a better way to deal with signal handling.
  44. #TODO: Try to determine flow/stream that caused segv by extracting from the bt and extract it from the pcap.
  45. #TODO: E-mail notification on segv?
  46. #TODO: Parse Valgrind output and alert on errors
  47. use strict;
  48. use warnings;
  49. use Capture::Tiny 'capture';
  50. use List::Util 'shuffle';
  51. use Devel::GDB;
  52. use File::Find;
  53. use Getopt::Long qw(:config no_ignore_case);
  54. use File::Basename;
  55. #globals
  56. my %config;
  57. my @tmpfiles;
  58. my @files;
  59. my $suricatabin;
  60. my $loopnum;
  61. my $loopnum_per_file = 0;
  62. my $rules;
  63. my $rules_exclusive = 0;
  64. my $logdir;
  65. my $configfile;
  66. my $editeratio;
  67. my $valgrindopt;
  68. my $shuffle;
  69. my $useltsuri;
  70. my $ltsuribin;
  71. my $core_dump;
  72. my $excluderegex;
  73. my %excludefuzz;
  74. my $timestamp;
  75. my $keeplogs;
  76. my $file_was_fuzzed = 0;
  77. Getopt::Long::Configure("prefix_pattern=(-|--)");
  78. GetOptions( \%config, qw(n=s N=s r=s c=s e=s v=s p=s l=s s=s S=s x=s k y z=s q h help) );
  79. &parseopts();
  80. #Parse the options
  81. sub parseopts {
  82. #display help if asked
  83. if ( $config{h} || $config{help} ) {
  84. &printhelp();
  85. }
  86. #filemask of pcaps to read?
  87. if ( $config{r} ) {
  88. @tmpfiles = <$config{r}>;
  89. if(@tmpfiles eq 0){
  90. print "parseopts: Pcap filemask was invalid we couldn't find any matching files\n";
  91. exit(1);
  92. } else {
  93. #escapes for filenames
  94. foreach my $file (@tmpfiles) {
  95. $file =~ s/\(/\\(/g;
  96. $file =~ s/\)/\\)/g;
  97. $file =~ s/\&/\\&/g;
  98. }
  99. }
  100. }
  101. else {
  102. print "parseopts: Pcap filemask not specified or doesn't exist\n";
  103. &printhelp();
  104. }
  105. #filemask do we have a path to suricata bin?
  106. if ( $config{p} && -e $config{p} ) {
  107. $suricatabin = $config{p};
  108. #do wrapper script detection lt-suricata won't be created until first run but .libs/suricata should exist.
  109. if ( -T $suricatabin ) {
  110. open my $in, '<', $suricatabin or die "Can't read old file: $!";
  111. while (<$in>) {
  112. if ( $_ =~
  113. m/suricata \- temporary wrapper script for \.libs\/suricata/
  114. )
  115. {
  116. print "parseopts: suricata bin file appears to be a wrapper script going to try to find the real bin for gdb.\n";
  117. my $tmpdirname = dirname $suricatabin;
  118. my $tmpltsuriname = $tmpdirname . "/.libs/suricata";
  119. if ( -e $tmpltsuriname && -B $tmpltsuriname ) {
  120. $ltsuribin = $tmpltsuriname;
  121. print "parseopts: telling gdb to use " . $ltsuribin . "\n";
  122. $useltsuri = "yes";
  123. }
  124. last;
  125. }
  126. }
  127. close $in;
  128. }
  129. elsif ( -B $suricatabin ) {
  130. if ( ! $config{q} ) {
  131. print "parseopts: suricata bin file checks out\n";
  132. }
  133. }
  134. else {
  135. print "parseopts: suricata bin file is not a text or a bin exiting.\n";
  136. exit(1);
  137. }
  138. }
  139. else {
  140. print "parseopts: Path to suricata bin not provided or doesn't exist\n";
  141. &printhelp();
  142. }
  143. if ( $config{n} && $config{N} ) {
  144. print "ERROR: can't mix -n and -N\n";
  145. exit(1);
  146. }
  147. #number of times to loop
  148. if ( $config{n} ) {
  149. $loopnum = $config{n};
  150. $loopnum_per_file = 1;
  151. print "parseopts: looping through the pcaps " . $loopnum . " times or until we have an error\n";
  152. }
  153. else {
  154. $loopnum = "infinity";
  155. }
  156. if ( $config{N} ) {
  157. $loopnum = $config{N};
  158. print "parseopts: looping through the pcaps " . $loopnum . " times or until we have an error\n";
  159. }
  160. else {
  161. $loopnum = "infinity";
  162. }
  163. if ( $loopnum == "infinity") {
  164. print "parseopts: looping through the pcaps forever or until we have an error\n";
  165. }
  166. #rules file do we have a path and does it exist
  167. if ( $config{s} && -e $config{s} ) {
  168. $rules = $config{s};
  169. print "parseopts: telling suricata to use rules file " . $rules . "\n";
  170. }
  171. else {
  172. print("parseopts: rules file not specified or doesn't exist\n");
  173. }
  174. # exclusive rules file: do we have a path and does it exist
  175. if ( $config{S} && -e $config{S} ) {
  176. $rules = $config{S};
  177. $rules_exclusive = 1;
  178. print "parseopts: telling suricata to use rules file exclusively " . $rules . "\n";
  179. }
  180. else {
  181. print("parseopts: rules file not specified or doesn't exist\n");
  182. }
  183. #log dir does it exist
  184. if ( $config{l} && -e $config{l} ) {
  185. $logdir = $config{l} . "/";
  186. print "parseopts: using log dir " . $logdir . "\n";
  187. }
  188. else {
  189. $logdir = "./";
  190. }
  191. #config file do we have a path and does it exist
  192. if ( $config{c} && -e $config{c} ) {
  193. $configfile = $config{c};
  194. print "parseopts: telling suricata to use the config file " . $configfile . "\n";
  195. }
  196. else {
  197. print "parseopts: config file not specified or doesn't exist\n";
  198. &printhelp();
  199. }
  200. #% chance that a byte will be modified.
  201. if ( $config{e} ) {
  202. #valid range?
  203. my $tmperatio = $config{e} * 100;
  204. if ( $tmperatio <= 100 && $tmperatio >= 0 ) {
  205. $editeratio = $config{e};
  206. print "parseopts: using error ratio " . $editeratio . "\n";
  207. }
  208. else {
  209. print "parseopts: error ratio specified but outside of range. Valid range is 0.00-1.0\n";
  210. exit(1);
  211. }
  212. }
  213. else {
  214. print("parseopts: not going to fuzz pcap(s)\n");
  215. }
  216. #parse the valgrind opts
  217. if ( $config{v} ) {
  218. if ( $config{v} =~ /^(memcheck|drd|helgrind|callgrind)$/ ) {
  219. $valgrindopt = $config{v};
  220. print "parseopts: using valgrind opt " . $valgrindopt . "\n";
  221. }
  222. else {
  223. print "invalid valgrind opt " . $valgrindopt . "\n";
  224. }
  225. }
  226. #shuffle the array if we are starting multiple fuzzers at once. GO-GO gadget shuffle
  227. if ( $config{y} ) {
  228. print "parseopts: going to shuffle the array\n";
  229. $shuffle = "yes";
  230. }
  231. #keep logs instead of removing them after each run
  232. if ( $config{k} ) {
  233. print "parseopts: going to keep logs instead of removing them\n";
  234. $keeplogs = "yes";
  235. }
  236. else {
  237. $keeplogs = "no";
  238. }
  239. #we want to process some files but not fuzz them add them to a hash and check it later
  240. if ( $config{z} ) {
  241. print "will process but not fuzz files that match regex of " . $config{z} . "\n";
  242. $excluderegex = $config{z};
  243. my $tmpfilepos = 0;
  244. while ($tmpfilepos <= $#tmpfiles) {
  245. if ($tmpfiles[$tmpfilepos] =~ m/$excluderegex/) {
  246. print "adding " . $tmpfiles[$tmpfilepos] . " to fuzz_exclude_hash because it matches our regex\n";
  247. $excludefuzz{$tmpfiles[$tmpfilepos]} = 1;
  248. }
  249. $tmpfilepos++
  250. }
  251. }
  252. #maybe we want to exclude a file based on some regex so we can restart the fuzzer after an error
  253. #and not have to worry about hitting the same file.
  254. if ( $config{x} ) {
  255. print "excluding files that match regex of " . $config{x} . "\n";
  256. $excluderegex = $config{x};
  257. my $tmpfilepos = 0;
  258. while ($tmpfilepos <= $#tmpfiles) {
  259. if ($tmpfiles[$tmpfilepos] =~ m/$excluderegex/) {
  260. print "removing " . $tmpfiles[$tmpfilepos] . " because it matches our exclude regex\n";
  261. splice(@tmpfiles, $tmpfilepos, 1);
  262. }
  263. else {
  264. $tmpfilepos++
  265. }
  266. }
  267. }
  268. if ( ! $config{q} ) {
  269. print "******************Initialization Complete**********************\n";
  270. }
  271. return;
  272. }
  273. sub printhelp {
  274. print "
  275. -h or help <this output>
  276. -r=<filemask for pcaps to read>
  277. -n=<(optional) number of iterations or if not specified will run until error>
  278. -N=<(optional) number of iterations of all files in the test set>
  279. -s=<(optional) path to ids rules file will be passed as -s to suricata>
  280. -S=<(optional) path to ids rules file will be passed as -S to suricata>
  281. -e=<(optional) editcap error ratio to introduce if not specified will not fuzz. Valid range for this is 0.00 - 1.0>
  282. -p=<path to the suricata bin>
  283. -l=<(optional) log dir for output if not specified will use current directory.>
  284. -v=<(optional) (memcheck|drd|helgrind|callgrind) will run the command through one of the specified valgrind tools.>
  285. -x=<(optional) regex for excluding certian files incase something blows up but we want to continue fuzzing .>
  286. -z=<(optional) regex for excluding certian files from fuzzing but still process them note: the original files will be processed and not removed.>
  287. -y <shuffle the array, this is useful if running multiple instances of this script.>
  288. -k <will keep alert-debug.log fast.log http.log and stats.log instead of removing them at the end of each run.>
  289. Example usage:
  290. First thing to do is download and build suricata from git with -O0 so vars don't get optimized out. See the example below:
  291. git clone git://phalanx.openinfosecfoundation.org/oisf.git suricatafuzz1 && cd suricatafuzz1 && ./autogen.sh && CFLAGS=\"-g -O0\" ./configure && make
  292. Second thing to do is to edit suricata.yaml to fit your environment.
  293. Third go ahead and run the script.
  294. In the example below the script will loop forever until an error is encountered will behave in the following way.
  295. 1.-r Process all pcaps in subdirectories of /home/somepath/pcaps/
  296. 2.-s Tell suricata to use the rules file /home/somepath/current-all.rules
  297. 3.-y Shuffle the array of pcaps this is useful if running multiple instances of this script.
  298. 4.-c Tell suricata to use the suricata.yaml in the current dir.
  299. 6.-e Tell editcap to introduce a 2% error ratio, i.e. there is a 2% chance that a byte will be fuzzed see http://wiki.wireshark.org/FuzzTesting for more info.
  300. 7.-p Use src/suricata as our suricata bin file. The script will determin if the argument passed is a bin file or a txt wrapper and will adjust accordingly.
  301. /usr/bin/wirefuzz.pl -r=/home/somepath/pcaps/*/* -s=/home/somepath/current-all.rules -y -c=suricata.yaml -e=0.02 -p src/suricata
  302. If an error is encountered a file named <fuzzedfile>ERR.txt will be created in the log dir (current dir in this example) that will contain output from stderr,stdout, and gdb.
  303. Take a look at the opts make it work for you environtment and from the OISF QA team thanks for helping us make our meerkat fuzzier! ;-)\n";
  304. exit;
  305. }
  306. my $logfile = $logdir . "wirefuzzlog.txt";
  307. open( LOGFILE, ">>$logfile" )
  308. || die( print "error: Could not open logfile! $logfile\n" );
  309. my $successcnt = 0;
  310. while ( $successcnt < $loopnum ) {
  311. if ( defined $shuffle ) {
  312. @files = shuffle(@tmpfiles);
  313. }
  314. else {
  315. @files = @tmpfiles;
  316. }
  317. foreach my $file (@files) {
  318. my $file_was_fuzzed = 0;
  319. #split out the path from the filename
  320. my $filedir = dirname $file;
  321. my $filename = basename $file;
  322. my ( $fuzzedfile, $editcapcmd, $editcapout, $editcaperr, $editcapexit,
  323. $editcap_sys_signal, $editcap_sys_coredump );
  324. my ( $fuzzedfiledir, $fuzzedfilename, $fullcmd, $out, $err, $exit,
  325. $suricata_sys_signal, $suricata_sys_coredump, $report);
  326. if ( ! $config{q} ) {
  327. print "Going to work with file: $file\n";
  328. }
  329. my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
  330. localtime(time);
  331. $timestamp = sprintf "%4d-%02d-%02d-%02d-%02d-%02d", $year + 1900,
  332. $mon + 1, $mday, $hour, $min, $sec;
  333. if ( defined $editeratio and !exists $excludefuzz{$file}) {
  334. $file_was_fuzzed = 1;
  335. $fuzzedfile = $logdir . $filename . "-fuzz-" . $timestamp;
  336. $editcapcmd =
  337. "editcap -E " . $editeratio . " " . $file . " " . $fuzzedfile;
  338. if ( ! $config{q} ) {
  339. print( "editcap: " . $editcapcmd . "\n" );
  340. }
  341. ( $editcapout, $editcaperr ) = capture {
  342. system $editcapcmd;
  343. $editcapexit = $? >> 8;
  344. $editcap_sys_signal = $? & 127;
  345. $editcap_sys_coredump = $? & 128;
  346. };
  347. if ( $editcapexit ne 0 ) {
  348. #this could still cause us to loop forever if all pcaps are bad but it's better than nothing.
  349. if ( @files < 2 ) {
  350. print "editcap: had an error and this was our only pcap:" . $editcaperr . "\n";
  351. exit(1);
  352. }
  353. else {
  354. print "editcap: had an error going to the next pcap:" . $editcaperr . "\n";
  355. next;
  356. }
  357. }
  358. elsif ( $editcap_sys_signal eq 2 ) {
  359. print "editcap: system() got a ctl+c we are bailing as well\n";
  360. exit(1);
  361. }
  362. elsif ( ! $config{q} ) {
  363. print("editcap: ran successfully\n");
  364. print
  365. "******************Editcap Complete**********************\n";
  366. }
  367. }
  368. else {
  369. $fuzzedfile = $file;
  370. }
  371. #split out the path from the filename
  372. $fuzzedfiledir = dirname $fuzzedfile;
  373. $fuzzedfilename = basename $fuzzedfile;
  374. $fullcmd = "ulimit -c unlimited; ";
  375. if ( defined $valgrindopt ) {
  376. if ( $valgrindopt eq "memcheck" ) {
  377. $fullcmd =
  378. $fullcmd
  379. . "valgrind -v --log-file="
  380. . $logdir
  381. . $fuzzedfilename
  382. . $timestamp
  383. . "-memcheck-vg.log ";
  384. }
  385. elsif ( $valgrindopt eq "drd" ) {
  386. $fullcmd =
  387. $fullcmd
  388. . "valgrind --tool=drd --var-info=yes -v --log-file="
  389. . $logdir
  390. . $fuzzedfilename
  391. . $timestamp
  392. . "-drd-vg.log ";
  393. }
  394. elsif ( $valgrindopt eq "helgrind" ) {
  395. $fullcmd =
  396. $fullcmd
  397. . "valgrind --tool=helgrind -v --log-file="
  398. . $logdir
  399. . $fuzzedfilename
  400. . $timestamp
  401. . "-helgrind-vg.log ";
  402. }
  403. elsif ( $valgrindopt eq "callgrind" ) {
  404. $fullcmd =
  405. $fullcmd
  406. . "valgrind --tool=callgrind -v --callgrind-out-file="
  407. . $logdir
  408. . $fuzzedfilename
  409. . $timestamp
  410. . "-callgrind-vg.log ";
  411. }
  412. }
  413. $fullcmd =
  414. $fullcmd
  415. . $suricatabin . " -c "
  416. . $configfile . " -r "
  417. . $fuzzedfile . " -l "
  418. . $logdir;
  419. if ( defined $rules ) {
  420. if ($rules_exclusive == 1) {
  421. $fullcmd = $fullcmd . " -S " . $rules;
  422. } else {
  423. $fullcmd = $fullcmd . " -s " . $rules;
  424. }
  425. }
  426. if ( ! $config{q} ) {
  427. print "suricata: $fullcmd \n";
  428. }
  429. my $starttime = time();
  430. ( $out, $err ) = capture {
  431. system $fullcmd;
  432. $exit = $? >> 8;
  433. $suricata_sys_signal = $? & 127;
  434. $suricata_sys_coredump = $? & 128;
  435. };
  436. my $stoptime = time();
  437. my $timetotal = $stoptime - $starttime;
  438. print LOGFILE $fullcmd . ","
  439. . $timetotal . ","
  440. . $exit . ","
  441. . $suricata_sys_signal . ","
  442. . $suricata_sys_coredump . "\n";
  443. if ( ! $config{q} ) {
  444. print "suricata: exit value $exit\n";
  445. }
  446. if ( $exit ne 0 ) {
  447. my $knownerr = 0;
  448. #fuzzer genrated some random link type we can't deal with
  449. if ( $err =~
  450. /datalink type \d+ not \(yet\) supported in module PcapFile\./ )
  451. {
  452. print "suricata: we matched a known error going to the next file\n";
  453. $knownerr = 1;
  454. }
  455. elsif ( $err =~
  456. /invalid interface capture length/ )
  457. {
  458. print "suricata: we matched a known error going to the next file\n";
  459. $knownerr = 1;
  460. }
  461. if ( $knownerr eq 1 ) {
  462. if ($loopnum_per_file == 1) {
  463. $successcnt++;
  464. }
  465. print "suricata: we have run with success " . $successcnt . " times\n";
  466. if( $keeplogs eq "yes" ) {
  467. &keep_logs($fuzzedfilename);
  468. $report = $logdir . $fuzzedfilename . "-OUT.txt";
  469. &generate_report($report, $fullcmd, $out, $err, $exit, "none");
  470. }
  471. &clean_logs($fuzzedfilename,$file_was_fuzzed);
  472. }
  473. else {
  474. print "suricata: $fullcmd \n";
  475. print "suricata: exit value $exit\n";
  476. my $report = $logdir . $fuzzedfilename . "-ERR.txt";
  477. &process_core_dump();
  478. if ($core_dump) {
  479. print "core dump \n $core_dump";
  480. system( "mv "
  481. . $ENV{'PWD'}
  482. . "/core* "
  483. . $logdir
  484. . $fuzzedfilename
  485. . ".core" );
  486. &generate_report($report, $fullcmd, $out, $err, $exit, $core_dump);
  487. }else{
  488. &generate_report($report, $fullcmd, $out, $err, $exit, "none");
  489. }
  490. exit(1);
  491. }
  492. }
  493. elsif ( $suricata_sys_signal eq 2 ) {
  494. print "suricata: system() got a ctl+c we are bailing as well\n";
  495. if( $keeplogs eq "yes" ) {
  496. &keep_logs($fuzzedfilename);
  497. }
  498. &clean_logs($fuzzedfilename,$file_was_fuzzed);
  499. exit;
  500. }
  501. else {
  502. if ($loopnum_per_file == 1) {
  503. $successcnt++;
  504. }
  505. if ( ! $config{q} ) {
  506. print "suricata: we have run with success " . $successcnt . " times\n";
  507. print "******************Suricata Complete**********************\n";
  508. }
  509. if( $keeplogs eq "yes" ) {
  510. &keep_logs($fuzzedfilename);
  511. $report = $logdir . $fuzzedfilename . "-OUT.txt";
  512. &generate_report($report, $fullcmd, $out, $err, $exit, "none");
  513. }
  514. &clean_logs($fuzzedfilename,$file_was_fuzzed);
  515. if ( ! $config{q} ) {
  516. print "******************Next Pcap or Exit *******************\n";
  517. }
  518. }
  519. if ($successcnt >= $loopnum) {
  520. last;
  521. }
  522. }
  523. if ($loopnum_per_file == 0) {
  524. $successcnt++;
  525. }
  526. if ($successcnt >= $loopnum) {
  527. last;
  528. }
  529. }
  530. sub process_core_dump {
  531. my $gdbbin;
  532. my $gdb = new Devel::GDB();
  533. my $coremask = $ENV{'PWD'} . "/core*";
  534. my @coredumps = <${coremask}>;
  535. if (@coredumps eq 1 ) {
  536. my $corefile = $coredumps[0];
  537. print "gdb: core dump found $corefile processesing with";
  538. if ( $useltsuri eq "yes" ) {
  539. $gdbbin = $ltsuribin;
  540. }
  541. else {
  542. $gdbbin = $suricatabin;
  543. }
  544. print " the following bin file" . $gdbbin . "\n";
  545. $core_dump .= join '',
  546. $gdb->get("file $gdbbin"), $gdb->get("core $corefile"),
  547. $gdb->get('info threads'), $gdb->get('thread apply all bt full');
  548. print "gdb: core dump \n $core_dump";
  549. }
  550. elsif ( @coredumps > 1 ) {
  551. print "gdb: multiple core dumps, please clear all core dumps and try the test again. We found:\n";
  552. foreach my $corefile (@coredumps) {
  553. print $corefile . "\n";
  554. }
  555. }
  556. else {
  557. print "gdb: no coredumps found returning.\n";
  558. print @coredumps;
  559. print " $#coredumps" . "\n";
  560. }
  561. print "******************GDB Complete**********************\n";
  562. return;
  563. }
  564. sub clean_logs {
  565. my $deleteme = shift;
  566. my $file_was_fuzzed = shift;
  567. my $deletemerge = $logdir . $deleteme;
  568. my $rmcmd;
  569. if ( defined $editeratio and $file_was_fuzzed) {
  570. if ( unlink($deletemerge) == 1 ) {
  571. if ( ! $config{q} ) {
  572. print "clean_logs: " . $deletemerge . " deleted successfully.\n";
  573. }
  574. }
  575. else {
  576. print "clean_logs: error " . $deletemerge . " was not deleted. You may have to delete the file manually.\n";
  577. }
  578. }
  579. if ( defined $valgrindopt ) {
  580. #uncomment the following lines if you want to remove valgrind logs
  581. #$rmcmd = "rm -f " . $deletemerge . "*vg.log";
  582. #print( "running " . $rmcmd . "\n" );
  583. #system("$rmcmd");
  584. }
  585. foreach my $file (glob "$logdir/*.log $logdir/*.json") {
  586. #print $file . "\n";
  587. if (unlink($file) <= 0) {
  588. print "clean_logs: failed to delete log file $file\n";
  589. }
  590. }
  591. return;
  592. }
  593. sub keep_logs {
  594. my $saveme = shift;
  595. unless(defined($editeratio) || $loopnum eq '1'){
  596. my $saveme = $saveme . "-" . $timestamp;
  597. }
  598. my $savecmd;
  599. if (-e $logdir . "alert-debug.log"){
  600. $savecmd = "mv -f " . $logdir
  601. . "alert-debug.log "
  602. . $logdir
  603. . $saveme
  604. . "-alert-debug.log";
  605. system($savecmd);
  606. }
  607. if (-e $logdir . "fast.log"){
  608. $savecmd = "mv -f " . $logdir
  609. . "fast.log "
  610. . $logdir
  611. . $saveme
  612. . "-fast.log";
  613. system($savecmd);
  614. }
  615. if (-e $logdir . "http.log"){
  616. $savecmd = "mv -f " . $logdir
  617. . "http.log "
  618. . $logdir
  619. . $saveme
  620. . "-http.log";
  621. system($savecmd);
  622. }
  623. if (-e $logdir . "stats.log"){
  624. $savecmd = "mv -f " . $logdir
  625. . "stats.log "
  626. . $logdir
  627. . $saveme
  628. . "-stats.log";
  629. system($savecmd);
  630. }
  631. print "******************Log Move Complete**********************\n";
  632. return;
  633. }
  634. sub generate_report {
  635. my ($report, $fullcmd, $stdout, $stderr, $exit, $coredump) = ($_[0], $_[1], $_[2], $_[3], $_[4], $_[5]);
  636. open( REPORT, ">$report" ) || ( print "Could not open report file! $report\n" );
  637. print REPORT "COMMAND:$fullcmd\n";
  638. print REPORT "EXITVAL:$exit\n";
  639. print REPORT "STDERR:$stderr\n";
  640. print REPORT "STDOUT:$stdout\n";
  641. if($coredump ne "none"){
  642. print REPORT "COREDUMP:$coredump\n";
  643. }
  644. close(REPORT);
  645. }