class dizzyUSB(object): def __init__(self, filename, timeout, device=DEFAULT_TTY, data="", fuzz_dscr=""): self.filename = filename self.timeout = timeout self.device = device self.data = data self.fuzz_dscr = fuzz_dscr self.sp = None self.d = None def open(self, dst=""): if DEBUG: verbose = 1 else: verbose = 0 ns = {} with open(self.filename) as f: exec(compile(f.read(), self.filename, 'exec'), ns) DD = ns["DD"] success = False if DEBUG: print("setting up facedancer") sys.__stdout__.flush() while not success: try: self.sp = Serial(self.device, 115200, parity=PARITY_NONE, timeout=2) self.fd = Facedancer(self.sp, verbose=verbose) self.app = MAXUSBApp(self.fd, verbose=verbose) self.d = dizzyUSBDevice(self.app, DD, verbose, self.data, self.fuzz_dscr) success = True except: time.sleep(0.1) self.d.connect() self.t = threading.Thread(target=self.run) self.ep = None self.opened = False if not dst == "": self.ep = int(dst) self.t.start() self.opened = True if DEBUG: print("Waiting for USB to setup...") if self.fuzz_dscr == "": time.sleep(2) else: times = self.timeout while (not (self.d.dd_sent and self.d.cd_sent and self.d.scr_recieved and False)) and times > 0: if DEBUG: sys.__stdout__.write(".") sys.__stdout__.flush() time.sleep(0.1) times -= 1 if DEBUG: sys.__stdout__.write("\n") sys.__stdout__.flush() if times <= 0 and DEBUG: print("timeout reached, canceled!") #raise return if DEBUG: print("USB setup complete.") def run(self): try: self.d.run() except SerialException: pass except select.error: pass except OSError: pass except TypeError: pass except IndexError: pass except Exception as e: if DEBUG: traceback.print_exc() print(e) self.opened = False def close(self): if not self.open: return if not self.d is None: try: self.d.disconnect() except IndexError: pass except SerialException: pass except ValueError: pass except Exception as e: if DEBUG: traceback.print_exc() print(e) if not self.sp is None: self.sp.close() self.open = False def read(self): pass def write(self, data): if not self.ep is None: while not self.opened: time.sleep(0.1) try: self.app.send_on_endpoint(self.ep, data) except Exception as e: #~ if DEBUG: #~ traceback.print_exc() #~ print(e) raise e