Skip to content

Commit

Permalink
Handle passing files with bad characters to hook functions. Only run …
Browse files Browse the repository at this point in the history
…hook when appropriate (was running all the time)
  • Loading branch information
ColumPaget committed Oct 3, 2017
1 parent 23718e6 commit 69b9462
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 106 deletions.
70 changes: 0 additions & 70 deletions Makefile

This file was deleted.

8 changes: 2 additions & 6 deletions check-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@
#include "fingerprint.h"
#include "files.h"


void HandleCheckFail(char *Path, char *ErrorMessage)
{
char *Tempstr=NULL;

if (Flags & FLAG_COLOR) printf("%s%s: FAILED. '%s'.%s\n",ANSICode(ANSI_RED, 0, 0),Path, ErrorMessage, ANSI_NORM);
else printf("%s: FAILED. %s.\n",Path,ErrorMessage);

if (StrLen(DiffHook))
{
Tempstr=MCopyStr(Tempstr,DiffHook," '",Path,"'",NULL);
system(Tempstr);
}

RunHookScript(DiffHook, Path);
DestroyString(Tempstr);
}

Expand Down
25 changes: 22 additions & 3 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ else
fi
}


TestLocateHook()
{
rm -f locatehook.out
HR_OUT=`echo $1 | ./hashrat -m -r . -hook "echo found > locatehook.out"`

if [ -e locatehook.out ]
then
OkayMessage "$3 works."
else
FailMessage "$3 BROKEN."
fi
}



TestExitCodes()
{
if [ "$4" = "FindDuplicates" ]
Expand Down Expand Up @@ -137,7 +153,7 @@ TestHash z85 "ZEROMQ85 encoding" "wX%ElWFTQ9+Z=X4h"
Title "Testing Misc. Features"

HR_OUT=`./hashrat -version`
if [ "$HR_OUT" = "version: 1.8.7" ]
if [ "$HR_OUT" = "version: 1.8.10" ]
then
OkayMessage "Version (-version) works"
else
Expand All @@ -154,7 +170,7 @@ else
fi

HR_OUT=`./hashrat -dir -sha1 -trad tests`
if [ "$HR_OUT" = "b8b4058dc499ee1f330926a5a073a2c598b10c91 tests" ]
if [ "$HR_OUT" = "9521674698e62496698c42f63c9cde9bc6399a03 tests" ]
then
OkayMessage "Directory hashing works"
else
Expand All @@ -163,7 +179,7 @@ fi


HR_OUT=`./hashrat -sha1 -trad -r tests | ./hashrat -sha1`
if [ "$HR_OUT" = "06af1d9f777bbeb1eecd76d71d869089683ded1b" ]
if [ "$HR_OUT" = "a31f3b27b58f8b26a3c2237beb9de410d1117fba" ]
then
OkayMessage "Recursive file hashing works"
else
Expand Down Expand Up @@ -198,6 +214,9 @@ TestLocate "MD5 (test file) = 6ec9de513a8ff1768eb4768236198cf3" "LOCATED: 6ec9de
HR_INPUT=`cat tests/test.ioc`
TestLocate "$HR_INPUT" "LOCATED: 6ec9de513a8ff1768eb4768236198cf3 ' Hashrat Test IOC' at ./tests/help.txt" "Locating files with OpenIOC input"

Title "Test hook functions"
TestLocateHook "hash='md5:6ec9de513a8ff1768eb4768236198cf3' mode='100644' uid='0' gid='0' size='621' mtime='1423180289' inode='2359456' path='test file'" "" "Hook function for file locate"
TestLocateHook "hash='md5:6933ee7eb504d29312b23a47d2dac374' mode='100644' uid='0' gid='0' size='621' mtime='1423180289' inode='2359456' path='test file'" "" "Hook function for file locate of files with bad characters in name"

Title "Testing exit codes for different operations"

Expand Down
40 changes: 21 additions & 19 deletions common.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,30 +127,32 @@ char *Tempstr=NULL;
}


void HandleCompareResult(char *Path, char *Status, int Flags, char *ErrorMessage)
void RunHookScript(const char *Hook, const char *Path)
{
char *Tempstr=NULL;
int Color=0;
char *Tempstr=NULL, *Quoted=NULL;
STREAM *S;

if (Flags & FLAG_COLOR)
{
switch (Flags & FLAG_RESULT_MASK)
if (StrValid(Hook))
{
//must quote twice to get through system comamnd
Quoted=QuoteCharsInStr(Quoted, Path,"\"'`!|;<> ");
S=STREAMSpawnCommand("/bin/sh","","",0);
if (S)
{
case RESULT_FAIL: Color=ANSI_RED; break;
case RESULT_PASS: Color=ANSI_GREEN; break;
case RESULT_WARN: Color=ANSI_YELLOW; break;
Tempstr=MCopyStr(Tempstr, DiffHook," ",Quoted,";exit\n",NULL);
STREAMWriteLine(Tempstr,S);
STREAMFlush(S);

Tempstr=STREAMReadLine(Tempstr,S);
while (Tempstr)
{
printf("%s\n",Tempstr);
Tempstr=STREAMReadLine(Tempstr,S);
}
}
}

if (Color > 0) printf("%s%s: %s. '%s'.%s\n",ANSICode(ANSI_RED, 0, 0),Status, Path, ErrorMessage, ANSI_NORM);
else printf("%s: %s. %s.\n",Status, Path,ErrorMessage);

if ((Flags & RESULT_RUNHOOK) && StrLen(DiffHook))
{
Tempstr=MCopyStr(Tempstr,DiffHook," '",Path,"'",NULL);
system(Tempstr);
}

DestroyString(Tempstr);
DestroyString(Tempstr);
DestroyString(Quoted);
}

4 changes: 2 additions & 2 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

#define IGNORE -1

#define VERSION "1.8.9"
#define VERSION "1.8.10"


typedef struct
Expand Down Expand Up @@ -115,6 +115,6 @@ TFingerprint *TFingerprintCreate(const char *Hash, const char *HashType, const c
void HashratCtxDestroy(void *p_Ctx);
void HashratStoreHash(HashratCtx *Ctx, char *Path, struct stat *Stat, char *Hash);
int HashratOutputInfo(HashratCtx *Ctx, STREAM *S, char *Path, struct stat *Stat, char *Hash);
void HandleCompareResult(char *Path, char *Status, int Flags, char *ErrorMessage);
void RunHookScript(const char *Hook, const char *Path);

#endif
7 changes: 1 addition & 6 deletions files.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,6 @@ char *Tempstr=NULL;
break;
}

if (StrValid(DiffHook))
{
Tempstr=MCopyStr(Tempstr,DiffHook," '",Path,"'",NULL);
system(Tempstr);
}


DestroyString(Tempstr);

Expand Down Expand Up @@ -532,6 +526,7 @@ case ACT_FINDMATCHES_MEMCACHED:
MatchCount++;
//here we return true if a match found
result=TRUE;
RunHookScript(DiffHook, Path);
}
else DiffCount++;
TFingerprintDestroy(FP);
Expand Down
1 change: 1 addition & 0 deletions tests/bad'file name".txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test file with ' in the name

0 comments on commit 69b9462

Please sign in to comment.