-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
111 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import contextlib | ||
import statistics | ||
|
||
from credsweeper.common.constants import Chars | ||
from credsweeper.config import Config | ||
from credsweeper.credentials import LineData | ||
from credsweeper.file_handler.analysis_target import AnalysisTarget | ||
from credsweeper.filters import Filter | ||
from credsweeper.utils import Util | ||
|
||
|
||
class ValueBase64PartCheck(Filter): | ||
""" | ||
Check that candidate is NOT a part of base64 long line | ||
""" | ||
|
||
def __init__(self, config: Config = None) -> None: | ||
pass | ||
|
||
def run(self, line_data: LineData, target: AnalysisTarget) -> bool: | ||
"""Run filter checks on received weird base64 token which must be a random string | ||
Args: | ||
line_data: credential candidate data | ||
target: multiline target from which line data was obtained | ||
Return: | ||
True, when need to filter candidate and False if left | ||
""" | ||
|
||
with contextlib.suppress(Exception): | ||
if line_data.value_start and '/' == line_data.line[line_data.value_start - 1]: | ||
if '-' in line_data.value or '_' in line_data.value: | ||
# the value contains url-safe chars, so '/' is a delimiter | ||
return False | ||
value_entropy = Util.get_shannon_entropy(line_data.value, Chars.BASE64STD_CHARS.value) | ||
left_start = line_data.value_start - len(line_data.value) | ||
if 0 > left_start: | ||
left_start = 0 | ||
left_entropy = Util.get_shannon_entropy(line_data.line[left_start:line_data.value_start], | ||
Chars.BASE64STD_CHARS.value) | ||
right_end = line_data.value_end + len(line_data.value) | ||
if len(line_data.line) < right_end: | ||
right_end = len(line_data.line) | ||
right_entropy = Util.get_shannon_entropy(line_data.line[line_data.value_end:right_end], | ||
Chars.BASE64STD_CHARS.value) | ||
data = [value_entropy, left_entropy, right_entropy] | ||
avg = statistics.mean(data) | ||
stdev = statistics.stdev(data, avg) | ||
avg_min = avg - stdev | ||
if avg_min < left_entropy and avg_min < right_entropy: | ||
# high entropy of bound parts looks like a part of base64 long line | ||
return True | ||
|
||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import re | ||
import unittest | ||
|
||
from credsweeper.credentials import LineData | ||
from credsweeper.filters import ValueBase64PartCheck | ||
from tests.filters.conftest import DUMMY_ANALYSIS_TARGET | ||
|
||
|
||
class TestValueBase64PartCheck(unittest.TestCase): | ||
EAA_PATTERN = re.compile(r"(?P<value>\bEAA[0-9A-Za-z]{32})") | ||
|
||
def test_value_check_n(self) -> None: | ||
line_data = LineData(config=None, | ||
path="dummy", | ||
file_type="", | ||
line="qcE81rS+FJHuvg39lz4T/EAACEb00Kse0BAlGy7KeQ5YnaCEd09Eo" | ||
"se0cBAlGy7KeQ5Yna9CoDsup39tiYdoQ4jH9Coup39tiYdWoQ4jHFZD", | ||
info="", | ||
line_num=1, | ||
line_pos=0, | ||
pattern=TestValueBase64PartCheck.EAA_PATTERN) | ||
self.assertTrue(ValueBase64PartCheck().run(line_data, DUMMY_ANALYSIS_TARGET)) | ||
|
||
def test_value_check_p(self) -> None: | ||
line_data = LineData(config=None, | ||
path="dummy", | ||
file_type="", | ||
line="http://meta.test/api/EAACRvAWiwzR8rcXFsLiUH13ybj0tdEa?x=login", | ||
info="", | ||
line_num=1, | ||
line_pos=0, | ||
pattern=TestValueBase64PartCheck.EAA_PATTERN) | ||
self.assertFalse(ValueBase64PartCheck().run(line_data, DUMMY_ANALYSIS_TARGET)) |
Large diffs are not rendered by default.
Oops, something went wrong.