Checked-In Secret Detection: Strings Are All You Need
检测代码里的硬编码密钥,只看字符串字面量就够了:如果你在维护密钥扫描工具,这条结论能直接省掉麻烦:不用再喂整段代码给模型,只保留密钥周围的字符串字面量,输入缩到原来的1/3,准确率反而更高。基于这个思路的Secretron在SecretBench上拿到98.74% F1,超过所有LLM基线,还在26个真实应用里挖出48个没人知道的密钥。
现有上下文感知模型把整段代码喂进去,模型因此对可混淆的变量名和语法结构产生依赖,跨语言场景也因训练数据分布不均而退化;StringGroup只提取字符串字面量,把上下文压到原长33.2%。Secretron用这个上下文在SecretBench上达到98.74% F1,混淆和跨语言场景优于LLM基线,并在26个应用中检出48个未知密钥。
字符串字面量是代码密钥检测的充分上下文:StringGroup把上下文压到原长33.2%并保留逾80%语义信息,Secretron据此在SecretBench上达到98.74% F1,超过LLM-based基线。
1. 旧做法把完整代码片段送进模型,模型对变量名和函数名产生虚假依赖:混淆重命名后性能骤降,主流语言之外泛化退化,冗长上下文还拖慢推理。 2. StringGroup只保留字符串字面量节点,上下文压到原长33.2%且保留逾80%语义信息;Secretron用Transformer分类器在SecretBench的标准、混淆、跨语言三场景设对照组与LLM基线比较。 3. 标准场景F1=98.74%超过全部LLM基线,混淆和跨语言场景更稳,并在26个真实应用检出48个未知密钥;工程上应把上下文提取限定为字符串字面量,但混淆与跨语言场景的具体F1未披露。
数据集是SecretBench,含开源项目的硬编码密钥正样本和非密钥负样本,规模未披露。三类场景:标准场景F1=98.74%,优于LLM-based基线;混淆对抗场景下变量名被重命名后,现有上下文感知模型性能下滑而Secretron保持鲁棒;跨语言场景下LLM基线泛化退化而Secretron更优。后两个场景的具体F1未提供,改善幅度无法量化。实际部署26个应用,检出48个未知密钥。
- 它要解决什么
- 只用字符串字面量做上下文,能不能一次解决密钥检测的三个老毛病:怕混淆、跨语言退化、上下文噪声大?
- 研究路径
- StringGroup对源代码做字符串字面量提取:在候选密钥周围定位所有字符串节点,过滤变量名、关键字、注释等可混淆元素,拼接成压缩上下文(原长的33.2%)。压缩后的序列送入预训练Transformer编码器,输出二分类概率判断是否为真实硬编码密钥。字符串字面量本身与语言无关,所以同一条管线可以直接覆盖多种语言。
- 这对工程意味着什么
- 第一步:把现有检测管线的上下文模块换成只提取字符串字面量。要避开的捷径:不要直接拿完整代码片段去微调LLM,模型会对变量名形成虚假相关,混淆之后性能骤降。
- 证据定位
- 标准场景下Secretron的F1为98.74%,超过所有LLM-based基线。混淆和跨语言场景下它比现有上下文感知方法更稳,但这两个场景的具体F1数值没有披露。实际部署在26个应用中检出48个此前未知的密钥。(筛选维度:可复核评测)
- 适用边界
- 摘录未给出混淆和跨语言场景的具体F1数值,鲁棒性改善幅度无法量化;SecretBench的规模和类别分布未披露;实际部署只覆盖26个应用;对周围没有字符串字面量的密钥场景,该策略的覆盖情况未讨论。
方法与英文摘要
StringGroup算法扫描候选密钥周围的代码,只保留字符串字面量节点,丢掉变量名、关键字和语法结构这些容易被混淆的东西,上下文被压缩到原长的33.2%。压缩后的序列送进一个Transformer二分类器(Secretron)。评测用SecretBench数据集,分标准、混淆对抗、跨语言三种场景,和正则及LLM-based基线比F1。最后还部署到26个真实应用里验证。
Hardcoded secrets in source code pose critical security vulnerabilities which can be easily exploited by malicious adversaries. Existing regex-based detection approaches suffer from fundamental limitations, as secrets often lack identifiable patterns, resulting in poor precision and recall. Recent studies have explored context-aware detection methods, as surrounding code can reveal the purpose of candidate strings. However, these methods confront three key challenges: (1) obfuscation robustness where models over-rely on easily obfuscated identifiers, (2) cross-language generalization difficulties due to uneven training data distribution, and (3) lengthy and noisy context that introduces excessive irrelevant tokens and slows inference. We observe that strings serve as a critical information source for code semantics, offering superior contextual density, obfuscation robustness, and language independence. Based on this insight, we propose StringGroup, a novel context extraction algorithm that mines strings surrounding potential secrets. By introducing a relatively simple modification to existing patterns that narrows the analysis specifically to string literals, the method achieves significant gains. With only 33.2% of the original context, it preserves over 80% of semantic information and significantly improves the signal-to-noise ratio for secret detection. We further design a context-aware secret detection tool, Secretron, based on StringGroup methods and Transformer model. Evaluation on the SecretBench dataset demonstrates high accuracy with 98.74% F1-score and strong robustness under obfuscation and cross-language scenarios, outperforming state-of-the-art LLM-based baselines. We deploy our tool in real-world environments and successfully detect 48 previously unknown secret keys from 26 applications, demonstrating the practical effectiveness of our approach.