-
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
29 changed files
with
2,306 additions
and
6,809 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,52 @@ | ||
import contextlib | ||
import json | ||
|
||
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.filters.value_entropy_base64_check import ValueEntropyBase64Check | ||
from credsweeper.utils import Util | ||
|
||
|
||
class ValueAzureTokenCheck(Filter): | ||
""" | ||
Azure tokens contains header, payload and signature | ||
https://learn.microsoft.com/en-us/azure/active-directory-b2c/access-tokens | ||
""" | ||
|
||
def __init__(self, config: Config = None) -> None: | ||
pass | ||
|
||
def run(self, line_data: LineData, target: AnalysisTarget) -> bool: | ||
"""Run filter checks on received token which might be structured. | ||
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): | ||
parts = line_data.value.split('.') | ||
if 3 != len(parts): | ||
return True | ||
hdr = Util.decode_base64(parts[0], padding_safe=True, urlsafe_detect=True) | ||
header = json.loads(hdr) | ||
if not ("alg" in header and "typ" in header and "kid" in header): | ||
# must be all parts in header | ||
return True | ||
pld = Util.decode_base64(parts[1], padding_safe=True, urlsafe_detect=True) | ||
payload = json.loads(pld) | ||
if not ("iss" in payload and "exp" in payload and "iat" in payload): | ||
# must be all parts in payload | ||
return True | ||
min_entropy = ValueEntropyBase64Check.get_min_data_entropy(len(parts[2])) | ||
entropy = Util.get_shannon_entropy(parts[2], Chars.BASE64URL_CHARS.value) | ||
# good signature has to be like random bytes | ||
return entropy < min_entropy | ||
|
||
return True |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.