Skip to content

Commit 4573c27

Browse files
committed
update
1 parent 5d25eab commit 4573c27

File tree

3 files changed

+263
-28
lines changed

3 files changed

+263
-28
lines changed

__init__.py

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
import os
2+
import sys
3+
import requests
4+
import bs4
5+
import binaryninja
6+
7+
# Logic:
8+
# ver, hash = native_plugins_data folder exists ? (current_native_plugin_data file exists ? ver, hash : none) : none
9+
# plugin_found = current_native_plugin file exists ? true : false
10+
# if !plugin_found -> download plugin && create (or overwrite) current_native_plugin_data file containing latest ver and latest hash
11+
# if plugin_found && ver != latest_ver && ver != none && hash == downloaded plugin hash -> alert user he is using outdated plugin (with link to latest release)
12+
# if plugin_found && ver != latest_ver && ver != none && hash != downloaded plugin hash -> delete current_native_plugin_data file &&
13+
# download latest plugin to temp, calculate its hash &&
14+
# if latest_hash == downloaded plugin hash -> save latest_hash and latest_ver to current_native_plugin_data file &&
15+
# delete file in temp
16+
# if plugin_found && ver == latest_ver && hash != downloaded plugin hash -> delete current_native_plugin_data file && download latest plugin to temp, calculate its hash &&
17+
# if latest_hash == downloaded plugin hash -> save latest_hash and latest_ver to current_native_plugin_data file &&
18+
# delete file in temp
19+
# if latest_hash != downloaded plugin hash -> alert to update (with link to latest release)
20+
# if plugin_found && ver == none && hash == none -> download latest plugin to temp, calculate its hash &&
21+
# if latest_hash == downloaded plugin hash -> save latest_hash and latest_ver to current_native_plugin_data file &&
22+
# delete file in temp
23+
# if latest_hash != downloaded plugin hash -> alert to update (with link to latest release)
24+
25+
# Function that determines whether system is supported
26+
def is_system_supported(file_name):
27+
return file_name != None and len(file_name) > 0
28+
29+
# Function that determines whether native_plugins_data folder exists
30+
def data_folder_exists():
31+
return os.path.isdir(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data'))
32+
33+
# Function that determines whether current_native_plugin_data file exists
34+
def data_file_exists(plugin_name):
35+
return os.path.isfile(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data', plugin_name + '.data'))
36+
37+
# Function that determines whether temp folder exists
38+
def temp_folder_exists():
39+
return os.path.isdir(os.path.join(binaryninja.user_plugin_path(), 'temp'))
40+
41+
# Function that reads current_native_plugin_data file
42+
def read_data_file(plugin_name):
43+
with open(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data', plugin_name + '.data'), 'r') as f:
44+
return f.read().splitlines()
45+
46+
# Function that writes to current_native_plugin_data file
47+
def write_data_file(plugin_name, version, hash):
48+
with open(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data', plugin_name + '.data'), 'w') as f:
49+
f.write(version + '\n' + hash)
50+
51+
# Function that deletes file from current_native_plugin_data
52+
def delete_data_file(plugin_name):
53+
os.remove(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data', plugin_name + '.data'))
54+
55+
# Function that calculates hash of file
56+
def calculate_hash(file_path):
57+
import hashlib
58+
hash = hashlib.sha256()
59+
with open(file_path, 'rb') as f:
60+
for chunk in iter(lambda: f.read(4096), b""):
61+
hash.update(chunk)
62+
return hash.hexdigest()
63+
64+
# Function that downloads file
65+
def download_file(file_url, file_name):
66+
response = requests.get(file_url)
67+
if response.status_code == 200:
68+
with open(os.path.join(binaryninja.user_plugin_path(), file_name), 'wb') as f:
69+
f.write(response.content)
70+
return True
71+
else:
72+
return False
73+
74+
# Function that downloads file to temp directory
75+
def download_file_to_temp(file_url, file_name):
76+
response = requests.get(file_url)
77+
if response.status_code == 200:
78+
with open(os.path.join(binaryninja.user_plugin_path(), 'temp', file_name), 'wb') as f:
79+
f.write(response.content)
80+
return True
81+
else:
82+
return False
83+
84+
# Function that deletes file
85+
def delete_file(file_path):
86+
os.remove(file_path)
87+
88+
# Function that deletes file from temp directory
89+
def delete_file_from_temp(file_name):
90+
os.remove(os.path.join(binaryninja.user_plugin_path(), 'temp', file_name))
91+
92+
# Function that determines whether plugin is installed
93+
def is_plugin_installed(file_name):
94+
return os.path.isfile(os.path.join(binaryninja.user_plugin_path(), file_name))
95+
96+
# Function that determines whether plugin is outdated
97+
def is_plugin_outdated(plugin_name, version, hash):
98+
if data_folder_exists():
99+
if data_file_exists(plugin_name):
100+
data = read_data_file(plugin_name)
101+
if data[0] == version and data[1] == hash:
102+
return False
103+
else:
104+
return True
105+
else:
106+
return True
107+
else:
108+
return True
109+
110+
# Function that alerts user
111+
def alert_user(plugin_name, description):
112+
binaryninja.interaction.show_message_box('{} (Native plugin loader)'.format(plugin_name), description, binaryninja.enums.MessageBoxButtonSet.OKButtonSet, binaryninja.enums.MessageBoxIcon.InformationIcon)
113+
114+
# Plugin details
115+
plugin_name = 'sigscan'
116+
117+
# Repository details
118+
repo_owner = 'rikodot'
119+
repo_name = 'binja_native_sigscan'
120+
file_url = 'https://github.com/{}/{}/releases/latest/download'.format(repo_owner, repo_name)
121+
122+
# Name of files in release section on github (leave blank if platform not supported)
123+
win_file = 'sigscan.dll'
124+
linux_file = ''
125+
darwin_file = ''
126+
127+
# Retrieve the HTML of the release page
128+
release_url = 'https://github.com/{}/{}/releases/latest'.format(repo_owner, repo_name)
129+
response = requests.get(release_url)
130+
html = response.content
131+
132+
# Parse the HTML to extract the release version
133+
soup = bs4.BeautifulSoup(html, 'html.parser')
134+
latest_version_tag = getattr(soup.find('span', {'class': 'css-truncate-target'}), 'text', None)
135+
latest_version = latest_version_tag.strip() if latest_version_tag else None
136+
137+
# Determine OS we are running on
138+
platform = sys.platform.lower()
139+
140+
# Windows
141+
if platform.startswith('win'):
142+
file_url = '{}/{}'.format(file_url, win_file)
143+
file = win_file
144+
# Linux
145+
elif platform.startswith('linux'):
146+
file_url = '{}/{}'.format(file_url, linux_file)
147+
file = linux_file
148+
# Mac
149+
elif platform.startswith('darwin'):
150+
file_url = '{}/{}'.format(file_url, darwin_file)
151+
file = darwin_file
152+
else:
153+
alert_user(plugin_name, 'Unsupported platform')
154+
155+
# Make sure we have data folder
156+
if not data_folder_exists():
157+
os.mkdir(os.path.join(binaryninja.user_plugin_path(), 'native_plugins_data'))
158+
159+
# Make sure we have temp folder
160+
if not temp_folder_exists():
161+
os.mkdir(os.path.join(binaryninja.user_plugin_path(), 'temp'))
162+
else:
163+
# Delete all files in temp folder
164+
for file in os.listdir(os.path.join(binaryninja.user_plugin_path(), 'temp')):
165+
delete_file_from_temp(file)
166+
167+
# Do the thing
168+
if (is_system_supported(file) and latest_version != None):
169+
plugin_data = (read_data_file(plugin_name) if data_file_exists(plugin_name) else None) if data_folder_exists() else None
170+
# Check if we have both version and hash of the plugin
171+
if plugin_data == None or len(plugin_data) != 2 or plugin_data[0] == None or plugin_data[1] == None:
172+
delete_data_file(plugin_name) if data_file_exists(plugin_name) else None
173+
plugin_data = None
174+
175+
data_version = plugin_data[0] if plugin_data != None else None
176+
data_hash = plugin_data[1] if plugin_data != None else None
177+
if not is_plugin_installed(file):
178+
# Plugin not installed, just download it
179+
if download_file(file_url, file):
180+
# Register plugin in data directory
181+
write_data_file(plugin_name, latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)))
182+
alert_user(plugin_name, 'Plugin downloaded successfully, please restart Binary Ninja to load it')
183+
else:
184+
alert_user(plugin_name, 'Failed to download plugin')
185+
else:
186+
# Plugin installed, no data about the plugin
187+
if (data_version == None and data_hash == None):
188+
# Download latest version of the plugin and check if we have that version
189+
download_file_to_temp(file_url, file)
190+
if (calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)) == calculate_hash(os.path.join(binaryninja.user_plugin_path(), 'temp', file))):
191+
# We have the latest version, register it in data directory
192+
write_data_file(plugin_name, latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)))
193+
delete_file_from_temp(file)
194+
else:
195+
# We don't have the latest version, alert user
196+
alert_user(plugin_name, 'You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
197+
# Plugin installed, but data shows it's outdated
198+
elif (data_version != latest_version):
199+
# Make sure the version in the data directory is actually the version we have installed (we compare hashes - hash now and hash when we downloaded the plugin)
200+
if (data_hash == calculate_hash(os.path.join(binaryninja.user_plugin_path(), file))):
201+
# Yep, version noted in data corresponds to the hash of currently installed plugin
202+
alert_user(plugin_name, 'You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
203+
else:
204+
# Nope, version noted in data doesn't correspond to the hash of currently installed plugin (user probably replaced the plugin)
205+
delete_data_file(plugin_name)
206+
# Download latest version of the plugin and check if we have that version
207+
download_file_to_temp(file_url, file)
208+
if (calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)) == calculate_hash(os.path.join(binaryninja.user_plugin_path(), 'temp', file))):
209+
# We have the latest version, register it in data directory so user is not prompted to update as he probably already did
210+
write_data_file(plugin_name, latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)))
211+
delete_file_from_temp(file)
212+
else:
213+
# We don't have the latest version, alert user
214+
alert_user(plugin_name, 'You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
215+
# Plugin installed, data shows it's up to date, but let's make sure
216+
elif (data_version == latest_version):
217+
# Make sure the version in the data directory is actually the version we have installed (we compare hashes - hash now and hash when we downloaded the plugin)
218+
if (data_hash != calculate_hash(os.path.join(binaryninja.user_plugin_path(), file))):
219+
# Nope, hash noted in data doesn't correspond to the hash of currently installed plugin (user probably replaced the plugin with different version)
220+
delete_data_file(plugin_name)
221+
# Let's check if our plugin matches the hash in the latest github release (developer could have replaced file in the github release and user updated to it)
222+
download_file_to_temp(file_url, file)
223+
if (calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)) == calculate_hash(os.path.join(binaryninja.user_plugin_path(), 'temp', file))):
224+
# Yep, hash of the plugin in the github release corresponds to the hash of currently installed plugin so we have the latest one
225+
write_data_file(plugin_name, latest_version, calculate_hash(os.path.join(binaryninja.user_plugin_path(), file)))
226+
delete_file_from_temp(file)
227+
else:
228+
# Not the latest one (according to the hash in the github release), but user might be intending to test different version of the plugin, add ignore option
229+
alert_user(plugin_name, 'You are using outdated version of this plugin and it must be updated manually\n1. download the latest version from {}\n2. close Binary Ninja\n3. replace the outdated plugin with the newly downloaded file in {}'.format(file_url, binaryninja.user_plugin_path()))
230+
else:
231+
alert_user(plugin_name, 'This plugin is not supported on current platform or plugin not found or its github releases not found')

