Skip to content

Commit 944fb17

Browse files
authored
Pattern improvements. Version Up to 1.14.7 (#809)
* Improve sql password rule * `get` improved with 'or'
1 parent e30c3a7 commit 944fb17

File tree

15 files changed

+192
-43
lines changed

15 files changed

+192
-43
lines changed

.ci/benchmark.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ FileType FileNumber ValidLines Positives Negatives
226226
.zsh 6 872 11
227227
.zsh-theme 1 97 1
228228
TOTAL: 11375 16999171 17141 53736
229-
credsweeper result_cnt : 16978, lost_cnt : 0, true_cnt : 16827, false_cnt : 151
229+
credsweeper result_cnt : 16979, lost_cnt : 0, true_cnt : 16828, false_cnt : 151
230230
Rules Positives Negatives Reported TP FP TN FN FPR FNR ACC PRC RCL F1
231231
------------------------------ ----------- ----------- ---------- ----- ---- ----- ---- -------- -------- -------- -------- -------- --------
232232
API 243 4009 242 236 6 4003 7 0.001497 0.028807 0.996943 0.975207 0.971193 0.973196
@@ -270,7 +270,7 @@ Nonce 131 109 128 127
270270
OTP / 2FA Secret 64 3 56 54 2 1 10 0.666667 0.156250 0.820896 0.964286 0.843750 0.900000
271271
Other 0 20 0 0 20 0 0.000000 1.000000
272272
PEM Private Key 1157 72 1157 1157 0 72 0 0.000000 0.000000 1.000000 1.000000 1.000000 1.000000
273-
Password 2595 11366 2527 2517 10 11356 78 0.000880 0.030058 0.993697 0.996043 0.969942 0.982819
273+
Password 2595 11366 2528 2518 10 11356 77 0.000880 0.029672 0.993768 0.996044 0.970328 0.983018
274274
Perplexity API Key 2 0 2 2 0 0 0 0.000000 1.000000 1.000000 1.000000 1.000000
275275
Postman Credentials 2 0 2 2 0 0 0 0.000000 1.000000 1.000000 1.000000 1.000000
276276
SQL Password 44 14 41 40 1 13 4 0.071429 0.090909 0.913793 0.975610 0.909091 0.941176
@@ -284,4 +284,4 @@ Token 1144 5285 1072 1066
284284
Twilio Credentials 30 39 30 30 0 39 0 0.000000 0.000000 1.000000 1.000000 1.000000 1.000000
285285
URL Credentials 225 401 221 220 1 400 5 0.002494 0.022222 0.990415 0.995475 0.977778 0.986547
286286
UUID 2517 3716 2554 2494 60 3656 23 0.016146 0.009138 0.986684 0.976507 0.990862 0.983632
287-
17141 53736 16979 16827 151 53585 314 0.002810 0.018319 0.993439 0.991106 0.981681 0.986371
287+
17141 53736 16980 16828 151 53585 313 0.002810 0.018260 0.993453 0.991107 0.981740 0.986401

.github/workflows/check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ jobs:
9292
run: |
9393
banner="$(python -m credsweeper --banner | head -1)"
9494
echo "banner = '${banner}'"
95-
if [ "CredSweeper 1.14.6 crc32:765e27c6" != "${banner}" ]; then
95+
if [ "CredSweeper 1.14.7 crc32:fdae340d" != "${banner}" ]; then
9696
echo "Update the check for '${banner}'"
9797
exit 1
9898
fi

credsweeper/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424
"__version__"
2525
]
2626

27-
__version__ = "1.14.6"
27+
__version__ = "1.14.7"

credsweeper/common/keyword_pattern.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class KeywordPattern:
2626
r"\s*" \
2727
r"(\[(?!\])|\((?!\))|\{(?!\}))" \
2828
r"(\s|\\{1,8}[tnr])*" \
29-
r"(?(get)('[^']{1,31}'|\"[^\"]{1,31}\")\s*,\s*|)" \
29+
r"(?(get)('[^']{1,31}'|\"[^\"]{1,31}\")\s*(,|\)\s*or)\s*|)" \
3030
r"([0-9a-z_]{1,32}\s*[:=]\s*)?" \
3131
r"){1,8})?"
3232
string_prefix = r"(((b|r|br|rb|u|f|rf|fr|l|@)(?=(\\*[\"'`])))?"

