Safeexec Security Tests Examplemultiprocessing from multiprocessing import Process def f(name): print('hello', name) if __name__ == '__main__': p = Process(target=f, args=('bob',)) p.start() p.join() Example32 or 64 bit? import math print(math.log(_sys.maxsize)/math.log(2)) Exampleyou can change the process name # this changes the thread name # run # ps Hcx -ef # to see the effect def set_proc_name(newname): from ctypes import cdll, byref, create_string_buffer libc = cdll.LoadLibrary('libc.so.6') buff = create_string_buffer(len(newname)+1) buff.value = newname libc.prctl(15, byref(buff), 0, 0, 0) def get_proc_name(): from ctypes import cdll, byref, create_string_buffer libc = cdll.LoadLibrary('libc.so.6') buff = create_string_buffer(128) # 16 == PR_GET_NAME from libc.prctl(16, byref(buff), 0, 0, 0) return buff.value import sys # sys.argv[0] == 'python' # outputs 'python' print(get_proc_name()) set_proc_name(bytes('this is the new process name', 'utf-8')) # outputs 'this is'... print(get_proc_name()) Exampleonly root can increase setrlimit import resource print(resource.getrlimit(resource.RLIMIT_AS)) resource.setrlimit(resource.RLIMIT_AS, (100000000, 100000000)) print(resource.getrlimit(resource.RLIMIT_AS)) resource.setrlimit(resource.RLIMIT_AS, (100000001, 100000001)) print(resource.getrlimit(resource.RLIMIT_AS)) Exampletrying to send email import smtplib s = smtplib.SMTP() s.connect() addr = "PUT@ADDRESS.HERE" s.sendmail(addr, addr, "email sent from pybox") s.quit() Exampletrying to retrieve a webpage import http.client conn = http.client.HTTPConnection("www.python.org") conn.request("GET", "/index.html") r1 = conn.getresponse() print(r1.status, r1.reason) Exampleforbidden function import os os.fork() Exampleinfinite loop while True: i=1 Exampleiptables check import socket UDP_IP = "129.97.140.241" # cemclinux1 -- talking to self should be blocked #UDP_IP = "173.194.73.105" # this is google UDP_PORT = 5005 MESSAGE = "Hello, World!" sock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM ) for i in range(0, 100): print(sock.sendto( bytes(MESSAGE, 'utf8') , (UDP_IP, UDP_PORT) )) Examplecall from subprocess import call call(["ls", "-l"]) Exampleloquacious import sys while True: print('blah') sys.stderr.write('blah') Examplememory s='s' while True: s=s+s Examplereading a directory listing import os cd=os.getcwd() print(cd) print(os.listdir(cd)) ExampleWriting a file import random r=random.randrange(0,999999999) fname='pybox_test_'+str(r) f=open(fname, 'w') f.write('I am filling your disk') Exampleallowed and denied things import os from inspect import currentframe print(os.getcwd()) #should work print(os.getuid(), os.geteuid()) #should work print(os.getgid(), os.getegid(), 1000, "should be same") try: print("contents of /lib", os.listdir('/lib')) #should work except OSError as E: print('ok') print("currrent filename", currentframe().f_code.co_filename) try: print('bad', os.listdir(os.getcwd())) #should work except OSError as E: print("ok:", E) try: print("bad:", os.listdir('/')) except OSError as E: print("ok:", E) try: print("contents of /scratch:", os.listdir('/scratch')) except OSError as E: print("ok:", E) try: print(len(open('safeexec.out').read())) except IOError as E: print("ok:", E) print(len(open(currentframe().f_code.co_filename).read())) print(len(open('usercode').read())) print(len(open('/static/_UTILITIES.py').read())) try: print(open('/README').read()) except IOError as E: print('ok') try: print(len(open('usercode').write('test'))) except IOError as E: print("ok:", E) try: print(len(open('/static/_UTILITIES.py').write('test'))) except IOError as E: print("ok:", E) Examplesleep until wall clock times out (8 seconds - but ajax times out first) import time,os print('My pid is', os.getpid(), 'uid', os.getuid()) i=1 while True: time.sleep(1) Exampletry to kill recently-created processes import os,signal pid = os.getpid() print('My pid is', pid) for i in range(pid-10,pid): try: os.kill(i,signal.SIGABRT) print('killed process id', i) except: pass for i in range(pid+1,pid+10): try: os.kill(i,signal.SIGABRT) print('killed process id', i) except: pass os.kill(pid,signal.SIGABRT) ExampleUseful for debugging ASCII issues import sys, locale, os #print(sys.stdout.encoding) #print(sys.stdout.isatty()) print(locale.getpreferredencoding()) print(sys.getfilesystemencoding()) print(os.environ["PYTHONIOENCODING"]) print(chr(246), chr(9786), chr(9787)) ExampleDon't cache hundreds of megs of stuff! while True: print('x '*100000) ExampleFirst network check import urllib.request import urllib.error import os class NoRedirectHandler(urllib.request.HTTPRedirectHandler): def redirect_request(self, req, fp, code, msg, headers, newurl): return None opener = urllib.request.build_opener(NoRedirectHandler) req = urllib.request.Request( "http://172.16.76.15/", headers={ "Host": "iss-app.private.uwaterloo.ca", "User-Agent": "Python-Test/1.0", } ) try: response = opener.open(req) print("Status:", response.status) print("Headers:") print(response.headers) print("Body:") print(response.read().decode("utf-8", errors="replace")) except urllib.error.HTTPError as e: print("Status:", e.code) print("Headers:") print(e.headers) location = e.headers.get("Location") if location: print("Redirect Location:", location) print("Body:") ExampleSecond network check import urllib.request import urllib.error import os class NoRedirectHandler(urllib.request.HTTPRedirectHandler): def redirect_request(self, req, fp, code, msg, headers, newurl): return None opener = urllib.request.build_opener(NoRedirectHandler) req = urllib.request.Request( "http://104.20.23.154/", headers={ "Host": "example.com", "User-Agent": "Python-Test/1.0", } ) try: response = opener.open(req) print("Status:", response.status) print("Headers:") print(response.headers) print("Body:") print(response.read().decode("utf-8", errors="replace")) except urllib.error.HTTPError as e: print("Status:", e.code) print("Headers:") print(e.headers) location = e.headers.get("Location") if location: print("Redirect Location:", location) print("Body:")