Skip to content

Commit

Permalink
Added 'hash chaining' where hash operations can be combined. Fixed ex…
Browse files Browse the repository at this point in the history
…it codes for most operations
  • Loading branch information
ColumPaget committed Sep 22, 2016
1 parent 70c7665 commit 8250776
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 202 deletions.
5 changes: 5 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Options:
-? Print this help
--version Print program version
-version Print program version
-type <type> Use hash algorithmn <type>. Types can be chained together as a comma-seperated list.
-md5 Use md5 hash algorithmn
-sha1 Use sha1 hash algorithmn
-sha256 Use sha256 hash algorithmn
Expand Down Expand Up @@ -149,6 +150,10 @@ USE EXAMPLES:

Read lines from stdin, and generate an md5 hash in 'traditional' format for every line INCLUDING TRAILING WHITESPACE. This is compatible with 'echo text | md5sum' where 'text' is one line, as 'echo' adds a newline to the end of the text it outputs.

hashrat -type sha256,whirl,md5

Read data from stdin, hash it with sha256, then hash the resulting hash with whirlpool, then with md5

hashrat *

Generate a list of hashes for files in the current directory (default hash type is md5).
Expand Down
7 changes: 5 additions & 2 deletions check-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ int CheckFileHash(HashratCtx *Ctx, char *Path, struct stat *Stat, char *ActualHa
int result=FALSE;

if (strcasecmp(FP->Hash,ActualHash) != 0) HandleCheckFail(Path, "Hash mismatch");
else
{
if (! (Flags & FLAG_OUTPUT_FAILS))
{
Expand Down Expand Up @@ -118,6 +119,7 @@ return(result);
}


//returns true on a significant event, meaning on FAIL
int CheckHashesFromList(HashratCtx *Ctx)
{
char *HashStr=NULL, *ptr;
Expand Down Expand Up @@ -150,7 +152,8 @@ while (FP)
fprintf(stderr,"\nChecked %d files. %d Failures\n",Checked,Errors);

DestroyString(HashStr);
if (Errors) return(FALSE);
return(TRUE);

if (Errors) return(TRUE);
return(FALSE);
}

33 changes: 31 additions & 2 deletions check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ else
fi
}

TestExitCodes()
{
if [ "$4" = "FindDuplicates" ]
then
HR_OUT=`./hashrat -r -dups $1`
EXIT_FOUND=$?
HR_OUT=`./hashrat -r -dups $2`
EXIT_NOTFOUND=$?
else
HR_OUT=`echo $1 $2 | ./hashrat $3 2>/dev/null`
EXIT_FOUND=$?
HR_OUT=`echo $1x $2 | ./hashrat $3 2>/dev/null`
EXIT_NOTFOUND=$?
fi

if [ "$EXIT_FOUND" = "0" -a "$EXIT_NOTFOUND" = "1" ]
then
OkayMessage "$4 exit codes correct"
else
FailMessage "$4 exit codes BROKEN."
fi
}



##################### MAIN STARTS HERE ##########################
Expand Down Expand Up @@ -114,7 +137,7 @@ TestHash z85 "ZEROMQ85 encoding" "wX%ElWFTQ9+Z=X4h"
Title "Testing Misc. Features"

HR_OUT=`./hashrat -version`
if [ "$HR_OUT" = "version: 1.8.2" ]
if [ "$HR_OUT" = "version: 1.8.3" ]
then
OkayMessage "Version (-version) works"
else
Expand Down Expand Up @@ -157,7 +180,7 @@ else
FailMessage "Checking files BROKEN"
fi

HR_OUT=`./hashrat -r -dups tests`
HR_OUT=`./hashrat -r -dups tests`
if [ "$HR_OUT" = "DUPLICATE: tests/quotes.txt of tests/duplicate.txt " ]
then
OkayMessage "Finding duplicate files works"
Expand All @@ -176,4 +199,10 @@ HR_INPUT=`cat tests/test.ioc`
TestLocate "$HR_INPUT" "LOCATED: 6ec9de513a8ff1768eb4768236198cf3 ' Hashrat Test IOC' at ./tests/help.txt" "Locating files with OpenIOC input"


Title "Testing exit codes for different operations"

TestExitCodes "6ec9de513a8ff1768eb4768236198cf3" "tests/help.txt" "-cf" "CheckHash"
TestExitCodes "6ec9de513a8ff1768eb4768236198cf3" "tests/help.txt" "-m -r ." "Locate"
TestExitCodes "tests" "libUseful-2.5" "-r -dups" "FindDuplicates"

exit $EXIT
6 changes: 6 additions & 0 deletions command-line-args.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ else if (strcmp(argv[i],"-jh256")==0) ParseFlags |= CommandLineHandleArg(argc, a
else if (strcmp(argv[i],"-jh384")==0) ParseFlags |= CommandLineHandleArg(argc, argv, i, 0, 0, "HashType", "jh-384",Ctx->Vars);
else if (strcmp(argv[i],"-jh512")==0) ParseFlags |= CommandLineHandleArg(argc, argv, i, 0, 0, "HashType", "jh-512",Ctx->Vars);
else if (strcmp(argv[i],"-jh")==0) ParseFlags |= CommandLineHandleArg(argc, argv, i, 0, 0, "HashType", "jh-512",Ctx->Vars);
else if (strcmp(argv[i],"-type")==0)
{
strcpy(argv[i],"");
i++;
ParseFlags |= CommandLineHandleArg(argc, argv, i, 0, 0, "HashType", argv[i],Ctx->Vars);
}
//else if (strcmp(argv[i],"-crc32")==0) ParseFlags |= CommandLineHandleArg(argc, argv, i, 0, 0, "HashType", "crc32",Ctx->Vars);
else if (strcmp(argv[i],"-8")==0) CommandLineSetCtx(argc, argv, i, Ctx, 0, ENCODE_OCTAL);
else if (strcmp(argv[i],"-10")==0) CommandLineSetCtx(argc, argv, i, Ctx, 0, ENCODE_DECIMAL);
Expand Down
4 changes: 3 additions & 1 deletion common.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@

#define BLOCKSIZE 4096

#define VERSION "1.8.2"
#define IGNORE -1

#define VERSION "1.8.3"


typedef struct
Expand Down
72 changes: 51 additions & 21 deletions files.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,14 @@ void HashratFinishHash(char **RetStr, HashratCtx *Ctx, THash *Hash)
int val;
char *ptr;

Hash->Finish(Hash,Ctx->Encoding,RetStr);
HashFinish(Hash,Ctx->Encoding,RetStr);

ptr=GetVar(Ctx->Vars,"Output:Length");
if (StrValid(ptr))
{
val=atoi(ptr);
if ((val > 0) && (StrLen(*RetStr) > val)) (*RetStr)[val]='\0';
}
HashDestroy(Hash);
}


Expand Down Expand Up @@ -297,11 +296,14 @@ THash *Hash;
if (! Ctx->Hash)
{
Hash=HashInit(Ctx->HashType);
if (Hash)
{
ptr=GetVar(Ctx->Vars,"EncryptionKey");
if (ptr) HMACSetKey(Hash, ptr, StrLen(ptr));

Hash->Update(Hash ,Data, DataLen);
HashratFinishHash(RetStr, Ctx, Hash);
}
}
else Ctx->Hash->Update(Ctx->Hash ,Data, DataLen);
}
Expand Down Expand Up @@ -414,24 +416,28 @@ return(0);