plugin.json

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
{
2-
"pluginmetadataversion": 2,
3-
"name": "Native SigScan",
4-
"type": [
5-
"helper"
6-
],
7-
"api": [
8-
"python2",
9-
"python3"
10-
],
11-
"description": "Find and create signatures",
12-
"longdescription": "This plugin allows to scan analyzed binaries for signatures or create them. Supports both normal signatures (e.g. 49 28 15 ? ? 30) and commonly used code signatures (e.g. \"\\x49\\x28\\x15\\x00\\x00\\x30\", \"xxx??x\").\n\nAs title suggests, it is coded in C++ and utilizes Binary Ninja's native API. For this reason this plugin serves just as a loader for the [actual plugin](https://github.com/rikodot/binja_native_sigscan).\n\nCurrently works on Windows only!",
13-
"license": {
14-
"name": "MIT",
15-
"text": "MIT License\n\nCopyright (c) 2023 rikodot\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."
16-
},
17-
"platforms": [
18-
"Windows"
19-
],
20-
"installinstructions": {
21-
"Windows": ""
22-
},
23-
"dependencies": {
24-
"pip": ["requests"]
25-
},
26-
"version": "1.0.0",
27-
"author": "rikodot",
28-
"minimumbinaryninjaversion": 0
29-
}
2+
"pluginmetadataversion":2,
3+
"name":"Native SigScan",
4+
"type":[
5+
"helper"
6+
],
7+
"api":[
8+
"python2",
9+
"python3"
10+
],
11+
"description":"Find and create signatures",
12+
"longdescription":"This plugin allows to scan analyzed binaries for signatures or create them. Supports both normal signatures (e.g. 49 28 15 ? ? 30) and commonly used code signatures (e.g. \"\\x49\\x28\\x15\\x00\\x00\\x30\", \"xxx??x\").\n\nAs title suggests, it is coded in C++ and utilizes Binary Ninja's native API. For this reason this plugin serves just as a loader for the [actual plugin](https://github.com/rikodot/binja_native_sigscan).\n\nCurrently works on Windows only!",
13+
"license":{
14+
"name":"MIT",
15+
"text":"MIT License\n\nCopyright (c) 2023 rikodot\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."
16+
},
17+
"platforms":[
18+
"Windows"
19+
],
20+
"installinstructions":{
21+
"Windows":""
22+
},
23+
"dependencies":{
24+
"pip":[
25+
"requests",
26+
"bs4"
27+
]
28+
},
29+
"version":"1.0.0",
30+
"author":"rikodot",
31+
"minimumbinaryninjaversion":0
32+
}

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
requests
2+
bs4

0 commit comments

Comments
 (0)