def select(crds): # -> idxSet, zid assert type(crds) is HandCards, "TypeError: not HandCards" assert 0 < len(crds) <= 5, "IndexError: overrange" keyMap = {'Z': 0, 'X': 1, 'C': 2, 'V': 3, 'B': 4} keyStr = "ZXCVB" keyStr = keyStr[0:len(crds)] # fix length to n print('use [%s] to select:' % keyStr) idxSet = set() selC, actLst = [], [] while True: k = getkey(blocking=True) # block version kn = keys.name(k) if len(kn) == 1 and kn in keyMap: if kn in idxSet: # already in set idxSet ^= {keyMap[kn]} # xor, flip else: idxSet |= {keyMap[kn]} # or, update selC = Cards([crds[i] for i in idxSet]) actLst = match(selC) print(selC, actLst) elif kn in {'ENTER', 'SPACE'} and len(actLst) > 0: if len(actLst) > 1: return idxSet, actLst[choose(actLst)] else: return idxSet, actLst[0] elif kn == "ESC": idxSet = set() else: pass
def processrules(self, string, conndata, dgram_time): """ TODO: This code is not 100% ideal. It places a lot of responsibility on the caller for processing the rule chain, etc. This should be abstracted out and hidden in in the rule module. This is a nice, functional version, though. Very little of this code depends on the class it is in. All it needs is a rule chain. Perhaps a RuleChain class is in order? """ if self.rules is None: return string if len(self.rules) == 0: return string addr = "" port = conndata.serverport if conndata.direction == "c2s": addr = conndata.clientip elif conndata.direction == "s2c": addr = conndata.serverip matchingrules = [] result = None kargs = {'addr':addr, 'port':port, 'direction':conndata.direction, 'payload':string} for rule in self.rules: #matched = rule.match(addr, port, conndata.direction) matched = rule.match(**kargs) if matched: matchingrules.append(rule) # Stop processing unless the rule is a passthru Rule if not rule.passthru: break for rule in matchingrules: self.log.debug("Matched UDP Rule: %s" % (rule)) was_fuzzed = False if rule.action.name == "muck": string = rule.action.execute(data=string) if rule.action.name == "fuzz": old_string = string was_fuzzed, string = rule.action.execute(data=string) if was_fuzzed: tdata = (conndata.clientip, conndata.clientport, conndata.serverip, conndata.serverport, conndata.direction, repr(old_string), repr(string), dgram_time) self.trafficdb.qfuzzudp.put(tdata) return string
def selectR(crds): # -> idxSet, zid assert type(crds) is HandCards, "TypeError: not HandCards" assert 0 < len(crds) <= 5, "IndexError: overrange" bag = list(range(1, 2**len(crds))) while True: if len(bag) > 0: r = randint(0, len(bag) - 1) # as index else: raise RuntimeError("no choice in bag") selPat = [bag[r] & (2**i) > 0 for i in range(5)] # binary exp idxSet = [] selC = [] for i in range(5): if selPat[i]: selC.append(crds[i]) idxSet.append(i) actLst = match(Cards(selC)) if len(actLst) >= 1: return set(idxSet), choice(actLst) else: del bag[r] # remove this val
def processrules(self, string, conndata, msg_cnt = -1): """ TODO: This code is not 100% ideal. It places a lot of responsibility on the caller for processing the rule chain, etc. This should be abstracted out and hidden in in the rule module. This is a nice, functional version, though. Very little of this code depends on the class it is in. All it needs is a rule chain. Perhaps a RuleChain class is in order? """ # return False, string # Make sure there are rules to process if self.rules is None: return False, string if len(self.rules) == 0: return False, string addr = "" port = conndata.serverport if conndata.direction == "c2s": addr = conndata.clientip elif conndata.direction == "s2c": addr = conndata.serverip matchingrules = [] result = None kargs = {'addr':addr, 'port':port, 'direction':conndata.direction, 'payload':string} for rule in self.rules: #matched = rule.match(addr, port, conndata.direction) matched = rule.match(**kargs) if matched: matchingrules.append(rule) # Stop processing unless the rule is a passthru Rule if not rule.passthru: break shoulddebug = False was_fuzzed = False for rule in matchingrules: self.log.debug("Matched rule: %s" % (rule)) # Debug flag set if rule.action.name == "debug": shoulddebug = True # Muck pipe execution if rule.action.name == "muck": string = rule.action.execute(data=string) # Fuzz rule execution if rule.action.name == "fuzz": old_string = string was_fuzzed, string = rule.action.execute(data=string) # Put the old string and new string in the DB if was_fuzzed: self.trafficdb.qfuzztcp.put((conndata.conncount, msg_cnt, conndata.direction, repr(old_string), repr(string))) # Logging cruft if matched and self.config.debug > 2: self.log.debug("==== RULE MATCH ====") self.log.debug("Matching rule action name:%s" % (matchingrule.action.name)) self.log.debug("Matching rule action:%s" % (matchingrule.action)) self.log.debug("Matched rule: %s" % (matchingrule)) self.log.debug(conndata) self.log.debug("Matched class%s" % (matchingrule.__class__)) self.log.debug("addr = %s, port = %d, direction = %s" % (addr, port, conndata.direction)) if shoulddebug: self.log.debug("I should be debugged!!") self.log.debug("==== END RULE ====") # TODO: Seems that processrules should not return a bool only related to debug return shoulddebug, string