void HashratAction(HashratCtx *Ctx, char *Path, struct stat *Stat)
//HashratAction returns true on a significant event, which is either an item found in search
//or a check failing in hash-checking mode
int HashratAction(HashratCtx *Ctx, char *Path, struct stat *Stat)
{
char *HashStr=NULL;
int Type, result=FALSE;
TFingerprint *FP;
int Type;

switch (Ctx->Action)
{
case ACT_HASHDIR:
Type=FileType(Path, Flags, Stat);
HashratHashFile(Ctx, Ctx->Hash, Type, Path, Stat->st_size);
//we return TRUE if hash succeeded
if (HashratHashFile(Ctx, Ctx->Hash, Type, Path, Stat->st_size)) result=TRUE;
break;

case ACT_HASH:
HashItem(Ctx, Ctx->HashType, Path, Stat, &HashStr);
HashratOutputInfo(Ctx, Ctx->Out, Path, Stat, HashStr);
HashratStoreHash(Ctx, Path, Stat, HashStr);
//we return TRUE if hash succeeded
result=TRUE;
break;

case ACT_CHECK:
Expand All @@ -441,11 +447,13 @@ case ACT_CHECK:
{
HashItem(Ctx, Ctx->HashType, Path, Stat, &HashStr);
FP=CheckForMatch(Ctx, Path, Stat, HashStr);
if (FP)
if (FP && HashratCheckFile(Ctx, Path, Stat, HashStr, FP)) MatchCount++;
else
{
if (HashratCheckFile(Ctx, Path, Stat, HashStr, FP)) MatchCount++;
HandleCheckFail(Path, "Changed or new");
//we return TRUE on FAILURE, as we are signaling a significant event
result=TRUE;
}
else HandleCheckFail(Path, "Changed or new");
TFingerprintDestroy(FP);
}
else if (Flags & FLAG_VERBOSE) fprintf(stderr,"ZERO LENGTH FILE: %s\n",Path);
Expand All @@ -455,20 +463,27 @@ break;
case ACT_CHECK_XATTR:
if (S_ISREG(Stat->st_mode))
{
//result == TRUE by default (TRUE==Signficant event, here meaning 'check failed')
result=TRUE;
FP=XAttrLoadHash(Ctx, Path);
if (FP)
{
HashItem(Ctx, FP->HashType, Path, Stat, &HashStr);
if (FP->Flags & FP_HASSTAT) HashratCheckFile(Ctx, Path, Stat, HashStr, FP);
else HashratCheckFile(Ctx, Path, Stat, HashStr, FP);
if (FP->Flags & FP_HASSTAT) if (HashratCheckFile(Ctx, Path, Stat, HashStr, FP)) result=FALSE;
else if (HashratCheckFile(Ctx, Path, Stat, HashStr, FP)) result=FALSE;
}
else fprintf(stderr,"ERROR: No stored hash for '%s'\n",Path);
}
else fprintf(stderr,"ERROR: Not regular file '%s'. Not checking in xattr mode.\n",Path);
break;


case ACT_CHECK_MEMCACHED:
if (S_ISREG(Stat->st_mode))
{
//result == TRUE by default (TRUE==Signficant event, here meaning 'check failed')
result=TRUE;

if (Stat->st_size > 0)
{
HashItem(Ctx, Ctx->HashType, Path, Stat, &HashStr);
Expand All @@ -477,12 +492,13 @@ case ACT_CHECK_MEMCACHED:
else FP->Path=MCopyStr(FP->Path,"hashrat://",LocalHost,Path,NULL);
FP->Hash=MemcachedGet(FP->Hash, FP->Path);

if (FP) HashratCheckFile(Ctx, Path, NULL, HashStr, FP);
if (FP && HashratCheckFile(Ctx, Path, NULL, HashStr, FP)) result=FALSE;
else fprintf(stderr,"ERROR: No stored hash for '%s'\n",Path);
TFingerprintDestroy(FP);
}
else if (Flags & FLAG_VERBOSE) fprintf(stderr,"ZERO LENGTH FILE: %s\n",Path);
}
else fprintf(stderr,"ERROR: Not regular file '%s'. Not checking in memcached mode.\n",Path);
break;

case ACT_FINDMATCHES:
Expand All @@ -498,6 +514,8 @@ case ACT_FINDMATCHES_MEMCACHED:
if (StrValid(FP->Path) || StrValid(FP->Data)) printf("LOCATED: %s '%s %s' at %s\n",FP->Hash, FP->Path, FP->Data, Path);
else printf("LOCATED: %s at %s\n",FP->Hash, Path);
MatchCount++;
//here we return true if a match found
result=TRUE;
}
else DiffCount++;
TFingerprintDestroy(FP);
Expand All @@ -518,6 +536,8 @@ case ACT_FINDDUPLICATES:
{
printf("DUPLICATE: %s of %s %s\n",Path,FP->Path,FP->Data);
MatchCount++;
//here we return true if a match found
result=TRUE;
TFingerprintDestroy(FP);
}
else
Expand All @@ -534,16 +554,19 @@ break;
}