credsweeper/rules/config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
confidence: weak
44
type: pattern
55
values:
6-
- (?P<variable>(\w*(?i:비밀번호|비번|패스워드|키|암호화?|토큰|(?<!by)pass(?!e[dns]|ing|ion|age)|\bpwd?\b|token|secret|key|cred)\w*)\s*(설정은|[=:!]{1,3}))?\s*([._0-9A-Za-z\[\]]*get(env)?\s*\(\s*(?(variable)[^,]+|[\"'\\]*(\\*([\"']|&(quot|apos|#3[49]);)){0,4}(\w*(?i:(?<!by)pass(?!e[dns]|ing|ion|age|\s+[a-z]{3,80})|\bpwd?\b|token|secret|key|cred)\w*))(\\*([\"']|&(quot|apos|#3[49]);)){0,4})\s*,\s*(default\s*=\s*)?([brufl@]{1,2}(?=\\*[\"'&]))?(?P<lq>(\\*([\"']|&(quot|apos|#3[49]);)){1,4})(?P<value>(.(?!(?P=lq))){4,80}.?)
6+
- (?P<variable>(\w*(?i:비밀번호|비번|패스워드|키|암호화?|토큰|(?<!by)pass(?!e[dns]|ing|ion|age)|\bpwd?\b|token|secret|key|cred)\w*)\s*(설정은|[=:!]{1,3}))?\s*([._0-9A-Za-z\[\]]*get(env)?\s*\(\s*(?(variable)[^,]+|[\"'\\]*(\\*([\"']|&(quot|apos|#3[49]);)){0,4}(\w*(?i:(?<!by)pass(?!e[dns]|ing|ion|age|\s+[a-z]{3,80})|\bpwd?\b|token|secret|key|cred)\w*))(\\*([\"']|&(quot|apos|#3[49]);)){0,4})\s*(,(\s*default\s*=)?|\)\s*or)\s*([brufl@]{1,2}(?=\\*[\"'&]))?(?P<lq>(\\*([\"']|&(quot|apos|#3[49]);)){1,4})(?P<value>(.(?!(?P=lq))){4,80}.?)
77
filter_type:
88
- ValueAllowlistCheck
99
- LineGitBinaryCheck
@@ -993,9 +993,9 @@
993993
confidence: moderate
994994
type: pattern
995995
values:
996-
- (?:^|/|[^\\0-9A-Za-z+_-]|\\[0abfnrtv]|(?:%|\\x)[0-9A-Fa-f]{2}|\\[0-7]{3}|\\[Uu][0-9A-Fa-f]{4}|\x1B\[[0-9;]{0,80}m)(?P<value>sl\.(u\.)?[0-9A-Za-z_-]{135})(?![0-9A-Za-z_-])
996+
- (?:^|/|[^\\0-9A-Za-z+_-]|\\[0abfnrtv]|(?:%|\\x)[0-9A-Fa-f]{2}|\\[0-7]{3}|\\[Uu][0-9A-Fa-f]{4}|\x1B\[[0-9;]{0,80}m)(?P<value>sl\.(u\.)?[0-9A-Za-z_-]{77,177})(?![0-9A-Za-z_-])
997997
filter_type: TokenPattern
998-
min_line_len: 138
998+
min_line_len: 80
999999
required_substrings:
10001000
- sl.
10011001
target:
@@ -1536,7 +1536,7 @@
15361536
confidence: weak
15371537
type: pattern
15381538
values:
1539-
- (\\[nrt]|\b)(?i:(?P<variable>(CREATE|ALTER|SET\s{1,8}PASSWORD|INSERT(\s{1,8}IGNORE)?|UPDATE\s{1,8}[^\s;]{1,80})\s{1,8}(LOGIN|USER|ROLE|FOR|INTO|SET)\s{1,8}([^\s;]{1,80}\s{1,8}|VALUES\s*\(){1,8}(IDENTIFIED((\s{1,8}WITH\s{1,8}\S{1,80})?\s{1,8}(BY|AS))|(=|WITH)?\s*PASSWORD\b(\s*=)?)))\s*(?P<wrap>[(]\s*)?(?P<value_leftquote>((?P<esq>\\{1,8})?([\"'`]|&(quot|apos|#3[49]);)){1,4})?(?P<value>(?(value_leftquote)((?!(?P=value_leftquote))(?(esq)((?!(?P=esq)([\"'`]|&(quot|apos|#3[49]);)).)|((?!(?P=value_leftquote)).)))|(?!&(quot|apos|#3[49]);)(\\+([ tnr]|[^\s\"'`])|[^\s\"'`,;\\])){3,80})(?(value_leftquote)(?P<value_rightquote>(?<!\\)(?P=value_leftquote))|(?(wrap)[)]|[\s\"'`,;]))
1539+
- (\\[nrt]|\b)(?i:(?P<variable>(CREATE|ALTER|SET\s{1,8}PASSWORD|INSERT(\s{1,8}IGNORE)?|UPDATE\s{1,8}[^\s;]{1,80})\s{1,8}(LOGIN|USER|ROLE|FOR|INTO|SET)\s{1,8}((?!IDENTIFIED|PASSWORD)[^\s;]{1,80}\s{1,8}|VALUES\s{0,8}\(){1,8}(IDENTIFIED((\s{1,8}WITH\s{1,8}\S{1,80})?\s{1,8}(BY|AS))|(=|WITH)?\s{0,8}PASSWORD\b(\s{0,8}=)?)))\s{0,8}(?P<wrap>[(]\s{0,8})?(?P<value_leftquote>((?P<esq>\\{1,8})?([\"'`]|&(quot|apos|#3[49]);)){1,4})?(?P<value>(?(value_leftquote)((?!(?P=value_leftquote))(?(esq)((?!(?P=esq)([\"'`]|&(quot|apos|#3[49]);)).)|((?!(?P=value_leftquote)).)))|(?!&(quot|apos|#3[49]);)(\\{1,8}([ tnr]|[^\s\"'`])|[^\s\"'`,;\\])){3,80})(?(value_leftquote)(?P<value_rightquote>(?<!\\)(?P=value_leftquote))|(?(wrap)[)]|[\s\"'`,;]))
15401540
filter_type:
15411541
- ValueAllowlistCheck
15421542
- ValuePatternCheck

tests/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
ZERO_ML_THRESHOLD = 0.0
1111

1212
# with option --doc & NEGLIGIBLE_ML_THRESHOLD
13-
SAMPLES_IN_DOC = 924
13+
SAMPLES_IN_DOC = 925
1414

1515
# credentials count after scan without filters and ML validations
16-
SAMPLES_REGEX_COUNT = 655
16+
SAMPLES_REGEX_COUNT = 656
1717

1818
# credentials count after scan with filters and without ML validation
19-
SAMPLES_FILTERED_COUNT = 544
19+
SAMPLES_FILTERED_COUNT = 545
2020

2121
# credentials count after default post-processing
22-
SAMPLES_POST_CRED_COUNT = 497
22+
SAMPLES_POST_CRED_COUNT = 499
2323

2424
# archived credentials that are not found without --depth
2525
SAMPLES_IN_DEEP_1 = SAMPLES_POST_CRED_COUNT + 138

tests/common/test_keyword_pattern.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def test_separator_p(self, config: Config, file_path: pytest.fixture, line: str)
4444
# ['''password=f"\\"secret=2\\""''', '''\\"secret=2\\"'''], # todo
4545
# ['''password=r"\\\\"secret=3\\\\""''', '''\\"secret=3\\"'''], # todo
4646
# ['''"password = 'sec;$2`\\'[\\/*;ret';";''', '''sec;$2`\\'[\\/*;ret'''], # todo
47+
['PASSWORD = os.environ.get("PASSWORD") or "at5G6zi!m"', "at5G6zi!m"],
4748
["deFINE \\n\\t('DB_PASSWORD',\\n\\t'devSeCrEt');", "devSeCrEt"],
4849
['''...log=1;User ID=X3;password=Quantum42!\\""''', '''Quantum42!'''],
4950
[

tests/data/depth_3_pedantic.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,27 @@
18061806
}
18071807
]
18081808
},
1809+
{
1810+
"rule": "Password",
1811+
"severity": "high",
1812+
"confidence": "moderate",
1813+
"ml_probability": 0.999,
1814+
"line_data_list": [
1815+
{
1816+
"line": "PASSWORD = os.environ.get(\"PASSWORD\") or \"at5G6zi!m\"",
1817+
"line_num": 5,
1818+
"path": "./tests/samples/doc_get_p",
1819+
"info": "FILE:./tests/samples/doc_get_p|RAW",
1820+
"variable": "PASSWORD",
1821+
"variable_start": 0,
1822+
"variable_end": 8,
1823+
"value": "at5G6zi!m",
1824+
"value_start": 42,
1825+
"value_end": 51,
1826+
"entropy": 3.16993
1827+
}
1828+
]
1829+
},
18091830
{
18101831
"rule": "Password",
18111832
"severity": "high",
@@ -14906,6 +14927,27 @@
1490614927
}
1490714928
]
1490814929
},
14930+
{
14931+
"rule": "SQL Password",
14932+
"severity": "medium",
14933+
"confidence": "weak",
14934+
"ml_probability": 0.996,
14935+
"line_data_list": [
14936+
{
14937+
"line": "ALTER USER 'test'@'1.1.1.1' IDENTIFIED WITH 'mysql_native_password' AS '*92FD05ADCA2EC9D1E10C096DEB1618BC2470E409' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK; -- old MySQL hash scheme with sha1",
14938+
"line_num": 31,
14939+
"path": "./tests/samples/sql_password",
14940+
"info": "FILE:./tests/samples/sql_password|RAW",
14941+
"variable": "ALTER USER 'test'@'1.1.1.1' IDENTIFIED WITH 'mysql_native_password' AS",
14942+
"variable_start": 0,
14943+
"variable_end": 70,
14944+
"value": "*92FD05ADCA2EC9D1E10C096DEB1618BC2470E409",
14945+
"value_start": 72,
14946+
"value_end": 113,
14947+
"entropy": 3.78769
14948+
}
14949+
]
14950+
},
1490914951
{
1491014952
"rule": "SQL Password",
1491114953
"severity": "medium",

tests/data/doc.json

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,6 +1642,27 @@
16421642
}
16431643
]
16441644
},
1645+
{
1646+
"rule": "DOC_GET",
1647+
"severity": "medium",
1648+
"confidence": "weak",
1649+
"ml_probability": 0.969,
1650+
"line_data_list": [
1651+
{
1652+
"line": "PASSWORD = os.environ.get(\"PASSWORD\") or \"at5G6zi!m\"",
1653+
"line_num": 5,
1654+
"path": "./tests/samples/doc_get_p",
1655+
"info": "FILE:./tests/samples/doc_get_p|RAW",
1656+
"variable": "PASSWORD =",
1657+
"variable_start": 0,
1658+
"variable_end": 10,
1659+
"value": "at5G6zi!m",
1660+
"value_start": 42,
1661+
"value_end": 51,
1662+
"entropy": 3.16993
1663+
}
1664+
]
1665+
},
16451666
{
16461667
"rule": "DOC_CREDENTIALS",
16471668
"severity": "medium",
@@ -19962,20 +19983,20 @@
1996219983
"rule": "SQL Password",
1996319984
"severity": "medium",
1996419985
"confidence": "weak",
19965-
"ml_probability": 0.0,
19986+
"ml_probability": 0.996,
1996619987
"line_data_list": [
1996719988
{
19968-
"line": "\"ALTER USER 'test'@'1.1.1.1' IDENTIFIED WITH 'mysql_native_password' AS '*SqLpa5sW0rD' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCO",
19989+
"line": "ALTER USER 'test'@'1.1.1.1' IDENTIFIED WITH 'mysql_native_password' AS '*92FD05ADCA2EC9D1E10C096DEB1618BC2470E409' REQUIRE NONE",
1996919990
"line_num": 31,
1997019991
"path": "./tests/samples/sql_password",
1997119992
"info": "FILE:./tests/samples/sql_password|RAW",
19972-
"variable": "ALTER USER 'test'@'1.1.1.1' IDENTIFIED WITH 'mysql_native_password' AS '*SqLpa5sW0rD' REQUIRE NONE PASSWORD",
19973-
"variable_start": 1,
19974-
"variable_end": 108,
19975-
"value": "EXPIRE",
19976-
"value_start": 109,
19977-
"value_end": 115,
19978-
"entropy": 2.25163
19993+
"variable": "ALTER USER 'test'@'1.1.1.1' IDENTIFIED WITH 'mysql_native_password' AS",
19994+
"variable_start": 0,
19995+
"variable_end": 70,
19996+
"value": "*92FD05ADCA2EC9D1E10C096DEB1618BC2470E409",
19997+
"value_start": 72,
19998+
"value_end": 113,
19999+
"entropy": 3.78769
1997920000
}
1998020001
]
1998120002
},

tests/data/no_filters_no_ml.json

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,27 @@
20382038
}
20392039
]
20402040
},
2041+
{
2042+
"rule": "Password",
2043+
"severity": "high",
2044+
"confidence": "moderate",
2045+
"ml_probability": null,
2046+
"line_data_list": [
2047+
{
2048+
"line": "PASSWORD = os.environ.get(\"PASSWORD\") or \"at5G6zi!m\"",
2049+
"line_num": 5,
2050+
"path": "./tests/samples/doc_get_p",
2051+
"info": "",
2052+
"variable": "PASSWORD",
2053+
"variable_start": 0,
2054+
"variable_end": 8,
2055+
"value": "at5G6zi!m",
2056+
"value_start": 42,
2057+
"value_end": 51,
2058+
"entropy": 3.16993
2059+
}
2060+
]
2061+
},
20412062
{
20422063
"rule": "Password",
20432064
"severity": "high",
@@ -13034,17 +13055,17 @@
1303413055
"ml_probability": null,
1303513056
"line_data_list": [
1303613057
{
13037-
"line": "\"ALTER USER 'test'@'1.1.1.1' IDENTIFIED WITH 'mysql_native_password' AS '*SqLpa5sW0rD' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK;\",",
13058+
"line": "ALTER USER 'test'@'1.1.1.1' IDENTIFIED WITH 'mysql_native_password' AS '*92FD05ADCA2EC9D1E10C096DEB1618BC2470E409' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK; -- old MySQL hash scheme with sha1",
1303813059
"line_num": 31,
1303913060
"path": "./tests/samples/sql_password",
1304013061
"info": "",
13041-
"variable": "ALTER USER 'test'@'1.1.1.1' IDENTIFIED WITH 'mysql_native_password' AS '*SqLpa5sW0rD' REQUIRE NONE PASSWORD",
13042-
"variable_start": 1,
13043-
"variable_end": 108,
13044-
"value": "EXPIRE",
13045-
"value_start": 109,
13046-
"value_end": 115,
13047-
"entropy": 2.25163
13062+
"variable": "ALTER USER 'test'@'1.1.1.1' IDENTIFIED WITH 'mysql_native_password' AS",
13063+
"variable_start": 0,
13064+
"variable_end": 70,
13065+
"value": "*92FD05ADCA2EC9D1E10C096DEB1618BC2470E409",
13066+
"value_start": 72,
13067+
"value_end": 113,
13068+
"entropy": 3.78769
1304813069
}
1304913070
]
1305013071
},

0 commit comments

Comments
 (0)