Skip to content

Commit

Permalink
end of file bugfix, stability impr., cmdline impr.
Browse files Browse the repository at this point in the history
  • Loading branch information
srozb committed Jul 18, 2022
1 parent 9499bee commit 1006839
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion entgrep.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.1.0"
version = "0.2.0"
author = "srozb"
description = "Grep but for secrets"
license = "MIT"
Expand Down
23 changes: 13 additions & 10 deletions src/entgrep.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import std/streams
import strutils
import strformat
import math
import tables


proc entropy(s: string): float {.inline.} =
var t = initCountTable[char]()
for c in s: t.inc(c)
Expand All @@ -12,27 +14,25 @@ func hexlify(buf: string): string = # TODO: hexlify it easier
for c in buf:
result &= ord(c).toHex[14..15]

proc reportFinding(offset: Natural, blob: string, asJson: bool) =
proc reportFinding(offset: Natural, blob: string, asJson: bool, fn: string) =
if asJson:
echo "{\"offset\": \"0x" & offset.toHex & "\", \"entropy\": " & $blob.entropy & ", \"blob\": \"" & blob.hexlify & "\"}"
echo "{\"fn\": \"" & fn & "\", " & "\"offset\": \"0x" & offset.toHex & "\", \"entropy\": " & $blob.entropy & ", \"blob\": \"" & blob.hexlify & "\"}"
else:
echo "High entropy blob found at: 0x" & offset.toHex & ", Entropy: " & $blob.entropy
echo blob.hexlify
echo "====================================================="
echo fmt"{fn},0x{offset}: (e:{$blob.entropy}) {blob.hexlify}"

proc extractBlob(blob: var string, blobSize: Natural, s: Stream, threshold: float, asJson: bool) {.inline.} =
blob = s.peekStr(blobSize)
try:
let trailingChar = s.peekStr(blobSize+1)[blobSize] # TODO: optimize
if blob.entropy > threshold and trailingChar == '\0':
if blob.entropy >= threshold and trailingChar == '\0':
discard s.readStr(blobSize)
else:
blob = ""
except IndexDefect: # End of file reached.
blob = ""
return

proc processStream(strm: Stream, blobSize=48, threshold=5.2, asJson=false) =
proc processStream(strm: Stream, blobSize=48, threshold=5.2, asJson=false, fn: string) =
var
prev, cur: char
blob = newStringOfCap(blobSize)
Expand All @@ -45,14 +45,17 @@ proc processStream(strm: Stream, blobSize=48, threshold=5.2, asJson=false) =
blob.extractBlob(blobSize, strm, threshold, asJson)
if blob == "":
continue
reportFinding(offset, blob, asJson)
reportFinding(offset, blob, asJson, fn)

proc processFiles(blobSize: Natural=48, threshold=5.2, asJson=false, files: seq[string]) =
if 0 >= threshold or threshold >= 8:
echo "Threshold must fit between 0 and 8."
return
for fn in files.items:
echo "Processing " & fn & "..."
var strm = newFileStream(fn, fmRead)
try:
processStream(strm, blobSize, threshold, asJson)
processStream(strm, blobSize, threshold, asJson, fn)
finally:
strm.close()

Expand All @@ -61,7 +64,7 @@ when isMainModule:
dispatch(
processFiles,
cmdName="entgrep",
doc="a grep for secret stuff",
doc="A grep for secret stuff",
short={
"blobSize": 's',
"threshold": 't',
Expand Down
2 changes: 1 addition & 1 deletion src/nim.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# --define:release
--define:release
--gc:orc

0 comments on commit 1006839

Please sign in to comment.