DestroyString(HashStr);
}

return(result);
}


//ProcessItem returns TRUE on a significant event, so any instance of TRUE
//from items checked makes return value here TRUE
int ProcessDir(HashratCtx *Ctx, char *Dir, char *HashType)
{
char *Tempstr=NULL, *HashStr=NULL;
ListNode *FileList, *Curr;
int result=FALSE;
int Type;
int result=TRUE;

Type=FileType(Dir, Flags, NULL);

Expand All @@ -553,7 +576,7 @@ int result=TRUE;
Curr=ListGetNext(FileList);
while (Curr)
{
ProcessItem(Ctx, Curr->Tag, (struct stat *) Curr->Item);
if (ProcessItem(Ctx, Curr->Tag, (struct stat *) Curr->Item)) result=TRUE;
Curr=ListGetNext(Curr);
}

Expand All @@ -566,11 +589,13 @@ return(result);
}



int HashratRecurse(HashratCtx *Ctx, char *Path, char **HashStr, int result)
//ProcessDir returns TRUE on a significant event, so any instance of TRUE
//from items checked makes return value here TRUE
int HashratRecurse(HashratCtx *Ctx, char *Path, char **HashStr)
{
char *ptr;
struct stat FStat;
int result=FALSE;

if ((Ctx->Action == ACT_HASHDIR) && (! Ctx->Hash))
{
Expand All @@ -582,35 +607,40 @@ struct stat FStat;
HashratFinishHash(HashStr, Ctx, Ctx->Hash);
stat(Path, &FStat);
HashratOutputInfo(Ctx, Ctx->Out, Path, &FStat, *HashStr);
result=TRUE;
}
else if (! ProcessDir(Ctx, Path, Ctx->HashType)) result=FALSE;
else if (ProcessDir(Ctx, Path, Ctx->HashType)) result=TRUE;

return(result);
}




