archive: add 5 repo prompt(s) [skip ci]

This commit is contained in:
github-actions[bot]
2026-02-24 13:41:36 +00:00
parent 610c2f40de
commit 9db607454d
5 changed files with 672247 additions and 0 deletions
+977
View File
@@ -0,0 +1,977 @@
Project Path: arc_0xricksanchez_Shellcoder_kjzr6h80
Source Tree:
```txt
arc_0xricksanchez_Shellcoder_kjzr6h80
├── LICENSE
├── README.md
├── __init__.py
├── img
│ └── interface.png
└── plugin.json
```
`LICENSE`:
```
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
Copyright 2024 0x434b <admin@0x434b.dev>
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```
`README.md`:
```md
# BinaryNinja Shellcoder Plugin
## Overview
Shellcoder is a lightweight plugin for Binary Ninja that enhances shellcode development and analysis.
It provides a versatile interface for assembling, disassembling, and formatting shellcode, supporting multiple architectures and output formats akin to the online ShellStorm Assembler/Disassembler.
## Features
- **Multi-architecture Support**: Compatible with all architectures supported by Binary Ninja.
- **Flexible Input Formats**:
- Assembly instructions
- Inline hex format (e.g., "\x90\x90")
- Space-separated hex format (e.g., 90 90)
- **Multiple Output Formats**:
- Inline hex
- Space-separated hex
- Python byte string
- C-style array
- Disassembled mnemonics (for disassembling)
- **Customizable Mnemonic Display**:
- Optional address display
- Optional bytecode display
- Adjustable base address for relative operations
- **Comment usage**:
- You can annotate your assembly/shellcode with comments (`#`, `//`, or `;`)
- **Bad Character Identification**: Quickly identify and highlight problematic bytes in your shellcode.
- **No External Dependencies**: Utilizes only Binary Ninja's built-in functionalities, ensuring a lightweight and easily deployable solution.
## Installation
1. Clone this repository or download the source code.
2. Copy the plugin file to your Binary Ninja plugins folder:
- Windows: `%APPDATA%\Binary Ninja\plugins\`
- Linux: `~/.binaryninja/plugins/`
- macOS: `~/Library/Application Support/Binary Ninja/plugins/`
3. Restart Binary Ninja or reload plugins.
## Usage
1. Open Binary Ninja and navigate to the "Plugins" menu.
2. Select "Shellcoder" to open the plugin interface.
3. Enter your assembly code or hex-formatted shellcode in the input area.
4. Choose your desired output format and options.
5. Click "Assemble" to process your input.
6. View the results in the output area.
_Note_: Alternatively you can use `CTRL+p` to open the command palette and search for the plugin.
## Showcase
![interface](img/interface.png)
## Contributing
Contributions to the plugin are more than welcome! Here's how you can contribute:
1. Fork the repository.
2. Create a new branch for your feature or bug fix.
3. Commit your changes with clear, descriptive commit messages.
4. Push your branch and submit a pull request.
Please ensure your code adheres to the existing style and includes appropriate tests and documentation whenever necessary.
## License
This project is licensed under the Apache 2.0 License - see the [LICENSE](LICENSE) file for details.
## Acknowledgments
- The Binary Ninja team for their excellent reverse engineering platform.
- The ShellStorm online assembler/disassembler for giving me the idea.
## Contact
For bug reports, feature requests, or general inquiries, please open an issue on the GitHub repository.
---
Happy shellcoding!
```
`__init__.py`:
```py
from binaryninja import Architecture, BinaryView
from binaryninjaui import UIAction, UIActionHandler, Menu, UIContext
from PySide6.QtWidgets import (
QApplication,
QWidget,
QVBoxLayout,
QHBoxLayout,
QTextEdit,
QPushButton,
QComboBox,
QLabel,
QLineEdit,
QCheckBox,
QMessageBox,
)
from PySide6.QtGui import QColor, QTextCharFormat, QTextCursor, QFont
from PySide6.QtCore import QTimer
import re
from typing import List, Dict
# Constants
COMMENT_CHARS = ["#", ";", "//"]
class AssemblerError(Exception):
pass
class DisassemblerError(Exception):
pass
class Assembler:
def __init__(self) -> None:
self.arch: Architecture = None
def set_architecture(self, arch_name: str) -> None:
try:
self.arch = Architecture[arch_name]
except KeyError:
raise AssemblerError(f"Unsupported architecture: {arch_name}")
def assemble(self, input_text: str) -> List[Dict]:
assembled_instructions = []
for line in input_text.split("\n"):
line = line.strip()
if line.startswith(tuple(COMMENT_CHARS)):
assembled_instructions.append({"type": "comment", "content": line})
elif line:
try:
result = self.arch.assemble(line)
assembled_instructions.append(
{
"type": "instruction",
"asm": line,
"bytes": result,
}
)
except Exception as e:
raise AssemblerError(
f"Could not assemble line: '{line}'. Error: {str(e)}"
)
if not assembled_instructions:
raise AssemblerError("No instructions were assembled")
return assembled_instructions
def disassemble(self, input_bytes: bytes) -> List[Dict]:
disassembled_instructions = []
bv = BinaryView.new(data=input_bytes)
bv.arch = self.arch
bv.platform = self.arch.standalone_platform
offset = 0
while offset < len(input_bytes):
disassembly = bv.get_disassembly(offset)
if disassembly is None:
break
instruction_length = len(bv.read(offset, self.arch.max_instr_length))
disassembled_instructions.append(
{
"type": "instruction",
"asm": disassembly,
"bytes": input_bytes[offset : offset + instruction_length],
}
)
offset += instruction_length
if not disassembled_instructions:
raise DisassemblerError("No instructions were disassembled")
return disassembled_instructions
def format_output(
self,
assembled_instructions: List[Dict],
output_format: str,
total_bytes: int,
mnemonic_options: Dict = {},
) -> str:
if total_bytes == 0:
return "No instructions to assemble"
if output_format == "Inline":
return (
'"'
+ "".join(
f"\\x{b:02x}"
for instr in assembled_instructions
if instr["type"] == "instruction"
for b in instr["bytes"]
)
+ '"'
)
elif output_format == "Hex":
return " ".join(
f"{b:02x}"
for instr in assembled_instructions
if instr["type"] == "instruction"
for b in instr["bytes"]
)
elif output_format == "Python":
lines = []
for instr in assembled_instructions:
if instr["type"] == "comment":
lines.append(
f" {instr['content'].replace('//', '#').replace(';', '#')}"
)
else:
lines.append(f' b"{instr["bytes"].hex()}", # {instr["asm"]}')
return (
"shellcode = [\n"
+ "\n".join(lines)
+ f"\n]\n\n# Total length: {total_bytes} bytes\n"
f"shellcode_length = {total_bytes}\n"
f"raw_shellcode = b''.join(shellcode)"
)
elif output_format == "C-Array":
lines = []
for instr in assembled_instructions:
if instr["type"] == "comment":
lines.append(
f" {instr['content'].replace('#', '//').replace(';', '//')}"
)
else:
hex_bytes = [f"0x{b:02x}" for b in instr["bytes"]]
lines.append(f" {', '.join(hex_bytes)}, // {instr['asm']}")
return (
"unsigned char shellcode[] = {{\n"
+ "\n".join(lines)
+ f"\n}};\n\n// Total length: {total_bytes} bytes\n"
f"const size_t shellcode_length = {total_bytes};"
)
elif output_format == "Mnemonics":
lines = []
address = mnemonic_options.get("base_address", 0) if mnemonic_options else 0
for instr in assembled_instructions:
if instr["type"] == "comment":
lines.append(instr["content"])
else:
line_parts = []
if mnemonic_options.get("show_addresses", True):
line_parts.append(f"{address:08x}:")
if mnemonic_options.get("show_bytecodes", True):
line_parts.append(f"{instr['bytes'].hex():<16}")
if mnemonic_options.get("show_instructions", True):
line_parts.append(instr["asm"])
lines.append(" ".join(line_parts))
address += len(instr["bytes"])
return "\n".join(lines)
else:
raise AssemblerError(f"Unsupported output format: {output_format}")
def search_pattern(
self, assembled_bytes: bytes, pattern: str, respect_boundaries: bool
) -> List[Dict]:
if respect_boundaries:
hex_string = " ".join(f"{b:02x}" for b in assembled_bytes)
byte_pattern = " ".join(
pattern[i : i + 2] for i in range(0, len(pattern), 2)
)
matches = list(re.finditer(byte_pattern, hex_string))
return [
{
"offset": match.start() // 3,
"matched": match.group().replace(" ", ""),
}
for match in matches
]
else:
hex_string = assembled_bytes.hex()
matches = list(re.finditer(pattern, hex_string))
return [
{"offset": match.start() // 2, "matched": match.group()}
for match in matches
]
def check_bad_patterns(
self,
assembled_bytes: bytes,
bad_patterns: List[bytes],
respect_instructions: bool,
) -> List[Dict]:
found_bad_patterns = []
instruction_width = 4 # Default to 4 bytes, adjust based on architecture
for pattern in bad_patterns:
for i in range(len(assembled_bytes) - len(pattern) + 1):
if respect_instructions:
instruction_start = (i // instruction_width) * instruction_width
if i + len(pattern) > instruction_start + instruction_width:
continue
if assembled_bytes[i : i + len(pattern)] == pattern:
found_bad_patterns.append({"offset": i, "pattern": pattern.hex()})
return found_bad_patterns
class AssemblerWidget(QWidget):
def __init__(self, parent=None):
super(AssemblerWidget, self).__init__(parent)
# To get a binaryview we can use the UIContext of the currently opened file/db
view = UIContext.activeContext().getCurrentView()
# Get the actual BinaryView from the UI view
self.bv = view.getData() if view else None
# Now we can access the architecture
self._current_arch = self.bv.arch if self.bv else None
self.assembler = Assembler()
self.initUI()
def initUI(self) -> None:
layout = QVBoxLayout()
# Architecture selection
arch_layout = QHBoxLayout()
arch_label = QLabel("Architecture:")
self.arch_combo = QComboBox()
for arch in list(Architecture):
self.arch_combo.addItem(arch.name)
if self._current_arch:
self.arch_combo.setCurrentText(self._current_arch.name)
arch_layout.addWidget(arch_label)
arch_layout.addWidget(self.arch_combo)
layout.addLayout(arch_layout)
# Output format selection
format_layout = QHBoxLayout()
format_label = QLabel("Output Format:")
self.format_combo = QComboBox()
self.format_combo.addItems(["Inline", "Hex", "Python", "C-Array", "Mnemonics"])
self.format_combo.currentIndexChanged.connect(self.update_output)
format_layout.addWidget(format_label)
format_layout.addWidget(self.format_combo)
layout.addLayout(format_layout)
# Mnemonic format options (initially hidden)
self.mnemonic_options = QWidget()
mnemonic_layout = QHBoxLayout()
self.show_addresses = QCheckBox("Addresses")
self.show_addresses.setToolTip(
"Enable this option to display the address of each instruction"
)
self.show_bytecodes = QCheckBox("Bytecodes")
self.show_bytecodes.setToolTip(
"Enable this option to display the raw bytes of each instruction"
)
self.show_bytecodes.setChecked(True)
self.show_instructions = QCheckBox("Instructions")
self.show_instructions.setToolTip(
"Enable this option to display the mnemonic of each instruction"
)
self.show_instructions.setChecked(True)
mnemonic_layout.addWidget(self.show_addresses)
mnemonic_layout.addWidget(self.show_bytecodes)
mnemonic_layout.addWidget(self.show_instructions)
self.mnemonic_options.setLayout(mnemonic_layout)
self.mnemonic_options.hide()
layout.addWidget(self.mnemonic_options)
# Base address input
self.base_address_widget = QWidget()
base_address_layout = QHBoxLayout()
base_address_label = QLabel("Base Address:")
self.base_address_input = QLineEdit()
self.base_address_input.setText("0")
base_address_layout.addWidget(base_address_label)
base_address_layout.addWidget(self.base_address_input)
self.base_address_widget.setLayout(base_address_layout)
self.base_address_widget.hide()
layout.addWidget(self.base_address_widget)
# Assembly input
self.asm_input = QTextEdit()
input_label = QLabel("Input:")
self.asm_input.setPlaceholderText(
"Enter assembly instructions (one per line), or inline/hex formatted shellcode"
)
layout.addWidget(input_label)
layout.addWidget(self.asm_input)
# Assemble button
self.assemble_button = QPushButton("Run")
self.assemble_button.clicked.connect(self.assemble)
layout.addWidget(self.assemble_button)
# Output display
self.output = QTextEdit()
output_label = QLabel("Output:")
self.output.setReadOnly(True)
self.output.setFont(QFont("Monospace"))
layout.addWidget(output_label)
layout.addWidget(self.output)
# Copy button
self.copy_button = QPushButton("Copy Output")
self.copy_button.clicked.connect(self.copy_output)
layout.addWidget(self.copy_button)
# Search pattern
search_layout = QHBoxLayout()
search_label = QLabel("Search pattern:")
self.search_input = QLineEdit()
self.search_input.setPlaceholderText(
"Enter a regex pattern (e.g., 00.. or 00(?!FF))"
)
self.search_button = QPushButton("Search")
self.search_button.clicked.connect(self.search_pattern)
self.byte_boundary_checkbox = QCheckBox("Respect byte boundaries")
self.byte_boundary_checkbox.setChecked(True)
search_layout.addWidget(search_label)
search_layout.addWidget(self.search_input)
search_layout.addWidget(self.search_button)
search_layout.addWidget(self.byte_boundary_checkbox)
layout.addLayout(search_layout)
# Bad characters input
bad_chars_layout = QHBoxLayout()
bad_chars_label = QLabel("Bad patterns:")
self.bad_chars_input = QLineEdit()
self.bad_chars_input.setPlaceholderText(
"Enter bad patterns (e.g., 00 0a 0d fffe)"
)
self.bad_chars_check = QPushButton("Check Bad Patterns")
self.bad_chars_check.clicked.connect(self.check_bad_patterns)
bad_chars_layout.addWidget(bad_chars_label)
bad_chars_layout.addWidget(self.bad_chars_input)
bad_chars_layout.addWidget(self.bad_chars_check)
layout.addLayout(bad_chars_layout)
# Instruction boundary checkbox for bad pattern search
self.instruction_size_checkbox = QCheckBox("Respect instruction boundaries")
layout.addWidget(self.instruction_size_checkbox)
# Length display
length_layout = QHBoxLayout()
length_label = QLabel("Length:")
self.length_value = QLabel("0 bytes")
length_layout.addWidget(length_label)
length_layout.addWidget(self.length_value)
length_layout.addStretch()
layout.addLayout(length_layout)
# Search results display
self.search_results = QTextEdit()
self.search_results.setReadOnly(True)
layout.addWidget(QLabel("Search Results:"))
layout.addWidget(self.search_results)
# Info display
self.info_display = QLabel()
self.info_display.setWordWrap(True)
layout.addWidget(self.info_display)
self.setLayout(layout)
# Connect signals
self.format_combo.currentIndexChanged.connect(self.toggle_mnemonic_options)
self.show_addresses.stateChanged.connect(self.update_output)
self.show_bytecodes.stateChanged.connect(self.update_output)
self.show_instructions.stateChanged.connect(self.update_output)
self.base_address_input.textChanged.connect(self.update_output)
def toggle_mnemonic_options(self, index):
if self.format_combo.itemText(index) == "Mnemonics":
self.mnemonic_options.show()
self.base_address_widget.show()
else:
self.mnemonic_options.hide()
self.base_address_widget.hide()
self.update_output()
def update_output(self):
self.assemble()
self.clear_highlighting()
def clear_highlighting(self):
cursor = self.output.textCursor()
cursor.beginEditBlock()
cursor.select(QTextCursor.Document)
cursor.setCharFormat(QTextCharFormat())
cursor.clearSelection()
cursor.endEditBlock()
self.output.setTextCursor(cursor)
def copy_output(self):
output_text = self.output.toPlainText()
QApplication.clipboard().setText(output_text)
# Visual feedback
original_text = self.copy_button.text()
self.copy_button.setText("Copied!")
self.copy_button.setEnabled(False)
# Reset button after 1.5 seconds
QTimer.singleShot(1500, lambda: self.reset_copy_button(original_text))
self.info_display.setText("Output copied to clipboard")
def reset_copy_button(self, original_text):
self.copy_button.setText(original_text)
self.copy_button.setEnabled(True)
def assemble(self) -> None:
current_info = self.info_display.text()
arch_name = self.arch_combo.currentText()
output_format = self.format_combo.currentText()
try:
self.assembler.set_architecture(arch_name)
input_text = self.asm_input.toPlainText()
# Determine if input is assembly or raw bytes
if all(
all(c in "0123456789ABCDEFabcdef \\x\"'" for c in line.strip())
for line in input_text.split("\n")
if line.strip() and not line.strip().startswith(tuple(COMMENT_CHARS))
):
# Raw bytes input
processed_input = bytearray()
for line in input_text.split("\n"):
line = line.strip()
if line.startswith(tuple(COMMENT_CHARS)):
continue
if line:
if line.startswith('"') and line.endswith('"'):
processed_input.extend(
bytes.fromhex(line.strip('"').replace("\\x", ""))
)
else:
processed_input.extend(bytes.fromhex(line.replace(" ", "")))
assembled_instructions = self.assembler.disassemble(processed_input)
else:
# Assembly input
assembled_instructions = self.assembler.assemble(input_text)
total_bytes = sum(
len(instr["bytes"])
for instr in assembled_instructions
if instr["type"] == "instruction"
)
mnemonic_options = {
"show_addresses": self.show_addresses.isChecked(),
"show_bytecodes": self.show_bytecodes.isChecked(),
"show_instructions": self.show_instructions.isChecked(),
"base_address": int(self.base_address_input.text(), 16),
}
formatted_output = self.assembler.format_output(
assembled_instructions, output_format, total_bytes, mnemonic_options
)
self.output.setPlainText(formatted_output)
self.clear_highlighting()
self.length_value.setText(f"{total_bytes} bytes")
except (AssemblerError, DisassemblerError) as e:
self.show_error(str(e))
finally:
self.info_display.setText(current_info)
def search_pattern(self):
pattern = self.search_input.text()
respect_boundaries = self.byte_boundary_checkbox.isChecked()
assembled_text = self.output.toPlainText()
try:
# Extract raw bytes
assembled_bytes = self.get_raw_bytes(assembled_text)
matches = self.assembler.search_pattern(
assembled_bytes, pattern, respect_boundaries
)
self.search_results.clear()
if matches:
for match in matches:
self.search_results.append(
f"Offset {match['offset']}: {match['matched']}"
)
self.info_display.setText(f"Found {len(matches)} match(es).")
else:
self.search_results.append("No matches found.")
self.info_display.setText("No matches found.")
except re.error as e:
self.show_error(f"Invalid regex pattern: {str(e)}")
def check_bad_patterns(self):
bad_patterns_input = self.bad_chars_input.text().strip()
assembled_text = self.output.toPlainText()
respect_instructions = self.instruction_size_checkbox.isChecked()
try:
bad_patterns = [
bytes.fromhex(pattern.replace(" ", ""))
for pattern in bad_patterns_input.split()
]
assembled_bytes = self.get_raw_bytes(assembled_text)
found_bad_patterns = self.assembler.check_bad_patterns(
assembled_bytes, bad_patterns, respect_instructions
)
self.search_results.clear()
if found_bad_patterns:
self.search_results.append("Bad patterns found:")
for result in found_bad_patterns:
self.search_results.append(
f"Offset {result['offset']}: {result['pattern']}"
)
self.info_display.setText(
f"Found {len(found_bad_patterns)} bad pattern(s)."
)
else:
self.search_results.append("No bad patterns found.")
self.info_display.setText("No bad patterns found.")
# Highlight bad patterns in the output
self.highlight_bad_patterns(assembled_text, found_bad_patterns)
except ValueError as e:
self.show_error(
f"Invalid input. Use hex format (e.g., 00 0a 0d d287). Error: {str(e)}"
)
def get_raw_bytes(self, assembled_text: str) -> bytes:
if "\\x" in assembled_text: # Inline format
return bytes.fromhex(assembled_text.replace('"', "").replace("\\x", ""))
elif "shellcode = [" in assembled_text: # Python format
hex_values = re.findall(r'b"([0-9a-fA-F]+)"', assembled_text)
return b"".join(bytes.fromhex(value) for value in hex_values)
elif "0x" in assembled_text: # C-Array format
hex_values = re.findall(r"0x([0-9a-fA-F]{2})", assembled_text)
return bytes.fromhex("".join(hex_values))
else: # Hex format
return bytes.fromhex(re.sub(r"\s", "", assembled_text))
def highlight_bad_patterns(
self, assembled_text: str, found_bad_patterns: List[Dict]
):
cursor = self.output.textCursor()
cursor.beginEditBlock()
highlight_format = QTextCharFormat()
highlight_format.setBackground(QColor(255, 200, 200)) # Light red background
highlight_format.setForeground(QColor(0, 0, 0)) # Black text
start = 0
length = 0
for result in found_bad_patterns:
offset, pattern = result["offset"], result["pattern"]
pattern_length = len(bytes.fromhex(pattern))
if "\\x" in assembled_text: # Inline format
start = assembled_text.index('"') + 1 + offset * 4
length = pattern_length * 4
elif "shellcode = [" in assembled_text: # Python format
# Find the correct chunk and highlight within it
cumulative_length = 0
for match in re.finditer(r'b"([0-9a-fA-F]+)"', assembled_text):
chunk_length = len(match.group(1)) // 2
if cumulative_length <= offset < cumulative_length + chunk_length:
relative_offset = offset - cumulative_length
start = match.start() + 2 + relative_offset * 2
length = min(pattern_length, chunk_length - relative_offset) * 2
break
cumulative_length += chunk_length
elif "0x" in assembled_text: # C-Array format
hex_positions = [
m.start() for m in re.finditer(r"0x[0-9a-fA-F]{2}", assembled_text)
]
start = hex_positions[offset]
length = pattern_length * 4
else: # Hex format
start = offset * 3
length = pattern_length * 3 - 1
if not start:
self.show_error("Could not find the start position for highlighting")
if not length:
self.show_error("Could not find the length for highlighting")
cursor.setPosition(start)
cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor, length)
cursor.mergeCharFormat(highlight_format)
cursor.endEditBlock()
self.output.setTextCursor(cursor)
def show_error(self, message: str):
QMessageBox.critical(self, "Error", message)
self.search_results.setPlainText(f"Error: {message}")
assembler_widget = None
def run_plugin(bv) -> None:
global assembler_widget
assembler_widget = AssemblerWidget()
assembler_widget.show()
UIAction.registerAction("Shellcoder\\Run")
UIActionHandler.globalActions().bindAction("Shellcoder\\Run", UIAction(run_plugin))
Menu.mainMenu("Plugins").addAction("Shellcoder\\Run", "Shellcoder")
```
`plugin.json`:
```json
{
"api": [
"python3"
],
"author": "434b",
"description": "Interactive shellcode disassembler/assembler",
"license": {
"name": "Apache 2.0",
"text": "Copyright 2024 0x434b <admin@0x434b.dev> Licensed under the Apache License, Version 2.0 (the \"License\"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License."
},
"longdescription": "",
"minimumbinaryninjaversion": 4526,
"name": "Shellcoder",
"platforms": [
"Darwin",
"Linux",
"Windows"
],
"pluginmetadataversion": 2,
"type": [
"helper"
],
"version": "1.0.2"
}
```
+743
View File
@@ -0,0 +1,743 @@
Project Path: arc_WhatTheFuzz_binaryninja-openai_zzy9p0rv
Source Tree:
```txt
arc_WhatTheFuzz_binaryninja-openai_zzy9p0rv
├── LICENSE
├── README.md
├── __init__.py
├── plugin.json
├── requirements.txt
├── resources
│ ├── output.png
│ ├── rename-after.png
│ ├── rename-before.png
│ └── settings.png
└── src
├── agent.py
├── c.py
├── entry.py
├── exceptions.py
├── query.py
└── settings.py
```
`LICENSE`:
```
MIT License
Copyright (c) 2022 Sean Deaton (@WhatTheFuzz)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
`README.md`:
```md
[![CodeQL](https://github.com/WhatTheFuzz/binaryninja-openai/actions/workflows/codeql.yml/badge.svg)](https://github.com/WhatTheFuzz/binaryninja-openai/actions/workflows/codeql.yml)
# BinaryNinja-OpenAI
Integrates OpenAI's GPT3 with Binary Ninja via a plugin and currently supports
two actions:
- Queries OpenAI to determine what a given function does (in Pseudo-C and HLIL).
- The results are logged to Binary Ninja's log to assist with RE.
- Allows users to rename variables in HLIL using OpenAI.
- Variable are renamed immediately and the decompiler is reloaded.
## Installation
If you're installing this as a standalone plugin, you can place (or sym-link)
this in Binary Ninja's plugin path. Default paths are detailed on
[Vector 35's documentation][default-plugin-dir].
This plugin has been tested on macOS and Linux. It probably works on Windows;
please submit a pull request if you've tested it.
### Dependencies
- Python 3.10+
- `openai` installed with `pip3 install --user openai`
## API Key
This requires an [API token from OpenAI][token]. The plugin checks for the API
key in three ways (in this order).
First, it tries to read the key from Binary Ninja's preferences. You can
access the entry in Binary Ninja via `Edit > Preferences > Settings > OpenAI`.
Or, use the hotkey ⌘+, and search for `OpenAI`. You should see customizable
settings like so.
![Settings](https://github.com/WhatTheFuzz/binaryninja-openai/blob/main/resources/settings.png?raw=true)
Second, it checks the environment variable `OPENAI_API_KEY`, which you can set
inside of Binary Ninja's Python console like so:
```python
import os
os.environ["OPENAI_API_KEY"] = "INSERT KEY HERE"
```
Or you can write it to a file. The file is set in [entry.py][entry] and is a
parameter to the Agent class. By default it checks for the file
`~/.openai/api_key.txt`. You can add your API token like so:
```shell
mkdir ~/.openai
echo -n "INSERT KEY HERE" > ~/.openai/api_key.txt
```
Note that if you have all three set, the plugin defaults to one set in Binary
Ninja. If your API token is invalid, you'll receive the following error:
```python
openai.error.AuthenticationError: Incorrect API key provided: <BAD KEY HERE>.
You can find your API key at https://beta.openai.com.
```
## Usage
### What Does this Function Do?
After installation, you can right-click on any function in Binary Ninja and
select `Plugins > OpenAI > What Does this Function Do (HLIL/Pseudo-C)?`.
Alternatively, select a function in Binary Ninja (by clicking on any instruction
in the function) and use the menu bar options `Plugins > OpenAI > ...`. If your
cursor has anything else selected other than an instruction inside a function,
`OpenAI` will not appear as a selection inside the `Plugins` menu. This can
happen if you've selected data or instructions that Binary Ninja determined did
not belong inside of the function. Additionally, the HLIL options are context
sensitive; if you're looking at the decompiled results in LLIL, you will not see
the HLIL options; this is easily fixed by changing the user view to HLIL
(Pseudo-C should always be visible).
The output will appear in Binary Ninja's Log like so:
![The output of running the plugin.](https://github.com/WhatTheFuzz/binaryninja-openai/blob/main/resources/output.png?raw=true)
### Renaming Variables
I feel like half of reverse engineering is figuring out variable names (which
in-turn assist with program understanding). This plugin is an experimental look
to see if OpenAI can assist with that. Right click on an instruction where a
variable is initialized and select `OpenAI > Rename Variable (HLIL)`. Watch the
magic happen. Here's a quick before-and-after.
![Before renaming](https://github.com/WhatTheFuzz/binaryninja-openai/blob/main/resources/rename-before.png?raw=true)
![After renaming](https://github.com/WhatTheFuzz/binaryninja-openai/blob/main/resources/rename-after.png?raw=true)
Renaming variables only works on HLIL instructions that are initializations (ie.
`HighLevelILVarInit`). You might also want this to support assignments
(`HighLevelILAssign`), but I did not get great results with this. Most of the
responses were just `result`. If your experience is different, please submit a
pull request.
## OpenAI Model
By default, the plugin uses the `text-davinci-003` model, you can tweak this
inside Binary Ninja's preferences. You can access these settings as described in
the [API Key](#api-key) section. It uses the maximum available number of tokens
for each model, as described in [OpenAI's documentation][tokens].
## Known Issues
Please submit an issue if you find something that isn't working properly.
## License
This project is licensed under the [MIT license][license].
[default-plugin-dir]:https://docs.binary.ninja/guide/plugins.html
[token]:https://beta.openai.com/account/api-keys
[tokens]:https://beta.openai.com/docs/models/gpt-3
[entry]:./src/entry.py
[license]:./LICENSE
```
`__init__.py`:
```py
from binaryninja import PluginCommand
from . src.settings import OpenAISettings
from . src.entry import check_function, rename_variable
# Register the settings group in Binary Ninja to store the API key and model.
OpenAISettings()
PluginCommand.register_for_high_level_il_function(r"OpenAI\What Does this Function Do (HLIL)?",
"Checks OpenAI to see what this HLIL function does." \
"Requires an internet connection and an API key "
"saved under the environment variable "
"OPENAI_API_KEY or modify the path in entry.py.",
check_function)
PluginCommand.register_for_function(r"OpenAI\What Does this Function Do (Pseudo-C)?",
"Checks OpenAI to see what this pseudo-C function does." \
"Requires an internet connection and an API key "
"saved under the environment variable "
"OPENAI_API_KEY or modify the path in entry.py.",
check_function)
PluginCommand.register_for_high_level_il_instruction(r"OpenAI\Rename Variable (HLIL)",
"If the current expression is a HLIL Initialization " \
"(HighLevelILVarInit), then query OpenAI to rename the " \
"variable to what it believes is correct. If the expression" \
"is not an HighLevelILVarInit, then do nothing. Requires " \
"an internet connection and an API key. ",
rename_variable)
```
`plugin.json`:
```json
{
"pluginmetadataversion": 2,
"name": "OpenAI GPT3",
"author": "Sean Deaton (@WhatTheFuzz)",
"type": [
"helper"
],
"api": [
"python3"
],
"description": "Queries OpenAI's GPT3 to determine what a given function does.",
"longdescription": "",
"license": {
"name": "MIT",
"text": "Copyright 2022 Sean Deaton (@WhatTheFuzz)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
},
"platforms": [
"Darwin",
"Linux"
],
"installinstructions": {
"Darwin": "`pip3 install --user openai`.\n Add your OpenAI API key to the OpenAI preferences in Binary Ninja.",
"Linux": "`pip3 install --user openai`.\n Add your OpenAI API key to the OpenAI preferences in Binary Ninja."
},
"dependencies": {
"pip": [
"openai"
]
},
"version": "3.0.1",
"minimumbinaryninjaversion": 3200
}
```
`requirements.txt`:
```txt
openai
```
`src/agent.py`:
```py
from __future__ import annotations
from collections.abc import Callable
import os
from typing import Optional, Union
from pathlib import Path
import openai
from openai import APIError
from binaryninja.function import Function
from binaryninja.lowlevelil import LowLevelILFunction
from binaryninja.mediumlevelil import MediumLevelILFunction
from binaryninja.highlevelil import HighLevelILFunction, HighLevelILInstruction, \
HighLevelILVarInit
from binaryninja.settings import Settings
from binaryninja import log, BinaryView
from . query import Query
from . c import Pseudo_C
from . exceptions import NoAPIKeyException
class Agent:
function_question: str = '''
This is a function that was decompiled with Binary Ninja.
It is in IL_FORM. What does this function do?
'''
rename_variable_question: str = "In one word, what should the variable " \
"be for the variable that is assigned to the result of the C " \
"expression:\n"
# A mapping of IL forms to their names.
il_name: dict[type, str] = {
LowLevelILFunction: 'Low Level Intermediate Language',
MediumLevelILFunction: 'Medium Level Intermediate Language',
HighLevelILFunction: 'High Level Intermediate Language',
Function: 'decompiled C code'
}
def __init__(self,
bv: BinaryView,
path_to_api_key: Optional[Path]=None) -> None:
# Read the API key from the environment variable.
self.client = openai.OpenAI(api_key=self.read_api_key(filename=path_to_api_key))
assert bv is not None, 'BinaryView is None. Check how you called this function.'
# Set instance attributes.
self.bv = bv
self.model = self.get_model()
# Used for the callback function.
self.instruction = None
def read_api_key(self, filename: Optional[Path]=None) -> str:
'''Checks for the API key in three locations.
First, it checks the openai.api_key key:value in Binary Ninja
preferences. This is accessed in Binary Ninja by going to Edit >
Preferences > Settings > OpenAI.
Second, it checks the OPENAI_API_KEY environment variable.
Finally, it checks the file specified by the filename argument.
Defaults to ~/.openai/api_key.txt.
'''
# First, check the Binary Ninja settings.
settings: Settings = Settings()
if settings.contains('openai.api_key'):
if key := settings.get_string('openai.api_key'):
return str(key)
# If the settings don't exist, contain the key, or the key is empty,
# check the environment variable.
if key := os.getenv('OPENAI_API_KEY'):
return key
# Finally, if the environment variable doesn't exist, check the default
# file.
if filename:
log.log_info(f'No API key detected under the environment variable '
f'OPENAI_API_KEY. Reading API key from {filename}')
try:
with open(filename, mode='r', encoding='ascii') as api_key_file:
return api_key_file.read()
except FileNotFoundError:
log.log_error(f'Could not find API key file at {filename}.')
raise NoAPIKeyException('No API key found. Refer to the documentation to add the '
'API key.')
def is_valid_model(self, model: str) -> bool:
'''Checks if the model is valid by querying the OpenAI API.'''
models: list = self.client.models.list().data
return model in [m.id for m in models]
def get_model(self) -> str:
'''Returns the model that the user has selected from Binary Ninja's
preferences. The default value is set by the OpenAISettings class. If
for some reason the user selected a model that doesn't exist, this
function defaults to 'text-davinci-003'.
'''
settings: Settings = Settings()
# Check that the key exists.
if settings.contains('openai.model'):
# Check that the key is not empty and get the user's selection.
if model := settings.get_string('openai.model'):
# Check that is a valid model by querying the OpenAI API.
if self.is_valid_model(model):
return str(model)
# Return a valid, default model.
assert self.is_valid_model('text-davinci-003')
return 'text-davinci-003'
def get_token_count(self) -> int:
'''Returns the maximum token count specified by the user. If no value is
set, for whatever reason, returns 1,024.'''
settings: Settings = Settings()
# Check that the key exists.
if settings.contains('openai.max_tokens'):
# Check that the value is not None.
if (max_tokens := settings.get_integer('openai.max_tokens')) is not None:
return int(max_tokens)
return 1_024
def instruction_list(self, function: Union[LowLevelILFunction,
MediumLevelILFunction,
HighLevelILFunction]) -> list[str]:
'''Generates a list of instructions in string representation given a
BNIL function.
'''
# Ensure that a function type was passed in.
if not isinstance(function, (Function, LowLevelILFunction,
MediumLevelILFunction, HighLevelILFunction)):
raise TypeError(f'Expected a BNIL function of type '
f'Function, LowLevelILFunction, '
f'MediumLevelILFunction, or HighLevelILFunction, '
f'got {type(function)}.')
if isinstance(function, Function):
return Pseudo_C(self.bv, function).get_c_source()
instructions: list[str] = []
for instruction in function.instructions:
instructions.append(str(instruction))
return instructions
def generate_query(self, function: Union[Function,
LowLevelILFunction,
MediumLevelILFunction,
HighLevelILFunction]) -> str:
'''Generates a query string given a BNIL function. Returns the query as
a string.
'''
prompt: str = self.function_question
# Read the prompt from the text file.
prompt = prompt.replace('IL_FORM', self.il_name[type(function)])
# Add some new lines. Maybe not necessary.
prompt += '\n\n'
# Add the instructions to the prompt.
prompt += '\n'.join(self.instruction_list(function))
return prompt
def generate_rename_variable_query(self,
instruction: HighLevelILInstruction) -> str:
'''Generates a query string given a BNIL instruction. Returns the query
as a string.
'''
if not isinstance(instruction, HighLevelILVarInit):
raise TypeError(f'Expected a BNIL instruction of type '
f'HighLevelILVarInit got {type(instruction)}.')
# Assign the instruction to the Agent instance. This is used for the
# callback function so we don't need to pass in the instruction to the
# Query instance. This is kind of janky and should be examined in future
# versions.
self.instruction = instruction
prompt: str = self.rename_variable_question
# Get the disassembly lines and add them to the prompt.
for line in instruction.instruction_operands:
prompt += str(line)
return prompt
def rename_variable(self, response: str) -> None:
'''Renames the variable of the instruction saved in the Agent instance
to the response passed in as an argument.
'''
if self.instruction is None:
raise TypeError('No instruction was saved in the Agent instance.')
if response is None or response == '':
raise TypeError(f'No response was returned from OpenAI; got type {type(response)}.')
# Get just one word from the response. Remove spaces and quotes.
try:
response = response.split()[0]
response = response.replace(' ', '')
response = response.replace('"', '')
response = response.replace('\'', '')
except IndexError as error:
raise IndexError(f'Could not split the response: `{response}`.') from error
# Assign the variable name to the response.
log.log_debug(f'Renaming variable in expression {self.instruction} to {response}.')
self.instruction.dest.name = response
def send_query(self, query: str, callback: Optional[Callable]=None) -> None:
'''Sends a query to the engine and prints the response.'''
query = Query(client=self.client,
query_string=query,
model=self.model,
max_token_count=self.get_token_count(),
callback_function=callback)
query.start()
```
`src/c.py`:
```py
from binaryninja.function import Function, DisassemblySettings
from binaryninja.enums import DisassemblyOption
from binaryninja.lineardisassembly import LinearViewObject, LinearViewCursor, \
LinearDisassemblyLine
from binaryninja import BinaryView
class Pseudo_C:
def __init__(self, bv: BinaryView, func: Function) -> None:
self.bv = bv
self.func = func
def get_c_source(self) -> list[str]:
'''Returns a list of strings representing the C source code for the
function.'''
lines: list[str] = []
settings: DisassemblySettings = DisassemblySettings()
settings.set_option(DisassemblyOption.ShowAddress, False)
linear_view: LinearViewObject = LinearViewObject.language_representation(
self.bv, settings)
cursor_end: LinearViewCursor = LinearViewCursor(linear_view)
cursor_end.seek_to_address(self.func.highest_address)
body: list[
LinearDisassemblyLine] = self.bv.get_next_linear_disassembly_lines(
cursor_end)
cursor_end.seek_to_address(self.func.highest_address)
header: list[
LinearDisassemblyLine] = self.bv.get_previous_linear_disassembly_lines(
cursor_end)
for line in header:
lines.append(str(line))
for line in body:
lines.append(str(line))
return lines
```
`src/entry.py`:
```py
from pathlib import Path
from binaryninja import BinaryView, Function
from binaryninja.highlevelil import HighLevelILInstruction, HighLevelILVarInit
from binaryninja.log import log_error
from . agent import Agent
API_KEY_PATH = Path.home() / Path('.openai/api_key.txt')
def check_function(bv: BinaryView, func: Function) -> None:
agent: Agent = Agent(
bv=bv,
path_to_api_key=API_KEY_PATH
)
query: str = agent.generate_query(func)
agent.send_query(query)
def rename_variable(bv: BinaryView, instruction: HighLevelILInstruction) -> None:
if not isinstance(instruction, HighLevelILVarInit):
log_error(f'Instruction must be of type HighLevelILVarInit, got type: ' \
f'{type(instruction)}')
return
agent: Agent = Agent(
bv=bv,
path_to_api_key=API_KEY_PATH
)
query: str = agent.generate_rename_variable_query(instruction)
agent.send_query(query=query, callback=agent.rename_variable)
# Difficult to test without a payment method added, given that the rate limits
# are so low. This should also probably take place in a background task of its
# own.
# def rename_all_variables_in_function(bv: BinaryView, func: HighLevelILFunction) -> None:
# # Get each instruction in the High Level IL Function.
# for instruction in func.instructions:
# match instruction:
# # Rename the variable if it is a HighLevelILVarInit.
# case HighLevelILVarInit():
# rename_variable(bv, instruction)
# # Explicit pass for all other cases.
# case _ :
# pass
```
`src/exceptions.py`:
```py
class NoAPIKeyException(Exception):
pass
class RegisterSettingsGroupException(Exception):
pass
class RegisterSettingsKeyException(Exception):
pass
```
`src/query.py`:
```py
from __future__ import annotations
from collections.abc import Callable
from typing import Optional
from openai import Client
from binaryninja.plugin import BackgroundTaskThread
from binaryninja.log import log_debug, log_info
class Query(BackgroundTaskThread):
def __init__(self, client: Client, query_string: str, model: str,
max_token_count: int, callback_function: Optional[Callable]=None) -> None:
BackgroundTaskThread.__init__(self,
initial_progress_text="",
can_cancel=False)
self.client: Client = client
self.query_string: str = query_string
self.model: str = model
self.max_token_count: int = max_token_count
self.callback = callback_function
def run(self) -> None:
self.progress = "Submitting query to OpenAI."
log_debug(f'Sending query: {self.query_string}')
if self.model in ["gpt-3.5-turbo","gpt-4","gpt-4-32k"]:
response = self.client.chat.completions.create(
model=self.model,
messages=[{"role":"user","content":self.query_string}],
max_tokens=self.max_token_count,
)
# Get the response text.
result: str = response.choices[0].message.content
else:
response = self.client.chat.completions.create(
model=self.model,
prompt=self.query_string,
max_tokens=self.max_token_count,
)
# Get the response text.
result: str = response.choices[0].text
# If there is a callback, do something with it.
if self.callback:
self.callback(result)
# Otherwise, assume we just want to log it.
else:
log_info(result)
```
`src/settings.py`:
```py
import json
from binaryninja.settings import Settings
from . exceptions import RegisterSettingsGroupException, \
RegisterSettingsKeyException
class OpenAISettings(Settings):
def __init__(self) -> None:
# Initialize the settings with the default instance ID.
super().__init__(instance_id='default')
# Register the OpenAI group.
if not self.register_group('openai', 'OpenAI'):
raise RegisterSettingsGroupException('Failed to register OpenAI '
'settings group.')
# Register the setting for the API key.
if not self.register_api_key_settings():
raise RegisterSettingsKeyException('Failed to register OpenAI API '
'key settings.')
# Register the setting for the model used to query.
if not self.register_model_settings():
raise RegisterSettingsKeyException('Failed to register OpenAI '
'model settings.')
# Register the setting for the max tokens used for both the prompt and
# completion.
if not self.register_max_tokens():
raise RegisterSettingsKeyException('Failed to register OpenAI '
'max tokens settings.')
def register_api_key_settings(self) -> bool:
'''Register the OpenAI API key settings in Binary Ninja.'''
# Set the attributes of the settings. Refer to:
# https://api.binary.ninja/binaryninja.settings-module.html
properties = {
'title': 'OpenAI API Key',
'type': 'string',
'description': 'The user\'s OpenAI API key used to make requests '
'the server.'
}
return self.register_setting('openai.api_key', json.dumps(properties))
def register_model_settings(self) -> bool:
'''Register the OpenAI model settings in Binary Ninja.
Defaults to gpt-3.5-turbo.
'''
# Set the attributes of the settings. Refer to:
# https://api.binary.ninja/binaryninja.settings-module.html
properties = {
'title': 'OpenAI Model',
'type': 'string',
'description': 'The OpenAI model used to generate the response.',
# https://beta.openai.com/docs/models
'enum': [
'gpt-4',
'gpt-4-32k',
'gpt-3.5-turbo',
'text-davinci-003',
'text-davinci-002',
'text-curie-001',
'text-babbage-001',
'text-babbage-002',
'code-davinci-002',
'code-cushman-001'
],
'enumDescriptions': [
'More capable than any GPT-3.5 model, able to do more complex tasks, and optimized for chat. Will be updated with our latest model iteration.',
'Same capabilities as the base gpt-4 mode but with 4x the context length. Will be updated with our latest model iteration.',
'Most capable GPT-3.5 model and optimized for chat at 1/10th the cost of text-davinci-003. Will be updated with our latest model iteration.',
'Can do any language task with better quality, longer output, and consistent instruction-following than the curie, babbage, or ada models.',
'Similar capabilities to text-davinci-003 but trained with supervised fine-tuning instead of reinforcement learning'
'Very capable, but faster and lower cost than Davinci.',
'Capable of straightforward tasks, very fast, and lower cost.',
'Capable of very simple tasks, usually the fastest model in the GPT-3 series, and lowest cost.',
'Most capable Codex model. Particularly good at translating natural language to code. In addition to completing code, also supports inserting completions within code.',
'Almost as capable as Davinci Codex, but slightly faster. This speed advantage may make it preferable for real-time applications.'
],
'default': 'gpt-3.5-turbo'
}
return self.register_setting('openai.model', json.dumps(properties))
def register_max_tokens(self) -> bool:
'''Register the OpenAI max tokens used for both the prompt and
completion. Defaults to 2,048. The Davinci model can use 4,000 or 8,000
tokens for GPT and Codex respectively. Check out the documentation here:
https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them
'''
properties = {
'title': 'OpenAI Max Completion Tokens',
'type': 'number',
'description': 'The maximum number of tokens used for completion. Tokens do not necessarily align with word or instruction count. Typically, each token is four characters. If your function is very large, you may need to decrease this value, as the number of tokens in your prompt counts against the total number of tokens supported by the model. Not all models support the same number of maximum tokens; most support 2,048 tokens. For larger functions, check out text-davinci-003 and code-davinci-002 which support 4,000 and 8,000 respectively. For the maximum amount of tokens, check out gpt-4-32k which supports up to 32,768 tokens. This may be costly, however.',
'default': 1_024,
'minValue': 1,
'maxValue': 32_768,
'message': "Min: 1, Max: 32,768"
}
return self.register_setting('openai.max_tokens',
json.dumps(properties))
```
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long