def __replaceBytes(self, buffer): width = 1 << rand.Index(4) if len(buffer) < width: width = len(buffer) pos = rand.Index(len(buffer) - width + 1) for i in range(width): buffer[pos + i] = rand.Intn(0xff) return True
def __swapIRP(self): """ """ idx1, idx2 = rand.Index(len(self.irps)), rand.Index(len(self.irps)) if idx1 == idx2: return False self.irps[idx1], self.irps[idx2] = self.irps[idx2], self.irps[idx1] return True
def __insertBytes(self, buffer): n = rand.Index(16) + 1 if len(buffer) + n > MAX_BUFFER_LEN: n = MAX_BUFFER_LEN - len(buffer) if n == 0: return False arr = [] for _ in range(n): arr.append(rand.Intn(0xff)) pos = rand.Index(len(buffer)) buffer = buffer[:pos] + arr + buffer[pos:] return True
def __removeIRP(self): if len(self.irps) <= 1: return False idx = rand.Index(len(self.irps)) del self.irps[idx] return True
def __mutateArg(self): self.set_dirty() idx = rand.Index(len(self.irps)) ok = False while not ok: ok = self.__mutateBuffer(self.irps[idx]) return ok
def mutate_buffer_length(self, index): self.cur_program.set_state('mutate_buffer_length') irp = self.cur_program.irps[index] if irp.InBufferLength <= 1 or irp.OutBufferLength <= 1: return # When it is unable to change the length. if "InBufferLength" in interface_manager[irp.IoControlCode] or \ "OutBufferLength" in interface_manager[irp.IoControlCode]: return orig = copy.deepcopy(irp) for _ in range(16): irp.OutBufferLength = rand.Index(orig.OutBufferLength) irp.InBufferLength = rand.Index(orig.InBufferLength) irp.InBuffer = orig.InBuffer[:irp.InBufferLength] if interface_manager.satisfiable(irp) and self.execute_irp(index): return True self.cur_program.irps[index] = orig
def __addsubBytes(self, buffer): width = 1 << rand.Index(4) if len(buffer) < width: return False pos = rand.Index(len(buffer) - width + 1) byts = buffer[pos:pos + width] delta = rand.Index(2 * MAX_DELTA + 1) - MAX_DELTA if delta == 0: delta = 1 if rand.oneOf(10): v = array2int(byts[::-1]) v += delta byts = int2array(v, width)[::-1] else: v = array2int(byts) v += delta byts = int2array(v, width) buffer[pos:pos + width] = byts return True
def __insertIRP(self, corpus_programs): """ This function inserts a IRP at a randomly chosen point. A IRP which is inserted can be both new and old one. """ if len(self.irps) >= MAX_IRP_COUNT: return False irp = self.__generateIRP( random.choice(list(interface_manager.get_all_codes()))) # TODO: biasd random?? self.irps.insert(rand.Index(len(self.irps)), irp) return True
def __removeBytes(self, buffer): n = rand.Index(16) + 1 pos = rand.Index(len(buffer)) buffer = buffer[:pos] + buffer[pos + n:] return True