void ProcessItem(HashratCtx *Ctx, char *Path, struct stat *Stat)
int ProcessItem(HashratCtx *Ctx, char *Path, struct stat *Stat)
{
char *HashStr=NULL;
int result=FALSE;

switch (ConsiderItem(Ctx, Path, Stat))
{
case CTX_EXCLUDE:
case CTX_ONE_FS:
result=IGNORE;
break;

case CTX_RECURSE:
HashratRecurse(Ctx, Path, &HashStr, 0);
result=HashratRecurse(Ctx, Path, &HashStr);
break;

default:
HashratAction(Ctx, Path, Stat);
result=HashratAction(Ctx, Path, Stat);
break;
}

DestroyString(HashStr);

return(result);
}


Expand Down
2 changes: 1 addition & 1 deletion files.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int StatFile(HashratCtx *Ctx, char *Path, struct stat *Stat);
int HashSingleFile(char **RetStr, HashratCtx *Ctx, int Type,char *Path);
void ProcessData(char **RetStr, HashratCtx *Ctx, char *Data, int DataLen);
int HashItem(HashratCtx *Ctx, char *HashType, char *Path, struct stat *FStat, char **HashStr);
void ProcessItem(HashratCtx *Ctx, char *Path, struct stat *Stat);
int ProcessItem(HashratCtx *Ctx, char *Path, struct stat *Stat);
int ProcessDir(HashratCtx *Ctx, char *Dir, char *HashType);

void HashratFinishHash(char **RetStr, HashratCtx *Ctx, THash *Hash);
Expand Down
3 changes: 1 addition & 2 deletions filesigning.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,9 @@ while (Tempstr)
if (strncmp(Tempstr, "hashrat-integrity-mark: ",24)==0)
{
tmpHash=Hash->Clone(Hash);
tmpHash->Finish(tmpHash,ENCODE_BASE64,&HashStr);
HashFinish(tmpHash,ENCODE_BASE64,&HashStr);

HashratOutputSigningCheck(Ctx, HashStr, Tempstr, LineCount);
HashDestroy(tmpHash);
}
Hash->Update(Hash ,Tempstr, StrLen(Tempstr));

Expand Down
Loading

0 comments on commit 8250776

Please sign in to comment.