-
Notifications
You must be signed in to change notification settings - Fork 979
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The detector requires: - The user to use the flag `--codex` (meaning that codex is not ran by default) - `openai` must be installed, and `OPENAI_API_KEY` set The detector works at the contract level, and send the whole contract body to codex
- Loading branch information
Showing
5 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
"deepdiff", | ||
"numpy", | ||
"solc-select>=v1.0.0b1", | ||
"openai" | ||
] | ||
}, | ||
license="AGPL-3.0", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import logging | ||
import os | ||
from typing import List | ||
|
||
from slither.detectors.abstract_detector import AbstractDetector, DetectorClassification | ||
from slither.utils.output import Output | ||
|
||
logger = logging.getLogger("Slither") | ||
|
||
|
||
class Codex(AbstractDetector): | ||
""" | ||
Use codex to detect vulnerability | ||
""" | ||
|
||
ARGUMENT = "codex" | ||
HELP = "Use Codex to find vulnerabilities." | ||
IMPACT = DetectorClassification.HIGH | ||
CONFIDENCE = DetectorClassification.LOW | ||
|
||
WIKI = "https://github.com/crytic/slither/wiki/Detector-Documentation#codex" | ||
|
||
WIKI_TITLE = "Codex" | ||
WIKI_DESCRIPTION = "Use [codex](https://openai.com/blog/openai-codex/) to find vulnerabilities" | ||
|
||
# region wiki_exploit_scenario | ||
WIKI_EXPLOIT_SCENARIO = """N/A""" | ||
# endregion wiki_exploit_scenario | ||
|
||
WIKI_RECOMMENDATION = "Review codex's message." | ||
|
||
def _detect(self) -> List[Output]: | ||
results: List[Output] = [] | ||
|
||
if not self.slither.codex_enabled: | ||
return [] | ||
|
||
try: | ||
# pylint: disable=import-outside-toplevel | ||
import openai | ||
except ImportError: | ||
logging.info("OpenAI was not installed") | ||
logging.info('run "pip install openai"') | ||
return [] | ||
|
||
api_key = os.getenv("OPENAI_API_KEY") | ||
if api_key is None: | ||
logging.info( | ||
"Please provide an Open API Key in OPENAI_API_KEY (https://beta.openai.com/account/api-keys)" | ||
) | ||
return [] | ||
openai.api_key = api_key | ||
|
||
for contract in self.compilation_unit.contracts: | ||
prompt = "Is there a vulnerability in this solidity contracts?\n" | ||
src_mapping = contract.source_mapping | ||
content = contract.compilation_unit.core.source_code[src_mapping.filename.absolute] | ||
start = src_mapping.start | ||
end = src_mapping.start + src_mapping.length | ||
prompt += content[start:end] | ||
answer = openai.Completion.create( # type: ignore | ||
model="text-davinci-003", prompt=prompt, temperature=0, max_tokens=200 | ||
) | ||
|
||
if "choices" in answer: | ||
if answer["choices"]: | ||
if "text" in answer["choices"][0]: | ||
if "Yes," in answer["choices"][0]["text"]: | ||
info = [ | ||
"Codex detected a potential bug in ", | ||
contract, | ||
"\n", | ||
answer["choices"][0]["text"], | ||
"\n", | ||
] | ||
|
||
res = self.generate_result(info) | ||
results.append(res) | ||
|
||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters