Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Codex vuln detector - devtooligan updates #1499

Merged

Conversation

devtooligan
Copy link
Contributor

Highlights of this pr:

  • Parameterize the inputs to the OpenAI codex call: model, temperature, and max_tokens
  • Add --codex-contracts argument which takes a comma-delimited list of contracts and, if specified, it will limit the Codex detector to only those contracts. I found this a great way to save money when working with a large contract with a deeply nested inheritance tree. Someday, may want to expand this feature to all detectors.
  • It also changes the prompt to something I was having more luck with. It no longer asks a "Yes" or "No" question, so the prompt now also requires the use of a keyword if vulns are found.

I did spend some time implementing a "retries" system. But for some reason, the retried "queries" were all identical. I guess I was hoping it would exhibit similar behavior to the chatbot when "retry" is pressed, which results in a different response. So I've removed that feature but will continue to look into it.

@devtooligan
Copy link
Contributor Author

Additional pr based on this branch submitted. #1498

prompt = "Is there a vulnerability in this solidity contracts?\n"
if self.slither.codex_contracts != "all" and contract.name not in self.slither.codex_contracts.split(","):
continue
prompt = "Analyze this Solidity contract and find the vulnerabilities. If you find any vulnerabilities, begin the response with {}".format(VULN_FOUND)
Copy link
Contributor Author

@devtooligan devtooligan Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably parameterize the prompt.

Also, maybe we could just always display the Codex response, even if it doesn't find anything. This would simplify it so we don't need adjust the prompt or look for a special word in the response that indicates something was found.

res = self.generate_result(info)
results.append(res)

logging.info("Querying OpenAI")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't 100% sure what to do with logging/printing . I noticed in this file we used the logger but it didn't display anything when Slither was run. And I noticed in other parts of the tool we use print.

print("Querying OpenAI")
answer = ""
res = {}
try:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this try/catch because I was hitting the max token limit with a large contract in the prompt. Another reason to consider running this per-function.

@@ -52,29 +53,63 @@ def _detect(self) -> List[Output]:
openai.api_key = api_key

for contract in self.compilation_unit.contracts:
prompt = "Is there a vulnerability in this solidity contracts?\n"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above in line 40, the import openai inside of this function was problematic when I tried to extract the OpenAI query logic into a separate function. I think it scopes the import to the function so I wasn't able to access it.

Maybe we can move the import (wrapped in a try/except) to the top level? We could still do something here that checks whether it's been installed.

@devtooligan devtooligan changed the title feat: devtooligan updates feat: Codex detector - devtooligan updates Dec 7, 2022
@devtooligan devtooligan changed the title feat: Codex detector - devtooligan updates feat: Codex vuln detector - devtooligan updates Dec 7, 2022
@montyly
Copy link
Member

montyly commented Dec 7, 2022

This is awesome, thanks @devtooligan

@montyly montyly merged commit 00d33c6 into crytic:dev-codex Dec 7, 2022
@montyly
Copy link
Member

montyly commented Dec 7, 2022

I merged the improvements in #1498, some notes:

  • The logging was not working because I used logging instead of logger (...)
  • I refactored part of the codex logic into a utils.codex. We can then use the similar logic for Use Codex to generate solidity documentation #1494. Right now its only for logging/import openai. We might add general parsing of openai queries, depending on how we are going to use openai in slither
  • I added a --codex-log flag. This will create files in crytic_export/codex/..., with all the queries/answers. This should help debugging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants