I apologize in advance if this is too long. I am studying for a test in app-sec and I have a lab in python (RepyV2 - a python variation) which I need to work on. Unfortunately, I am new to programming. I have just gotten started with Python. Basically, you are presented with a piece of code (reference monitor), and you are required to modify it to prevent a user/attacker from making the code perform certain unauthorized actions. How do I start? I need help Here is the code of the reference monitor:
“”"
"""
TYPE="type"
ARGS="args"
RETURN="return"
EXCP="exceptions"
TARGET="target"
FUNC="func"
OBJC="objc"
class ABFile():
def __init__(self,filename,create):
# globals
mycontext['debug'] = False
# local (per object) reference to the underlying file
self.Afn = filename+'.a'
self.Bfn = filename+'.b'
# make the files and add 'SE' to the readat file...
if create:
self.Afile = openfile(self.Afn,create)
self.Bfile = openfile(self.Bfn,create)
self.Afile.writeat('SE',0)
def writeat(self,data,offset):
# Write the requested data to the B file using the sandbox's writeat call
self.Bfile.writeat(data,offset)
def readat(self,bytes,offset):
# Read from the A file using the sandbox's readat...
return self.Afile.readat(bytes,offset)
def close(self):
self.Afile.close()
self.Bfile.close()
def ABopenfile(filename, create):
return ABFile(filename,create)
# The code here sets up type checking and variable hiding for you. You
# should not need to change anything below here.
sec_file_def = {"obj-type":ABFile,
"name":"ABFile",
"writeat":{"type":"func","args":(str,int),"exceptions":Exception,"return":(int,type(None)),"target":ABFile.writeat},
"readat":{"type":"func","args":((int,type(None)),(int)),"exceptions":Exception,"return":str,"target":ABFile.readat},
"close":{"type":"func","args":None,"exceptions":None,"return":(bool,type(None)),"target":ABFile.close}
}
CHILD_CONTEXT_DEF["ABopenfile"] = {TYPE:OBJC,ARGS:(str,bool),EXCP:Exception,RETURN:sec_file_def,TARGET:ABopenfile}
# Execute the user code
secure_dispatch_module()
================================================================================= Testing your security layer In this part of the assignment you will pretend to be an attacker. Remember the attacker’s objective is to bypass the A/B restrictions or cause the security layer to act in a disallowed manner. By understanding how the attacker thinks, you will be able to write better security layers.
An example of an attack is found below:
if "testfile.txt.a" in listfiles():
removefile("testfile.txt.a")
if "testfile.txt.b" in listfiles():
removefile("testfile.txt.b")
myfile=ABopenfile("testfile.txt",True) #Create an AB file
# I should get 'SE' when reading an empty file...
assert('SE' == myfile.readat(None,0))
# put some valid data in the file.
myfile.writeat("Stest12345E",0)
# I should still get 'SE' because the file wasn't closed.
assert('SE' == myfile.readat(None,0))
#Close the file
myfile.close()
In the example above, a successful attack would cause an error when assert(‘SE’== myfile.readat(None, 0)) is executed because the newly created valid file did not contain ‘SE’, meaning an invalid read occurred
If the example above executed without error, meaning that the newly created file contained ‘SE’, then the attack was successfully defended.
Note: All attacks should be written as Repy V2 files, using the .r2py extension.