-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreference_monitor.r2py
89 lines (79 loc) · 3.88 KB
/
reference_monitor.r2py
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
# Constants used as dictionary keys for security definitions
TYPE = "type"
ARGS = "args"
RETURN = "return"
EXCP = "exceptions"
TARGET = "target"
FUNC = "func"
OBJC = "objc"
# Definition of a class ABFile
class ABFile:
def __init__(self, filename, create):
# Set the 'debug' flag in the context to False
mycontext['debug'] = False
# Initialize variables to store file names
self.Afn = filename + '.a' # File 'A' name
self.Bfn = filename + '.b' # File 'B' name
self.original_filename = filename
# Depending on the 'create' flag, create or open files 'A' and 'B'
if create:
# If 'A' file does not exist, create both 'A' and 'B' files
if self.Afn not in listfiles():
self.Afile = openfile(self.Afn, True)
self.Bfile = openfile(self.Bfn, True)
self.Afile.writeat('SE', 0) # Write 'SE' at the beginning of file 'A'
else:
# If 'A' file exists, open 'A' in read mode and 'B' in write mode
self.Afile = openfile(self.Afn, False)
self.Bfile = openfile(self.Bfn, True)
else:
# If 'create' flag is False, open 'A' in read mode and 'B' in write mode
self.Afile = openfile(self.Afn, False)
self.Bfile = openfile(self.Bfn, True)
# Method to write data at a specific offset in file 'B'
def writeat(self, data, offset):
if offset < 0:
raise ValueError("Offset cannot be negative")
if not data:
raise ValueError("Data cannot be empty")
self.Bfile.writeat(data, offset)
# Method to read a certain number of bytes from file 'A' at a given offset
def readat(self, bytes, offset):
if offset < 0:
raise ValueError("Offset cannot be negative")
return self.Afile.readat(bytes, offset)
# Method to close both files 'A' and 'B' with a backup mechanism
def close(self):
# Read the content of file 'B' at offset 0
backup_data = self.Bfile.readat(None, 0)
# If the content starts with "S" and ends with "E," it's considered valid backup data
if backup_data.startswith("S") and backup_data.endswith("E"):
# Restore the content of file 'A' from the backup data
self.Afile.writeat(backup_data, 0)
# Open the original file and restore its content as well
original_file = openfile(self.original_filename, True)
original_file.writeat(backup_data, 0)
original_file.close()
# Close both files 'A' and 'B' and remove file 'B'
self.Afile.close()
self.Bfile.close()
removefile(self.Bfn)
# Function to create or open instances of ABFile
def ABopenfile(filename, create):
# Check if the filename ends with '.a' or '.b' and raise an exception if so
if filename.endswith('.a') or filename.endswith('.b'):
raise Exception("Security violation: Cannot open a file ending with '.a' or '.b' for writing.")
# Create and return an ABFile object with the specified filename and create flag
return ABFile(filename, create)
# Dictionary containing security definitions for ABFile methods
sec_file_def = {
"obj-type": ABFile,
"name": "ABFile",
"writeat": {TYPE: FUNC, ARGS: (str, (int, int)), EXCP: Exception, RETURN: (int, type(None)), TARGET: ABFile.writeat},
"readat": {TYPE: FUNC, ARGS: ((int, int, type(None)), (int, int)), EXCP: Exception, RETURN: str, TARGET: ABFile.readat},
"close": {TYPE: FUNC, ARGS: None, EXCP: Exception, RETURN: (bool, type(None)), TARGET: ABFile.close}
}
# Add the security definitions for ABopenfile to CHILD_CONTEXT_DEF dictionary
CHILD_CONTEXT_DEF["ABopenfile"] = {TYPE: OBJC, ARGS: (str, bool), EXCP: Exception, RETURN: sec_file_def, TARGET: ABopenfile}
# Call a function secure_dispatch_module() for some security-related operation
secure_dispatch_module()