From 69b9462d11e3d648c7310dd971f19f97123b30f2 Mon Sep 17 00:00:00 2001 From: Colum Paget Date: Tue, 3 Oct 2017 20:18:13 +0100 Subject: [PATCH] Handle passing files with bad characters to hook functions. Only run hook when appropriate (was running all the time) --- Makefile | 70 ------------------------------------- check-hash.c | 8 ++--- check.sh | 25 +++++++++++-- common.c | 40 +++++++++++---------- common.h | 4 +-- files.c | 7 +--- "tests/bad'file name\".txt" | 1 + 7 files changed, 49 insertions(+), 106 deletions(-) delete mode 100644 Makefile create mode 100644 "tests/bad'file name\".txt" diff --git a/Makefile b/Makefile deleted file mode 100644 index dfc06b9..0000000 --- a/Makefile +++ /dev/null @@ -1,70 +0,0 @@ - -CC = gcc -VERSION = -CFLAGS = -g -O2 -LIBS = -INSTALL=/bin/install -c -prefix=/usr/local -bindir=$(prefix)${exec_prefix}/bin -FLAGS=$(LDFLAGS) $(CPPFLAGS) $(CFLAGS) -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DSTDC_HEADERS=1 -D_FILE_OFFSET_BITS=64 -OBJ=common.o command-line-args.o ssh.o fingerprint.o files.o filesigning.o xattr.o cgi.o check-hash.o find.o memcached.o -EXE=hashrat - -all: hashrat - -hashrat: $(OBJ) main.c - @cd libUseful-2.5; $(MAKE) - $(CC) $(FLAGS) -o$(EXE) $(OBJ) main.c libUseful-2.5/libUseful-2.5.a $(LIBS) - -common.o: common.h common.c - $(CC) $(FLAGS) -c common.c - -fingerprint.o: fingerprint.h fingerprint.c - $(CC) $(FLAGS) -c fingerprint.c - -files.o: files.h files.c - $(CC) $(FLAGS) -c files.c - -filesigning.o: filesigning.h filesigning.c - $(CC) $(FLAGS) -c filesigning.c - -find.o: find.h find.c - $(CC) $(FLAGS) -c find.c - -check-hash.o: check-hash.h check-hash.c - $(CC) $(FLAGS) -c check-hash.c - -xattr.o: xattr.h xattr.c - $(CC) $(FLAGS) -c xattr.c - -ssh.o: ssh.h ssh.c - $(CC) $(FLAGS) -c ssh.c - -cgi.o: cgi.h cgi.c - $(CC) $(FLAGS) -c cgi.c - -memcached.o: memcached.h memcached.c - $(CC) $(FLAGS) -c memcached.c - -command-line-args.o: command-line-args.h command-line-args.c - $(CC) $(FLAGS) -c command-line-args.c - -check: hashrat - @./check.sh - -clean: - -rm -f *.o */*.o */*.a */*.so $(EXE) - -rm -f config.log config.status */config.log */config.status - -rm -fr autom4te.cache */autom4te.cache - -distclean: - -rm -f *.o */*.o */*.a */*.so $(EXE) - -rm -f config.log config.status */config.log */config.status Makefile */Makefile - -rm -fr autom4te.cache */autom4te.cache - - -install: hashrat - -mkdir -p $(DESTDIR)$(prefix)/bin - cp -f $(EXE) $(DESTDIR)$(prefix)/bin - -mkdir -p $(DESTDIR)$(prefix)/share/man/man1 - cp -f hashrat.1 $(DESTDIR)$(prefix)/share/man/man1 diff --git a/check-hash.c b/check-hash.c index 483f901..1cb1122 100644 --- a/check-hash.c +++ b/check-hash.c @@ -2,6 +2,7 @@ #include "fingerprint.h" #include "files.h" + void HandleCheckFail(char *Path, char *ErrorMessage) { char *Tempstr=NULL; @@ -9,12 +10,7 @@ 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); } diff --git a/check.sh b/check.sh index 038f7ed..1bf3ab2 100755 --- a/check.sh +++ b/check.sh @@ -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" ] @@ -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 @@ -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 @@ -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 @@ -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" diff --git a/common.c b/common.c index c4441dc..eb71d0a 100644 --- a/common.c +++ b/common.c @@ -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); } diff --git a/common.h b/common.h index 5742348..b7fbf3b 100644 --- a/common.h +++ b/common.h @@ -72,7 +72,7 @@ #define IGNORE -1 -#define VERSION "1.8.9" +#define VERSION "1.8.10" typedef struct @@ -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 diff --git a/files.c b/files.c index 10be18b..0575725 100644 --- a/files.c +++ b/files.c @@ -418,12 +418,6 @@ char *Tempstr=NULL; break; } - if (StrValid(DiffHook)) - { - Tempstr=MCopyStr(Tempstr,DiffHook," '",Path,"'",NULL); - system(Tempstr); - } - DestroyString(Tempstr); @@ -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); diff --git "a/tests/bad'file name\".txt" "b/tests/bad'file name\".txt" new file mode 100644 index 0000000..b9a818c --- /dev/null +++ "b/tests/bad'file name\".txt" @@ -0,0 +1 @@ +test file with ' in the name