| 1 |
#!/usr/bin/perl -w |
|---|
| 2 |
|
|---|
| 3 |
use strict; |
|---|
| 4 |
use Test::More 'no_plan'; |
|---|
| 5 |
$| = 1; |
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
# =begin testing |
|---|
| 10 |
{ |
|---|
| 11 |
# Make sure HoneyClient::Agent::Integrity loads. |
|---|
| 12 |
BEGIN { use_ok('HoneyClient::Agent::Integrity', qw(initAll checkAll initRegistry checkRegistry initFileSystem checkFileSystem)) or diag("Can't load HoneyClient::Util::Config package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 13 |
require_ok('HoneyClient::Agent::Integrity'); |
|---|
| 14 |
#can_ok('HoneyClient::Agent::Integrity', 'new'); |
|---|
| 15 |
can_ok('HoneyClient::Agent::Integrity', 'initAll'); |
|---|
| 16 |
can_ok('HoneyClient::Agent::Integrity', 'checkAll'); |
|---|
| 17 |
can_ok('HoneyClient::Agent::Integrity', 'initFileSystem'); |
|---|
| 18 |
can_ok('HoneyClient::Agent::Integrity', 'checkFileSystem'); |
|---|
| 19 |
can_ok('HoneyClient::Agent::Integrity', 'initRegistry'); |
|---|
| 20 |
can_ok('HoneyClient::Agent::Integrity', 'checkRegistry'); |
|---|
| 21 |
use HoneyClient::Agent::Integrity qw(initAll checkAll initRegistry checkRegistry initFileSystem checkFileSystem); |
|---|
| 22 |
|
|---|
| 23 |
# Make sure HoneyClient::Util::Config loads. |
|---|
| 24 |
BEGIN { use_ok('HoneyClient::Util::Config', qw(getVar)) or diag("Can't load HoneyClient::Util::Config package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 25 |
require_ok('HoneyClient::Util::Config'); |
|---|
| 26 |
can_ok('HoneyClient::Util::Config', 'getVar'); |
|---|
| 27 |
use HoneyClient::Util::Config qw(getVar); |
|---|
| 28 |
|
|---|
| 29 |
# Make sure File::Find loads. |
|---|
| 30 |
BEGIN { use_ok('File::Find', qw(find)) or diag("Can't load File::Find package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 31 |
require_ok('File::Find'); |
|---|
| 32 |
can_ok('File::Find', 'find'); |
|---|
| 33 |
use File::Find; |
|---|
| 34 |
|
|---|
| 35 |
# Make sure Digest::MD5 loads. |
|---|
| 36 |
#BEGIN { use_ok('Digest::MD5', qw(new)) or diag("Can't load Digest::MD5 package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 37 |
#require_ok('Digest::MD5'); |
|---|
| 38 |
#use Digest::MD5; |
|---|
| 39 |
|
|---|
| 40 |
# Make sure MIME::Base64 loads. |
|---|
| 41 |
BEGIN { use_ok('MIME::Base64', qw(encode_base64 decode_base64)) or diag("Can't load MIME::Base64 package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 42 |
require_ok('MIME::Base64'); |
|---|
| 43 |
can_ok('MIME::Base64', 'encode_base64'); |
|---|
| 44 |
can_ok('MIME::Base64', 'decode_base64'); |
|---|
| 45 |
use MIME::Base64 qw(encode_base64 decode_base64); |
|---|
| 46 |
|
|---|
| 47 |
# Make sure Storable loads. |
|---|
| 48 |
BEGIN { use_ok('Storable', qw(dclone)) or diag("Can't load Storable package. Check to make sure the package library is correctly listed within the path."); } |
|---|
| 49 |
require_ok('Storable'); |
|---|
| 50 |
can_ok('Storable', 'dclone'); |
|---|
| 51 |
use Storable qw(dclone); |
|---|
| 52 |
|
|---|
| 53 |
###Testing Globals### |
|---|
| 54 |
# Directory where the known-good test files are stored |
|---|
| 55 |
$test_dir = getVar(name => "test_dir", namespace => "HoneyClient::Agent::Integrity"); |
|---|
| 56 |
|
|---|
| 57 |
# List of files and directories to check during filesystem checking |
|---|
| 58 |
$file_checklist = getVar(name => "file_checklist", namespace => "HoneyClient::Agent::Integrity"); |
|---|
| 59 |
|
|---|
| 60 |
# List of files or directories to exclude if found in subdirs during |
|---|
| 61 |
# filesystem check. |
|---|
| 62 |
$file_exclude = getVar(name => "file_exclude", namespace => "HoneyClient::Agent::Integrity"); |
|---|
| 63 |
|
|---|
| 64 |
# File where found changes are written to |
|---|
| 65 |
$change_file = getVar(name => "change_file", namespace => "HoneyClient::Agent::Integrity"), |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
# =begin testing |
|---|
| 71 |
{ |
|---|
| 72 |
#Testing initFileSystem(); |
|---|
| 73 |
|
|---|
| 74 |
my $ob = HoneyClient::Agent::Integrity->new(); |
|---|
| 75 |
|
|---|
| 76 |
system("mkdir /tmp/hc_test_dir"); |
|---|
| 77 |
system("echo hi > /tmp/hc_test_dir/hi.txt"); |
|---|
| 78 |
system("echo /tmp/hc_test_dir/hi.txt > $file_checklist"); |
|---|
| 79 |
system("echo /tmp/hc_test_dir/hi.txt > $file_exclude"); |
|---|
| 80 |
$ob->initFileSystem(); |
|---|
| 81 |
open (FILE, "cleanfile.txt") or die "Can't check the cleanfile.txt\n"; |
|---|
| 82 |
@result = <FILE>; |
|---|
| 83 |
close FILE; |
|---|
| 84 |
#Bad test because it will be empty in the case of an error anyway? |
|---|
| 85 |
is(scalar(@result), 0, 'initFileSystem: Explicit Filesystem Omission'); |
|---|
| 86 |
|
|---|
| 87 |
system("rm $file_exclude"); |
|---|
| 88 |
system("echo hi > /tmp/hc_test_dir/hi.txt"); |
|---|
| 89 |
system("echo /tmp/hc_test_dir/hi.txt > $file_checklist"); |
|---|
| 90 |
system("echo /tmp/hc_test_dir/ > $file_exclude"); |
|---|
| 91 |
$ob->initFileSystem(); |
|---|
| 92 |
open (FILE, "cleanfile.txt") or die "Can't check the cleanfile.txt\n"; |
|---|
| 93 |
@result = <FILE>; |
|---|
| 94 |
close FILE; |
|---|
| 95 |
#Bad test because it will be empty in the case of an error anyway? |
|---|
| 96 |
is(scalar(@result), 0, 'initFileSystem: Directory Filesystem Omission'); |
|---|
| 97 |
|
|---|
| 98 |
system("rm $file_exclude"); |
|---|
| 99 |
system("echo hi > /tmp/hc_test_dir/hi.txt"); |
|---|
| 100 |
system("echo /tmp/hc_test_dir/hi.txt > $file_checklist"); |
|---|
| 101 |
$ob->initFileSystem(); |
|---|
| 102 |
open (DIFF, "diff $test_dir/fs1.txt cleanfile.txt |") or die "Can't check the cleanfile.txt\n"; |
|---|
| 103 |
@result = <DIFF>; |
|---|
| 104 |
close DIFF; |
|---|
| 105 |
#Bad test because it will be empty in the case of an error anyway? |
|---|
| 106 |
is(scalar(@result), 0, 'initFileSystem: Known-good file hash'); |
|---|
| 107 |
|
|---|
| 108 |
system("rm -rf /tmp/hc_test_dir/"); |
|---|
| 109 |
system("rm $file_checklist"); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
# =begin testing |
|---|
| 115 |
{ |
|---|
| 116 |
#Testing that checkFileSystem() |
|---|
| 117 |
|
|---|
| 118 |
my $ob = HoneyClient::Agent::Integrity->new(); |
|---|
| 119 |
my @result; |
|---|
| 120 |
|
|---|
| 121 |
#add |
|---|
| 122 |
system("rm $change_file"); |
|---|
| 123 |
system("mkdir /tmp/hc_test_dir/"); |
|---|
| 124 |
system("echo hi > /tmp/hc_test_dir/hi.txt"); |
|---|
| 125 |
system("echo /tmp/hc_test_dir/ > $file_checklist"); |
|---|
| 126 |
$ob->initFileSystem(); |
|---|
| 127 |
system("echo hi > /tmp/hc_test_dir/hi2.txt"); |
|---|
| 128 |
$ob->checkFileSystem(); |
|---|
| 129 |
open(CHECK, "diff $test_dir/fs2.txt $change_file |") or die "There was a problem doing the fs2.txt diff"; |
|---|
| 130 |
#XXX Won't the die statement just be masked by the redirection of stdout/stderr? |
|---|
| 131 |
@result = <CHECK>; |
|---|
| 132 |
close(CHECK); |
|---|
| 133 |
is(scalar(@result), 0, "checkFileSystem: Files added"); |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
#delete |
|---|
| 137 |
system("rm $change_file"); |
|---|
| 138 |
system("rm /tmp/hc_test_dir/hi.txt"); |
|---|
| 139 |
$ob->checkFileSystem(); |
|---|
| 140 |
open(CHECK, "diff $test_dir/fs3.txt $change_file |") or die "There was a problem doing the fs2.txt diff"; |
|---|
| 141 |
#XXX Won't the die statement just be masked by the redirection of stdout/stderr? |
|---|
| 142 |
@result = <CHECK>; |
|---|
| 143 |
close(CHECK); |
|---|
| 144 |
is(scalar(@result), 0, "checkFileSystem: Files deleted"); |
|---|
| 145 |
|
|---|
| 146 |
#change |
|---|
| 147 |
system("rm $change_file"); |
|---|
| 148 |
system("echo again >> /tmp/hc_test_dir/hi.txt"); |
|---|
| 149 |
$ob->checkFileSystem(); |
|---|
| 150 |
open(CHECK, "diff $test_dir/fs4.txt $change_file |") or die "There was a problem doing the fs2.txt diff"; |
|---|
| 151 |
#XXX Won't the die statement just be masked by the redirection of stdout/stderr? |
|---|
| 152 |
@result = <CHECK>; |
|---|
| 153 |
close(CHECK); |
|---|
| 154 |
is(scalar(@result), 0, "checkFileSystem: Files changed"); |
|---|
| 155 |
|
|---|
| 156 |
system("rm -rf /tmp/hc_test_dir/"); |
|---|
| 157 |
system("rm $file_checklist"); |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 |
# =begin testing |
|---|
| 163 |
{ |
|---|
| 164 |
#Testing initRegistry() |
|---|
| 165 |
my $ob = HoneyClient::Agent::Integrity->new(); |
|---|
| 166 |
|
|---|
| 167 |
system("regedit.exe /s noTEST.reg"); |
|---|
| 168 |
system("regedit.exe /s /c $test_dir/t1a.reg"); |
|---|
| 169 |
$ob->initRegistry("HKEY_LOCAL_MACHINE\\HARDWARE\\TEST"); |
|---|
| 170 |
open (DIFF, "diff $test_dir/t1a.reg clean.reg0 |") or die "Can't check the changes files\n"; |
|---|
| 171 |
@result = <DIFF>; |
|---|
| 172 |
close DIFF; |
|---|
| 173 |
#Bad test because it will be empty in the case of an error anyway? |
|---|
| 174 |
is(scalar(@result), 0, 'initRegistry: General Test'); |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
|
|---|
| 179 |
# =begin testing |
|---|
| 180 |
{ |
|---|
| 181 |
my $ob = HoneyClient::Agent::Integrity->new(); |
|---|
| 182 |
|
|---|
| 183 |
reg_test($ob, 1, "checkRegistry: case 1 Multi-line addition changes."); |
|---|
| 184 |
reg_test($ob, 2, "checkRegistry: case 2 Single-line addition changes."); |
|---|
| 185 |
reg_test($ob, 3, "checkRegistry: case 3 Multi-line deletion changes."); |
|---|
| 186 |
reg_test($ob, 4, "checkRegistry: case 4 Single-line deletion changes."); |
|---|
| 187 |
reg_test($ob, 5, "checkRegistry: case 5 Simple multi-line to multi-line changes."); |
|---|
| 188 |
is(6, 6, "checkRegistry: case 6 - SKIPPING (currently can't recreate conditions for test)"); |
|---|
| 189 |
#reg_test($ob, 6, "checkRegistry: case 6 Complicated multi-line to multi-line changes."); |
|---|
| 190 |
reg_test($ob, 7, "checkRegistry: case 7 Simple multi-line to single-line changes."); |
|---|
| 191 |
reg_test($ob, 8, "checkRegistry: case 8 Complicated multi-line to single-line changes."); |
|---|
| 192 |
reg_test($ob, 9, "checkRegistry: case 9 Simple single-line to multi-line changes."); |
|---|
| 193 |
reg_test($ob, 10, "checkRegistry: case 10 Complicated single-line to multi-line changes."); |
|---|
| 194 |
reg_test($ob, 11, "checkRegistry: case 11 Simple single-line to single-line changes."); |
|---|
| 195 |
reg_test($ob, 12, "checkRegistry: case12 Complicated single-line to single-line changes."); |
|---|
| 196 |
|
|---|
| 197 |
sub reg_test{ |
|---|
| 198 |
my $ob = shift; |
|---|
| 199 |
my $num = shift; |
|---|
| 200 |
my $string = shift; |
|---|
| 201 |
|
|---|
| 202 |
#for safety |
|---|
| 203 |
if(-e "temp_reg_export.reg"){ |
|---|
| 204 |
system("mv temp_reg_export.reg temp_reg_export.reg.CBL"); |
|---|
| 205 |
} |
|---|
| 206 |
system('regedit.exe /a temp_reg_export.reg "HKEY_LOCAL_MACHINE\HARDWARE\TEST"'); |
|---|
| 207 |
|
|---|
| 208 |
system("regedit.exe /s noTEST.reg"); |
|---|
| 209 |
system("regedit.exe /s /c $test_dir/t" . "$num" . "a.reg"); |
|---|
| 210 |
$ob->initRegistry("HKEY_LOCAL_MACHINE\\HARDWARE"); |
|---|
| 211 |
system("regedit.exe /s noTEST.reg"); |
|---|
| 212 |
system("regedit.exe /s /c $test_dir/t" . "$num" . "b.reg"); |
|---|
| 213 |
$ob->checkRegistry("HKEY_LOCAL_MACHINE\\HARDWARE"); |
|---|
| 214 |
open (DIFF, "diff $test_dir/t" . "$num" . "changes.txt changes.txt |") or die "Can't check the changes files\n"; |
|---|
| 215 |
@result = <DIFF>; |
|---|
| 216 |
close DIFF; |
|---|
| 217 |
#Bad test because it will be empty in the case of an error anyway? |
|---|
| 218 |
is(scalar(@result), 0, "$string"); |
|---|
| 219 |
|
|---|
| 220 |
#for safety/cleanup |
|---|
| 221 |
if(-e "temp_reg_export.reg"){ |
|---|
| 222 |
system("regedit.exe /s noTEST.reg"); |
|---|
| 223 |
system("regedit.exe /s /c temp_reg_export.reg"); |
|---|
| 224 |
system("rm temp_reg_export.reg"); |
|---|
| 225 |
if(-e "temp_reg_export.reg.CBL"){ |
|---|
| 226 |
system("mv temp_reg_export.reg.CBL temp_reg_export.reg"); |
|---|
| 227 |
} |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
} |
|---|
| 231 |
} |
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
1; |
|---|