forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
679 lines (551 loc) · 18.9 KB
/
project.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# Copyright (c) 2015-2025 Vector 35 Inc
#
# 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.
import ctypes
from contextlib import contextmanager
from os import PathLike
from typing import Callable, List, Optional, Union
import binaryninja
from . import _binaryninjacore as core
from .exceptions import ProjectException
from .metadata import Metadata, MetadataValueType
ProgressFuncType = Callable[[int, int], bool]
AsPath = Union[PathLike, str]
#TODO: notifications
def _nop(*args, **kwargs):
"""
Function that just returns True, used as default for callbacks
:return: True
"""
return True
def _wrap_progress(progress_func: ProgressFuncType):
"""
Wraps a progress function in a ctypes function for passing to the FFI
:param progress_func: Python progress function
:return: Wrapped ctypes function
"""
return ctypes.CFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.c_ulonglong, ctypes.c_ulonglong)(
lambda ctxt, cur, total: progress_func(cur, total))
class ProjectFile:
"""
Class representing a file in a project
"""
def __init__(self, handle: core.BNProjectFileHandle):
self._handle = handle
def __del__(self):
if core is not None:
core.BNFreeProjectFile(self._handle)
def __repr__(self) -> str:
path = self.name
parent = self.folder
while parent is not None:
path = parent.name + '/' + path
parent = parent.parent
return f'<ProjectFile: {self.project.name}/{path}>'
def __str__(self) -> str:
path = self.name
parent = self.folder
while parent is not None:
path = parent.name + '/' + path
parent = parent.parent
return f'<ProjectFile: {self.project.name}/{path}>'
@property
def project(self) -> 'Project':
"""
Get the project that owns this file
:return: Project that owns this file
"""
proj_handle = core.BNProjectFileGetProject(self._handle)
if proj_handle is None:
raise ProjectException("Failed to get project for file")
return Project(handle=proj_handle)
@property
def path_on_disk(self) -> str:
"""
Get the path on disk to this file's contents
:return: Path on disk as a string
"""
return core.BNProjectFileGetPathOnDisk(self._handle) # type: ignore
@property
def exists_on_disk(self) -> bool:
"""
Check if this file's contents exist on disk
:return: True if this file's contents exist on disk, False otherwise
"""
return core.BNProjectFileExistsOnDisk(self._handle)
@property
def id(self) -> str:
"""
Get the unique id of this file
:return: Unique identifier of this file
"""
return core.BNProjectFileGetId(self._handle) # type: ignore
@property
def name(self) -> str:
"""
Get the name of this file
:return: Name of this file
"""
return core.BNProjectFileGetName(self._handle) # type: ignore
@name.setter
def name(self, new_name: str) -> bool:
"""
Set the name of this file
:param new_name: Desired name
"""
return core.BNProjectFileSetName(self._handle, new_name)
@property
def description(self) -> str:
"""
Get the description of this file
:return: Description of this file
"""
return core.BNProjectFileGetDescription(self._handle) # type: ignore
@description.setter
def description(self, new_description: str) -> bool:
"""
Set the description of this file
:param new_description: Desired description
"""
return core.BNProjectFileSetDescription(self._handle, new_description)
@property
def folder(self) -> Optional['ProjectFolder']:
"""
Get the folder that contains this file
:return: Folder that contains this file, or None
"""
folder_handle = core.BNProjectFileGetFolder(self._handle)
if folder_handle is None:
return None
return ProjectFolder(handle=folder_handle)
@folder.setter
def folder(self, new_folder: Optional['ProjectFolder']) -> bool:
"""
Set the folder that contains this file
:param new_parent: The folder that will contain this file, or None
"""
folder_handle = None if new_folder is None else new_folder._handle
return core.BNProjectFileSetFolder(self._handle, folder_handle)
def export(self, dest: AsPath) -> bool:
"""
Export this file to disk
:param dest: Destination path for the exported contents
:return: True if the export succeeded, False otherwise
"""
return core.BNProjectFileExport(self._handle, str(dest))
class ProjectFolder:
"""
Class representing a folder in a project
"""
def __init__(self, handle: core.BNProjectFolderHandle):
self._handle = handle
def __del__(self):
if core is not None:
core.BNFreeProjectFolder(self._handle)
def __repr__(self) -> str:
path = self.name
parent = self.parent
while parent is not None:
path = parent.name + '/' + path
parent = parent.parent
return f'<ProjectFolder: {self.project.name}/{path}>'
def __str__(self) -> str:
path = self.name
parent = self.parent
while parent is not None:
path = parent.name + '/' + path
parent = parent.parent
return f'<ProjectFolder: {self.project.name}/{path}>'
@property
def project(self) -> 'Project':
"""
Get the project that owns this folder
:return: Project that owns this folder
"""
proj_handle = core.BNProjectFolderGetProject(self._handle)
if proj_handle is None:
raise ProjectException("Failed to get project for folder")
return Project(handle=proj_handle)
@property
def id(self) -> str:
"""
Get the unique id of this folder
:return: Unique identifier of this folder
"""
return core.BNProjectFolderGetId(self._handle) # type: ignore
@property
def name(self) -> str:
"""
Get the name of this folder
:return: Name of this folder
"""
return core.BNProjectFolderGetName(self._handle) # type: ignore
@name.setter
def name(self, new_name: str) -> bool:
"""
Set the name of this folder
:param new_name: Desired name
"""
return core.BNProjectFolderSetName(self._handle, new_name)
@property
def description(self) -> str:
"""
Get the description of this folder
:return: Description of this folder
"""
return core.BNProjectFolderGetDescription(self._handle) # type: ignore
@description.setter
def description(self, new_description: str) -> bool:
"""
Set the description of this folder
:param new_description: Desired description
"""
return core.BNProjectFolderSetDescription(self._handle, new_description)
@property
def parent(self) -> Optional['ProjectFolder']:
"""
Get the parent folder of this folder
:return: Folder that contains this folder, or None if it is a root folder
"""
folder_handle = core.BNProjectFolderGetParent(self._handle)
if folder_handle is None:
return None
return ProjectFolder(handle=folder_handle)
@parent.setter
def parent(self, new_parent: Optional['ProjectFolder']) -> bool:
"""
Set the parent folder of this folder
:param new_parent: The folder that will contain this folder, or None
"""
parent_handle = None if new_parent is None else new_parent._handle
return core.BNProjectFolderSetParent(self._handle, parent_handle)
def export(self, dest: AsPath, progress_func: ProgressFuncType = _nop) -> bool:
"""
Recursively export this folder to disk
:param dest: Destination path for the exported contents
:param progress_func: Progress function that will be called as contents are exporting
:return: True if the export succeeded, False otherwise
"""
return core.BNProjectFolderExport(self._handle, str(dest), None, _wrap_progress(progress_func))
class Project:
"""
Class representing a project
"""
def __init__(self, handle: core.BNProjectHandle):
self._handle = handle
def __del__(self):
if core is not None:
core.BNFreeProject(self._handle)
def __repr__(self) -> str:
return f'<Project: {self.name}>'
def __str__(self) -> str:
return f'<Project: {self.name}>'
@staticmethod
def open_project(path: AsPath) -> 'Project':
"""
Open an existing project
:param path: Path to the project directory (.bnpr) or project metadata file (.bnpm)
:return: Opened project
:raises ProjectException: If there was an error opening the project
"""
binaryninja._init_plugins()
project_handle = core.BNOpenProject(str(path))
if project_handle is None:
raise ProjectException("Failed to open project")
return Project(handle=project_handle)
@staticmethod
def create_project(path: AsPath, name: str) -> 'Project':
"""
Create a new project
:param path: Path to the project directory (.bnpr)
:param name: Name of the new project
:return: Opened project
:raises ProjectException: If there was an error creating the project
"""
binaryninja._init_plugins()
project_handle = core.BNCreateProject(str(path), name)
if project_handle is None:
raise ProjectException("Failed to create project")
return Project(handle=project_handle)
def open(self) -> bool:
"""
Open a closed project
:return: True if the project is now open, False otherwise
"""
return core.BNProjectOpen(self._handle)
def close(self) -> bool:
"""
Close an opened project
:return: True if the project is now closed, False otherwise
"""
return core.BNProjectClose(self._handle)
@property
def id(self) -> str:
"""
Get the unique id of this project
:return: Unique identifier of project
"""
return core.BNProjectGetId(self._handle) # type: ignore
@property
def is_open(self) -> bool:
"""
Check if the project is currently open
:return: True if the project is currently open, False otherwise
"""
return core.BNProjectIsOpen(self._handle)
@property
def path(self) -> str:
"""
Get the path of the project
:return: Path of the project's .bnpr directory
"""
return core.BNProjectGetPath(self._handle) # type: ignore
@property
def name(self) -> str:
"""
Get the name of the project
:return: Name of the project
"""
return core.BNProjectGetName(self._handle) # type: ignore
@name.setter
def name(self, new_name: str):
"""
Set the name of the project
:param new_name: Desired name
"""
core.BNProjectSetName(self._handle, new_name)
@property
def description(self) -> str:
"""
Get the description of the project
:return: Description of the project
"""
return core.BNProjectGetDescription(self._handle) # type: ignore
@description.setter
def description(self, new_description: str):
"""
Set the description of the project
:param new_description: Desired description
"""
core.BNProjectSetDescription(self._handle, new_description)
def query_metadata(self, key: str) -> MetadataValueType:
"""
Retrieves metadata stored under a key from the project
:param str key: Key to query
"""
md_handle = core.BNProjectQueryMetadata(self._handle, key)
if md_handle is None:
raise KeyError(key)
return Metadata(handle=md_handle).value
def store_metadata(self, key: str, value: MetadataValueType):
"""
Stores metadata within the project
:param str key: Key under which to store the Metadata object
:param Varies value: Object to store
"""
_val = value
if not isinstance(_val, Metadata):
_val = Metadata(_val)
core.BNProjectStoreMetadata(self._handle, key, _val.handle)
def remove_metadata(self, key: str):
"""
Removes the metadata associated with this key from the project
:param str key: Key associated with the metadata object to remove
"""
core.BNProjectRemoveMetadata(self._handle, key)
def create_folder_from_path(self, path: Union[PathLike, str], parent: Optional[ProjectFolder] = None, description: str = "", progress_func: ProgressFuncType = _nop) -> ProjectFolder:
"""
Recursively create files and folders in the project from a path on disk
:param path: Path to folder on disk
:param parent: Parent folder in the project that will contain the new contents
:param description: Description for created root folder
:param progress_func: Progress function that will be called
:return: Created root folder
"""
parent_handle = parent._handle if parent is not None else None
folder_handle = core.BNProjectCreateFolderFromPath(
project=self._handle,
path=str(path),
parent=parent_handle,
description=description,
ctxt=None,
progress=_wrap_progress(progress_func)
)
if folder_handle is None:
raise ProjectException("Failed to create folder")
return ProjectFolder(handle=folder_handle)
def create_folder(self, parent: Optional[ProjectFolder], name: str, description: str = "") -> ProjectFolder:
"""
Recursively create files and folders in the project from a path on disk
:param parent: Parent folder in the project that will contain the new folder
:param name: Name for the created folder
:param description: Description for created folder
:return: Created folder
"""
parent_handle = parent._handle if parent is not None else None
folder_handle = core.BNProjectCreateFolder(
project=self._handle,
parent=parent_handle,
name=name,
description=description,
)
if folder_handle is None:
raise ProjectException("Failed to create folder")
return ProjectFolder(handle=folder_handle)
@property
def folders(self) -> List[ProjectFolder]:
"""
Get a list of folders in the project
:return: List of folders in the project
"""
count = ctypes.c_size_t()
value = core.BNProjectGetFolders(self._handle, count)
if value is None:
raise ProjectException("Failed to get list of project folders")
result = []
try:
for i in range(count.value):
folder_handle = core.BNNewProjectFolderReference(value[i])
if folder_handle is None:
raise ProjectException("core.BNNewProjectFolderReference returned None")
result.append(ProjectFolder(folder_handle))
return result
finally:
core.BNFreeProjectFolderList(value, count.value)
def get_folder_by_id(self, id: str) -> Optional[ProjectFolder]:
"""
Retrieve a folder in the project by unique id
:param id: Unique identifier for a folder
:return: Folder with the requested id or None
"""
handle = core.BNProjectGetFolderById(self._handle, id)
if handle is None:
return None
folder = ProjectFolder(handle)
return folder
def delete_folder(self, folder: ProjectFolder, progress_func: ProgressFuncType = _nop) -> bool:
"""
Recursively delete a folder from the project
:param folder: Folder to delete recursively
:param progress_func: Progress function that will be called as objects get deleted
:return: True if the folder was deleted, False otherwise
"""
return core.BNProjectDeleteFolder(self._handle, folder._handle, None, _wrap_progress(progress_func))
def create_file_from_path(self, path: AsPath, folder: Optional[ProjectFile], name: str, description: str = "", progress_func: ProgressFuncType = _nop) -> ProjectFile:
"""
Create a file in the project from a path on disk
:param path: Path on disk
:param folder: Folder to place the created file in
:param name: Name to assign to the created file
:param description: Description to assign to the created file
:param progress_func: Progress function that will be called as the file is being added
"""
folder_handle = folder._handle if folder is not None else None
file_handle = core.BNProjectCreateFileFromPath(
project=self._handle,
path=str(path),
folder=folder_handle,
name=name,
description=description,
ctxt=None,
progress=_wrap_progress(progress_func)
)
if file_handle is None:
raise ProjectException("Failed to create file")
return ProjectFile(handle=file_handle)
def create_file(self, contents: bytes, folder: Optional[ProjectFile], name: str, description: str = "", progress_func: ProgressFuncType = _nop) -> ProjectFile:
"""
Create a file in the project
:param contents: Bytes of the file that will be created
:param folder: Folder to place the created file in
:param name: Name to assign to the created file
:param description: Description to assign to the created file
:param progress_func: Progress function that will be called as the file is being added
"""
folder_handle = folder._handle if folder is not None else None
buf = (ctypes.c_ubyte * len(contents))()
ctypes.memmove(buf, contents, len(contents))
file_handle = core.BNProjectCreateFile(
project=self._handle,
contents=buf,
contentsSize=len(contents),
folder=folder_handle,
name=name,
description=description,
ctxt=None,
progress=_wrap_progress(progress_func)
)
if file_handle is None:
raise ProjectException("Failed to create file")
return ProjectFile(handle=file_handle)
@property
def files(self) -> List[ProjectFile]:
"""
Get a list of files in the project
:return: List of files in the project
"""
count = ctypes.c_size_t()
value = core.BNProjectGetFiles(self._handle, count)
if value is None:
raise ProjectException("Failed to get list of project files")
result = []
try:
for i in range(count.value):
file_handle = core.BNNewProjectFileReference(value[i])
if file_handle is None:
raise ProjectException("core.BNNewProjectFileReference returned None")
result.append(ProjectFile(file_handle))
return result
finally:
core.BNFreeProjectFileList(value, count.value)
def get_file_by_id(self, id: str) -> Optional[ProjectFile]:
"""
Retrieve a file in the project by unique id
:param id: Unique identifier for a file
:return: File with the requested id or None
"""
handle = core.BNProjectGetFileById(self._handle, id)
if handle is None:
return None
file = ProjectFile(handle)
return file
def delete_file(self, file: ProjectFile) -> bool:
"""
Delete a file from the project
:param file: File to delete
:return: True if the file was deleted, False otherwise
"""
return core.BNProjectDeleteFile(self._handle, file._handle)
@contextmanager
def bulk_operation(self):
"""
A context manager to speed up bulk project operations.
Project modifications are synced to disk in chunks,
and the project on disk vs in memory may not agree on state
if an exception occurs while a bulk operation is happening.
:Example:
>>> from pathlib import Path
>>> with project.bulk_operation():
... for i in Path('/bin/').iterdir():
... if i.is_file() and not i.is_symlink():
... project.create_file_from_path(i, None, i.name)
"""
core.BNProjectBeginBulkOperation(self._handle)
yield
core.BNProjectEndBulkOperation(self._handle)