def __add__(self,rhs): "Add histograms in rhs to current sample and create a new sample" s = self.Clone() print 'Plotter ### in add for Label = ', self.Label # Deal with samples not having hists (e.g. files not found) if len(rhs._HistDict)==0: return s if len(s._HistDict)==0: return rhs #make copies so elements can be removed without lasting damage RhsHistDict = rhs._HistDict SHistDict = s._HistDict # make a new OrderedDict that contains only the hists that are in both rhs and self and fill it with those in self MinHistDict = OrderedDict() for key, obj in SHistDict.iteritems(): if SHistDict.has_key(key) and RhsHistDict.has_key(key): MinHistDict[key]=obj if testVerbose: print 'Sample ### MinHistDict = ', MinHistDict #now add rhs to the new ordered dict for key, obj in MinHistDict.iteritems(): MinHistDict[key].Add(RhsHistDict[key]) self._OpSetProperties(s, rhs, '+') #alter the hist dict of copy of self by setting it to the new hist dict created above and return it s._HistDict = MinHistDict return s
def sort_recursive(dictionary): """ Recursively sorts nested dictionaries. Should not be applied if the structures are nested too deeply and/or there is even the remote possibility that the nesting of the passed dictionary contains a cyclic structure. Args: dictionary (dict): A python dictionary Returns: A recursively sorted dictionary Example: >>> dict = { 'a': '2', 'c': 3, 'b': { 'e': 4, 'd': 1 }, 'f': 5} >>> sort_recursive(dict) OrderedDict([('a', '2'), ('b', OrderedDict([('d', 1), ('e', 4)])), ('c', 3), ('f', 5)]) """ sorted_list = OrderedDict(sorted(dictionary.items(), key = lambda x: x[0])) # TODO test for cyclic structures. for key, value in sorted_list.items(): if type(value) is dict: sorted_list[key] = sort_recursive(value) return sorted_list
class JobTracker(object): def __init__(self, name, trackers, cls=Monitor): self.name = name self.count = 0 self.trackers = OrderedDict() for n, getter in trackers: mon = cls("%s: %s" % (name, n)) self.trackers[n] = (mon, getter) def __getattr__(self, attr): if attr in self.trackers: return self.trackers[attr][0] else: raise AttributeError def record(self, agent, job): for mon, getter in self.trackers.itervalues(): mon.observe(getter(agent, job)) self.count += 1 def report(self): for mon, _ in self.trackers.itervalues(): print self._report(mon) def _report(self, tally): s = "mean %s: %.2f (%.2f)" if tally.count(): vars = (tally.name, tally.mean(), math.sqrt(tally.var())) else: vars = (tally.name, 0, 0) return s % vars
def _flatten_dictionary(self, params, parent=None): """ Flatten a hierarchical dictionary into a simple dictionary. >>> self._flatten_dictionary({ "test": { "foo": 12, "bar": "hello", }, "baz": False }) { "test[foo]": 12, "test[bar]": hello, "baz": False } """ data = OrderedDict() for key, val in params.items(): full_key = parent + "[" + key + "]" if parent else key if isinstance(val, dict): data.update(self._flatten_dictionary(val, full_key)) else: data[full_key] = val return data
def get_stats(self, dim, prefix=''): """Get various 1d statistics for the datatable. """ def add_stat(stats, key, val): stats[prefix+key] = val def get_stat(stats, key): return stats[prefix+key] #print dim #print self.num_cells #print self.data p = self.get_points(dim) s = OrderedDict() add_stat(s, 'num_cells', self.num_cells) add_stat(s, 'min', np.min(p)) add_stat(s, 'max', np.max(p)) add_stat(s, 'average', np.average(p)) add_stat(s, 'std', np.std(p)) add_stat(s, 'median', np.median(p)) add_stat(s, 'gaussian_fit', self.gaussian_pdf_compare( dim, 100, get_stat(s, 'average'), get_stat(s, 'std'))) keys = s.keys() vals = np.array([s.values()]) ret = DataTable(vals, keys, name=self.sub_name('stats for %s' % dim)) ret.properties['original_table'] = self return ret
def __init__(self, method='POST', action='', submitLabel='Submit', attrs=None): '''Initialize a new, empty form instance. @param method: the form method attribute (i.e. "GET" or "POST") @type method: str @param action: the form action attribute. This will often be the empty @type action: str string ("self-submission"), for simplified validation and error message display. @param submitLabel: the text of the final submit button. @type submitLabel: str @param attrs: a dictionary of attribute names and values for the form element (i.e. the "class" or "id" attributes). The I{method} and I{action} attributes can be set here, or through their parameters (which are provided simply as a convenience; if set on both, the values here take precedence. @type attrs: dict ''' OrderedDict.__init__(self) self.schema = schema.Schema() self.schema.fields = self self.attrs = {'method':method, 'action':action} if attrs: self.attrs.update(attrs) self.submitLabel = submitLabel
def __init__(self, bgpid, asnum, holdtime=180, version=4): self.version = version self.holdtime = holdtime self.asnum = asnum self.bgpid = bgpid self.caps = OD() self.params = OD()
def __init__(self, name): self.name = name self.sensitivities = OrderedDict() self.baseClassifications = OrderedDict() self.rawToTrans = OrderedDict() self.transToRaw = OrderedDict() self.groups = OrderedDict()
def startElement(self, name, attrs): self.path.append((name, attrs or None)) if len(self.path) > self.item_depth: self.stack.append((self.item, self.data)) attrs = OrderedDict((self.attr_prefix+key, yaml_eval(value)) for (key, value) in attrs.items()) self.item = self.xml_attribs and attrs or None self.data = None
def merge_hists(hists_d, merge_groups, order=PhysicsProcess.desired_plot_order): """ Merges the dictionary of input histograms according to the merge rules, which are specified as a key-value dictionary, where the key is the target and value a list of (regular) expressions to merge under the key. For example, { "WJets": ["W[1-4]Jets_.*"], "tchan": ["T_t_ToLeptons", "Tbar_t_ToLeptons"], } will perform the corresponding merges. The values of the merge dict are the keys of the input histogram dict. returns - a dictionary with the merged histograms. Optionally you can specify a list with the desired order of the keys. """ for v in hists_d.values(): if not isinstance(v, Hist) and not isinstance(v, ROOT.TH1I) and not isinstance(v, ROOT.TH1F) and not isinstance(v, Hist2D) and not isinstance(v, ROOT.TH2I) and not isinstance(v, ROOT.TH2F): raise ValueError("First argument(hists_d) must be a dict of Histograms, but found %s" % v) out_d = OrderedDict() logger.debug("merge_hists: input histograms %s" % str(hists_d)) for merge_name, items in merge_groups.items(): logger.debug("Merging %s to %s" % (items, merge_name)) matching_keys = [] for item in items: t = filter(lambda x: re.match(item + "$", x), hists_d.keys()) matching_keys += t logger.debug("Matched %s to %s" % (str(t), item)) if len(matching_keys)==0: continue logger.debug("Merging matched %s" % str(matching_keys)) hist = hists_d[matching_keys[0]].Clone() for item in matching_keys[1:]: hist.Add(hists_d[item]) out_d[merge_name] = hist out_d[merge_name].SetTitle(merge_name) out_d[merge_name].SetName(merge_name) out_d_ordered = OrderedDict() for elem in order: try: out_d_ordered[elem] = out_d.pop(elem) if hasattr(PhysicsProcess, merge_name): if type(getattr(PhysicsProcess, merge_name)) is dict: #take nominal name if multiple options out_d_ordered[elem].SetTitle(getattr(PhysicsProcess, merge_name)["nominal"].pretty_name) else: #regular out_d_ordered[elem].SetTitle(getattr(PhysicsProcess, merge_name).pretty_name) except KeyError: #We don't care if there was an element in the order which was not present in the merge output pass #Put anything that was not in the order list simply to the end for k, v in out_d.items(): out_d_ordered[k] = v return out_d_ordered
class Domain: def __init__(self, name): self.name = name self.sensitivities = OrderedDict() self.baseClassifications = OrderedDict() self.rawToTrans = OrderedDict() self.transToRaw = OrderedDict() self.groups = OrderedDict() def addSensitivity(self, sensitivity, level): if self.sensitivities.has_key(sensitivity) == 0: self.sensitivities[sensitivity] = level def findSensitivityByName(self, sensitivity): for k, v in self.sensitivities.iteritems(): # print k,v if v == sensitivity: return sensitivity return None def addGroup(self, groupName): group = Group(groupName) self.groups[groupName] = group return group def addBaseClassification(self, bcName): baseClassification = BaseClassification(bcName) self.baseClassifications[bcName] = baseClassification return baseClassification def str(self): str = "" str = str + "Domain name: %s\n" % (self.name) str = str + "Sensitivities:" for key in self.sensitivities.keys(): str = str + "%s %s\n" % (key, self.sensitivities[key]) str = str + "Base classifications:\n" for key in self.baseClassifications.keys(): str = str + str(self.baseClassifications[key]) str = str + "Groups:\n" for key in self.groups.keys(): str = str + str(self.groups[key]) # str = str + "Raw to translated:" # for key in self.rawToTrans.keys(): # str = str + "%s %s" % (key, self.rawToTrans[key]) # str = str + "Translated to raw:" # for key in self.transToRaw.keys(): # str = str + "%s %s" % (key, self.transToRaw[key]) return str
class Group: def __init__(self, name): self.name = name self.wordDict = OrderedDict() self.whitespace = "" self.join = "" self.default = "" self.prefixes = [] self.suffixes = [] def __str__(self): str = "" str = str + "\nName: %s\n" % (self.name) str = str + "Whitespace: %s\n" % (self.whitespace) str = str + "Join: %s\n" % (self.join) str = str + "Default: %s\n" % (self.default) str = str + "Prefixes: %s\n" % (self.prefixes) str = str + "Suffixes: %s\n" % (self.suffixes) str = str + "Words:\n" for key in self.wordDict.keys(): str = str + "%s %s\n" % (key, self.wordDict[key]) return str def set_used_codeword(self, codeword): self.wordDict[codeword].used = True def count_used_codewords(self): count = 0 for codeword in self.wordDict.values(): if codeword.used: count += 1 return count def get_used_codeword(self): def make_iter(self=self): for codeword in self.wordDict.values(): if codeword.used: yield codeword return make_iter() def get_unused_codeword(self): def make_iter(self=self): for codeword in self.wordDict.values(): if not codeword.used: yield codeword return make_iter()
def separate_tables_by_time(self, tables, time_col): return [tables], [range(len(tables))] time_to_tables = OrderedDict() time_to_idx = OrderedDict() for i, time in enumerate(time_col): time_to_tables.setdefault(time, []).append(tables[i]) time_to_idx.setdefault(time, []).append(i) return time_to_tables.values(), time_to_idx.values()
def from_bytes(cls, bytes): self = cls() d = {} idx = 0 for kind in ('withdraw', 'pathattr'): plen, = struct.unpack_from('!H', bytes, idx) idx += 2 d[kind] = bytes[idx:idx+plen] idx += plen self.nlri = nlri.parse(bytes[idx:]) self.withdraw = nlri.parse(d['withdraw']) self.pathattr = OD() idx = 0 bytes = d['pathattr'] while idx < len(bytes): used, pattr = pathattr.decode(bytes, idx) idx += used self.pathattr[pattr.type] = pattr return self
def __init__(self): super(MainMenu, self).__init__("The Williams' Search Task") self.screen = director.get_window_size() ratio = self.screen[1] / self.screen[0] self.select_sound = StaticSource(pyglet.resource.media('move.wav')) self.font_title['font_name'] = 'Pipe Dream' self.font_title['font_size'] = self.screen[0] / 18 self.font_title['color'] = (255, 255, 255, 255) self.font_item['font_name'] = 'Pipe Dream', self.font_item['color'] = (255, 255, 255, 255) self.font_item['font_size'] = self.screen[1] / 16 * ratio self.font_item_selected['font_name'] = 'Pipe Dream' self.font_item_selected['color'] = (0, 0, 255, 255) self.font_item_selected['font_size'] = self.screen[1] / 16 * ratio self.menu_anchor_y = 'center' self.menu_anchor_x = 'center' self.items = OrderedDict() self.items['mode'] = MultipleMenuItem('Mode: ', self.on_mode, director.settings['modes'], director.settings['modes'].index(director.settings['mode'])) self.items['player'] = MultipleMenuItem('Player: ', self.on_player, director.settings['players'], director.settings['players'].index(director.settings['player'])) self.items['start'] = MenuItem('Start', self.on_start) self.items['options'] = MenuItem('Options', self.on_options) self.items['quit'] = MenuItem('Quit', self.on_quit) self.create_menu(self.items.values(), zoom_in(), zoom_out())
def __init__(self, **kw): self.fields = OrderedDict(self.__class__.fields) for k,v in kw.items(): if callable(v): self.fields[k] = v(self.fields[k]) else: self.fields[k] = v
def __init__(self, name, trackers, cls=Monitor): self.name = name self.count = 0 self.trackers = OrderedDict() for n, getter in trackers: mon = cls("%s: %s" % (name, n)) self.trackers[n] = (mon, getter)
def __init__(self): super(OptionsMenu, self).__init__('Options') self.screen = director.get_window_size() ratio = self.screen[1] / self.screen[0] self.select_sound = StaticSource(pyglet.resource.media('move.wav')) self.font_title['font_name'] = 'Pipe Dream' self.font_title['font_size'] = self.screen[0] / 18 self.font_title['color'] = (255, 255, 255, 255) self.font_item['font_name'] = 'Pipe Dream', self.font_item['color'] = (255, 255, 255, 255) self.font_item['font_size'] = self.screen[1] / 16 * ratio self.font_item_selected['font_name'] = 'Pipe Dream' self.font_item_selected['color'] = (0, 0, 255, 255) self.font_item_selected['font_size'] = self.screen[1] / 16 * ratio self.items = OrderedDict() self.items['seed'] = EntryMenuItem('Random Seed:', self.on_seed, director.settings['seed']) self.items['fps'] = ToggleMenuItem('Show FPS:', self.on_show_fps, director.show_FPS) self.items['fullscreen'] = ToggleMenuItem('Fullscreen:', self.on_fullscreen, director.window.fullscreen) if eyetracking: self.items['eyetracker'] = ToggleMenuItem("EyeTracker:", self.on_eyetracker, director.settings['eyetracker']) self.items['eyetracker_ip'] = EntryMenuItem('EyeTracker IP:', self.on_eyetracker_ip, director.settings['eyetracker_ip']) self.items['eyetracker_in_port'] = EntryMenuItem('EyeTracker In Port:', self.on_eyetracker_in_port, director.settings['eyetracker_in_port']) self.items['eyetracker_out_port'] = EntryMenuItem('EyeTracker Out Port:', self.on_eyetracker_out_port, director.settings['eyetracker_out_port']) self.set_eyetracker_extras(director.settings['eyetracker']) self.create_menu(self.items.values(), zoom_in(), zoom_out())
def __init__(self): super(OptionsMenu, self).__init__('Options') self.screen = director.get_window_size() ratio = self.screen[1] / self.screen[0] self.font_title['font_size'] = self.screen[0] / 18 self.font_title['color'] = (255, 255, 255, 255) self.font_item['color'] = (255, 255, 255, 255) self.font_item['font_size'] = self.screen[1] / 16 * ratio self.font_item_selected['color'] = (0, 0, 255, 255) self.font_item_selected['font_size'] = self.screen[1] / 16 * ratio self.items = OrderedDict() self.speed_opts = ['SLOW','FAST'] self.level_opts = ['NONE','WEAK','MEDIUM','STRONG'] self.eye_opts = ['NONE','RIGHT','LEFT'] self.points_opts = ['2','5','9','13'] self.items['overlay'] = ToggleMenuItem('Debug Overlay:', self.on_overlay, director.settings['overlay']) self.items['eyetracker'] = ToggleMenuItem('EyeTracker:', self.on_eyetracker, director.settings['eyetracker']) self.items['eyetracker_ip'] = EntryMenuItem('EyeTracker IP:', self.on_eyetracker_ip, director.settings['eyetracker_ip']) self.items['eyetracker_in_port'] = EntryMenuItem('EyeTracker In Port:', self.on_eyetracker_in_port, director.settings['eyetracker_in_port']) self.items['eyetracker_out_port'] = EntryMenuItem('EyeTracker Out Port:', self.on_eyetracker_out_port, director.settings['eyetracker_out_port']) self.items['calibration_points'] = MultipleMenuItem('Calibration Points:', self.on_cal_points, self.points_opts, self.points_opts.index(str(director.settings['calibration_points']))) self.items['calibration_eye'] = MultipleMenuItem('Calibration Eye:', self.on_cal_eye, self.eye_opts, director.settings['calibration_eye']) self.items['calibration_level'] = MultipleMenuItem('Calibration Check Level:', self.on_cal_level, self.level_opts, director.settings['calibration_level']) self.items['calibration_speed'] = MultipleMenuItem('Calibration Speed:', self.on_cal_speed, self.speed_opts, director.settings['calibration_speed']) self.items['calibration_auto'] = ToggleMenuItem('Calibration Auto-accept:', self.on_cal_auto, director.settings['calibration_auto']) self.items['calibration_wait'] = ToggleMenuItem('Calibration Wait For Good:', self.on_cal_wait, director.settings['calibration_wait']) self.items['calibration_random'] = ToggleMenuItem('Calibration Randomize:', self.on_cal_random, director.settings['calibration_random']) self.create_menu(self.items.values(), zoom_in(), zoom_out())
class Node(object): def __init__(self, name): self.name = name self.attributes = OrderedDict() self.children = [] self.parent = None def __eq__(self, other): return (type(other) == type(self) and self.name == other.name and dict(self.attributes) == dict(other.attributes) and self.children == other.children) def __ne__(self, other): return not self == other __gt__ = __lt__ = __ge__ = __le__ = not_implemented __hash__ = None def to_string(self): attribute_string = "".join(' %s="%s"' % (key, value) for key, value in self.attributes.items()) name = self.name if not self.children: return '<%s />' % (name + attribute_string) children_string = "".join(node.to_string() for node in self.children) return '<%s>%s</%s>' % (name + attribute_string, children_string, self.name) # Hmmm... __repr__ = to_string
def cheat(url, mappings): """ Modifies BitTorrent tracker URLs, faking the amount of data uploaded. All other URLs should pass through unimpeded. """ parsed = urlparse.urlparse(url) if "=" not in parsed.query: return url query = OrderedDict([i.split("=") for i in parsed.query.split("&")]) if "uploaded" not in query or query["uploaded"] == "0": return url if parsed.hostname in mappings: multiple = mappings[parsed.hostname] else: if "default" in mappings: multiple = mappings["default"] else: return url # Don't bother munging the URL if the upload amount isn't going # to change. if multiple == 1: return url fakeupload = int(query["uploaded"]) logger = logging.getLogger("cheatbt") logger.debug("%s: %d -> %d" % (parsed.hostname, fakeupload, fakeupload * multiple)) fakeupload = fakeupload * multiple query["uploaded"] = str(fakeupload) new_query = "" for k in query.keys(): new_query += k + "=" + query[k] + "&" new_query = new_query[:-1] # Remove trailing "&" # <scheme>://<netloc>/<path>;<params>?<query>#<fragment> new_url = urlparse.urlunparse((parsed.scheme, parsed.netloc, parsed.path, parsed.params, new_query, parsed.fragment)) return new_url
def __init__(self, name): self.name = name self.wordDict = OrderedDict() self.whitespace = "" self.join = "" self.default = "" self.prefixes = [] self.suffixes = []
def keepSTIntoDict(st_list): global st_dict global counter global ex_counter global curr_ex_type global fl_st_counter # ordered dictionary for the stack trace body st_dict = OrderedDict([]) # initialise the variables counter = 0 ex_counter = 0 curr_ex_type = "type" # for each trace from the stack trace # check whether it begins with: 1) at, 2) caused by, 3) ...# more, 4) empty line for i, t in enumerate(st_list): if re.search("^at\s", st_list[i]): if processAtTrace(st_list, i) == True: st_dict.setdefault(curr_ex_type, []).append(keepOnlyMethod(st_list[i])) else: break elif re.search("Caused\s", st_list[i]) and ex_counter > 0: if (i + 1) < len(st_list): if checkEndPointExistence(st_list, i + 1, "\.\.\.\s[\d]+\smore") and checkExLevelChain(): newChainedExLevel(st_list, i) else: # increase the counter for the filtered stack traces (thrown exception level chain) fl_st_counter = fl_st_counter + 1 break elif re.search("\.\.\.\s[\d]+\smore", st_list[i]) and ex_counter > 0: # remove last method from values of the current exception type st_dict.setdefault(curr_ex_type, []).remove(keepOnlyMethod(st_list[i - 1])) # check next trace if contAfterMoreTrace(st_list, i) == False: break # search for empty lines elif re.search("^\s*$", st_list[i]): break # in the beginning of the stack trace we may have trash, # but we want to continue to the next lines (i.e. next traces) else: continue # return the current stack trace into dictionary return st_dict
def on_enter(self): super(ParticipantMenu, self).on_enter() self.items = OrderedDict() self.items['firstname'] = BetterEntryMenuItem('First Name:', self.on_info_change, "", validator=lambda x: x.isalpha()) self.items['lastname'] = BetterEntryMenuItem('Last Name:', self.on_info_change, "", validator=lambda x: x.isalpha()) self.items['rin'] = BetterEntryMenuItem('RIN:', self.on_info_change, "", max_length=9, validator=lambda x: unicode(x).isnumeric()) self.items['start'] = MenuItem('Start', self.on_start) self.create_menu(self.items.values(), zoom_in(), zoom_out()) self.items['start'].visible = False
def __init__(self, *pathattr, **kw): self.nlri = [] self.withdraw = [] self.pathattr = OD() for n in kw.pop('nlri', []): if isinstance(n, str): n = nlri.ipv4(n) self.nlri.append(n) for w in kw.pop('withdraw', []): if isinstance(w, str): w = nlri.ipv4(w) self.withdraw.append(w) self.pathattr = OD() for p in pathattr: self.pathattr[p.type] = p
def __init__(self, size=100, load=1.0, regions=(4,4), coords=(100,100), run_times=6, arrival_dist = dists.expon, resource_sizes = dists.gamma(100, 10), job_sizes = dists.gamma(20, 4), job_durations = dists.gamma(100, 10), service_means = dists.normal(0.1), service_dist = dists.shaped_gamma(0.1), latency_means = dists.normal(0.1), latency_dist = dists.shaped_gamma(0.1), regional_latency = dists.gamma(0.2, 0.1), global_latency = dists.gamma(0.4, 0.1) ): # calculated results self.runtime = run_times * job_durations.mean total_capacity = size * resource_sizes.mean; max_jobs = total_capacity / float(job_sizes.mean); arrival_mean = job_durations.mean / float(max_jobs); self.inter_arrival_time = arrival_dist(arrival_mean / load) self.job_sizes = job_sizes self.job_durations = job_durations # store for stats self.size = size self.load = load # generate network self.graph = network.Network( coords, regions, latency_means, regional_latency, global_latency, latency_dist) # generate nodes, but no topology network.generate_nodes(self.graph, size) self.service_dist = service_dist self.service_means = service_means for node in self.graph.nodes_iter(): node.server = Server(node, service_dist(service_means())) node.resource = GridResource(node, int(resource_sizes())) self.mons = OrderedDict() self.mons["grid_util"] = Monitor("grid_util") self.mons["server_util"] = Monitor("server_util") self.mons["server_queue"] = Monitor("server_queue")
class MainMenu(BetterMenu): def __init__(self): super(MainMenu, self).__init__("Snake") self.screen = director.get_window_size() ratio = self.screen[1] / self.screen[0] self.select_sound = StaticSource(pyglet.resource.media('move.wav')) self.font_title['font_name'] = 'Pipe Dream' self.font_title['font_size'] = self.screen[0] / 18 self.font_title['color'] = (255, 255, 255, 255) self.font_item['font_name'] = 'Pipe Dream', self.font_item['color'] = (255, 255, 255, 255) self.font_item['font_size'] = self.screen[1] / 16 * ratio self.font_item_selected['font_name'] = 'Pipe Dream' self.font_item_selected['color'] = (0, 0, 255, 255) self.font_item_selected['font_size'] = self.screen[1] / 16 * ratio self.menu_anchor_y = 'center' self.menu_anchor_x = 'center' self.items = OrderedDict() #self.items['mode'] = MultipleMenuItem('Mode: ', self.on_mode, director.settings['modes'], director.settings['modes'].index(director.settings['mode'])) self.items['player'] = MultipleMenuItem('Player: ', self.on_player, director.settings['players'], director.settings['players'].index(director.settings['player'])) # self.items['tutorial'] = MenuItem('Tutorial', self.on_tutorial) self.items['start'] = MenuItem('Start', self.on_start) self.items['options'] = MenuItem('Options', self.on_options) self.items['quit'] = MenuItem('Quit', self.on_quit) self.create_menu(self.items.values(), zoom_in(), zoom_out()) def on_player(self, player): director.settings['player'] = director.settings['players'][player] def on_mode(self, mode): director.settings['mode'] = director.settings['modes'][mode] def on_tutorial(self): director.push(SplitColsTransition(Scene(TutorialLayer()))) def on_options(self): self.parent.switch_to(1) def on_start(self): filebase = "Snake_%s" % (getDateTimeStamp()) director.settings['filebase'] = filebase director.scene.dispatch_event('start_task') def on_quit(self): reactor.callFromThread(reactor.stop)
class Packet(): fields = OrderedDict([ ]) def __init__(self, **kw): self.fields = OrderedDict(self.__class__.fields) for k,v in kw.items(): if callable(v): self.fields[k] = v(self.fields[k]) else: self.fields[k] = v def __str__(self): return "".join(map(str, self.fields.values()))
def __init__(self): self.Identifiers = OrderedDict() self.Parameters = OrderedDict() self.Variables = OrderedDict() self.Equations = OrderedDict() self.DifferentialEquations = OrderedDict() self.Functions = OrderedDict() self.InitialValues = OrderedDict() self.Bounds = {} #---------Some builtin functions------- self.Functions['exp'] = [('math.exp')] self.Functions['log'] = [('math.log')] self.Functions['log10'] = [('math.log10')] self.Functions['pow'] = [('math.pow')] self.Functions['sqrt'] = [('math.sqrt')] self.Functions['sin'] = [('math.sin')] self.Functions['cos'] = [('math.cos')] self.Functions['tan'] = [('math.tan')] self.Functions['hypot'] = [('math.hypot')] self.Functions['asin'] = [('math.asin')] self.Functions['acos'] = [('math.acos')] self.Functions['atan'] = [('math.atan')] self.Functions['radians'] = [('math.radians')] self.Functions['degrees'] = [('math.degrees')]
def _flatten_errors(self, params, parent=None): """ A modified version of the flatten_dictionary method above used to coerce the structure holding errors returned by Braintree into a flattened dictionary where the keys are the names of the fields and the values the error messages, which can be directly used to set the field errors on the Django form object for display in templates. """ data = OrderedDict() for key, val in params.items(): full_key = parent + "[" + key + "]" if parent else key if full_key.endswith("[errors]"): full_key = full_key[:-len("[errors]")] if isinstance(val, dict): data.update(self._flatten_errors(val, full_key)) elif key == "errors": for error in val: data[full_key + "[" + error["attribute"] + "]"] = [error["message"]] else: data[full_key] = [val] return data
class SMBv2Header(Packet): fields = OrderedDict([ ("Proto", "\xfe\x53\x4d\x42"), ("Len", "\x40\x00"), ("CreditCharge", "\x00\x00"), ("NTStatus", "\x00\x00\x00\x00"), ("Cmd", "\x00\x00"), ("Credits", "\x01\x00"), ("Flags", "\x01\x00\x00\x00"), ("NextCmd", "\x00\x00\x00\x00"), ("MessageId", "\x00\x00\x00\x00\x00\x00\x00\x00"), ("PID", "\xff\xfe\x00\x00"), ("TID", "\x00\x00\x00\x00"), ("SessionID", "\x00\x00\x00\x00\x00\x00\x00\x00"), ("Signature", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), ])
class DHCPInformACK(Packet): fields = OrderedDict([ ("MessType", "\x02"), ("HdwType", "\x01"), ("HdwLen", "\x06"), ("Hops", "\x00"), ("Tid", "\x22\x1b\xe0\x1a"), ("ElapsedSec", "\x00\x00"), ("BootpFlags", "\x00\x00"), ("ActualClientIP", "\x00\x00\x00\x00"), ("GiveClientIP", "\x00\x00\x00\x00"), ("NextServerIP", "\x00\x00\x00\x00"), ("RelayAgentIP", "\x00\x00\x00\x00"), ("ClientMac", "\xb8\x76\x3f\xbd\xdd\x05"), ("ClientMacPadding", "\x00" * 10), ("ServerHostname", "\x00" * 64), ("BootFileName", "\x00" * 128), ("MagicCookie", "\x63\x82\x53\x63"), ("Op53", "\x35\x01\x05"), #Msgtype(ACK) ("Op54", "\x36"), ("Op54Len", "\x04"), ("Op54Str", ""), #DHCP Server ("Op1", "\x01"), ("Op1Len", "\x04"), ("Op1Str", ""), #Netmask ("Op15", "\x0f"), ("Op15Len", "\x0e"), ("Op15Str", DNSNAME), #DNS Name ("Op3", "\x03"), ("Op3Len", "\x04"), ("Op3Str", ""), #Router ("Op6", "\x06"), ("Op6Len", "\x08"), ("Op6Str", ""), #DNS Servers ("Op252", "\xfc"), ("Op252Len", "\x04"), ("Op252Str", WPADSRV), #Wpad Server. ("Op255", "\xff"), ]) def calculate(self): self.fields["Op54Str"] = inet_aton(DHCPSERVER) self.fields["Op1Str"] = inet_aton(NETMASK) self.fields["Op3Str"] = inet_aton(ROUTERIP) self.fields["Op6Str"] = inet_aton(DNSIP) + inet_aton(DNSIP2) self.fields["Op15Len"] = struct.pack(">b", len(DNSNAME)) self.fields["Op252Len"] = struct.pack(">b", len(WPADSRV))
class SMBTransRAPData(Packet): fields = OrderedDict([ ("Wordcount", "\x10"), ("TotalParamCount", "\x00\x00"), ("TotalDataCount","\x00\x00" ), ("MaxParamCount", "\xff\xff"), ("MaxDataCount","\xff\xff"), ("MaxSetupCount", "\x00"), ("Reserved","\x00\x00"), ("Flags", "\x00"), ("Timeout","\x00\x00\x00\x00"), ("Reserved1","\x00\x00"), ("ParamCount","\x00\x00"), ("ParamOffset", "\x5c\x00"), ("DataCount", "\x00\x00"), ("DataOffset", "\x54\x00"), ("SetupCount", "\x02"), ("Reserved2", "\x00"), ("PeekNamedPipe", "\x23\x00"), ("FID", "\x00\x00"), ("Bcc", "\x47\x00"), ("Terminator", "\x00"), ("PipeName", "\\PIPE\\"), ("PipeTerminator","\x00\x00"), ("Data", ""), ]) def calculate(self): #Padding if len(str(self.fields["Data"]))%2==0: self.fields["PipeTerminator"] = "\x00\x00\x00\x00" else: self.fields["PipeTerminator"] = "\x00\x00\x00" ##Convert Path to Unicode first before any Len calc. self.fields["PipeName"] = self.fields["PipeName"].encode('utf-16le') ##Data Len self.fields["TotalParamCount"] = struct.pack("<i", len(str(self.fields["Data"])))[:2] self.fields["ParamCount"] = struct.pack("<i", len(str(self.fields["Data"])))[:2] ##Packet len FindRAPOffset = str(self.fields["Wordcount"])+str(self.fields["TotalParamCount"])+str(self.fields["TotalDataCount"])+str(self.fields["MaxParamCount"])+str(self.fields["MaxDataCount"])+str(self.fields["MaxSetupCount"])+str(self.fields["Reserved"])+str(self.fields["Flags"])+str(self.fields["Timeout"])+str(self.fields["Reserved1"])+str(self.fields["ParamCount"])+str(self.fields["ParamOffset"])+str(self.fields["DataCount"])+str(self.fields["DataOffset"])+str(self.fields["SetupCount"])+str(self.fields["Reserved2"])+str(self.fields["PeekNamedPipe"])+str(self.fields["FID"])+str(self.fields["Bcc"])+str(self.fields["Terminator"])+str(self.fields["PipeName"])+str(self.fields["PipeTerminator"]) self.fields["ParamOffset"] = struct.pack("<i", len(FindRAPOffset)+32)[:2] ##Bcc Buff Len BccComplete = str(self.fields["Terminator"])+str(self.fields["PipeName"])+str(self.fields["PipeTerminator"])+str(self.fields["Data"]) self.fields["Bcc"] = struct.pack("<i", len(BccComplete))[:2]
class LDAPSearchSupportedMechanismsPacket(Packet): fields = OrderedDict([ ("ParserHeadASNID", "\x30"), ("ParserHeadASNLenOfLen", "\x84"), ("ParserHeadASNLen", "\x00\x00\x00\x60"), #96 ("MessageIDASNID", "\x02"), ("MessageIDASNLen", "\x01"), ("MessageIDASNStr", "\x02"), ("OpHeadASNID", "\x64"), ("OpHeadASNIDLenOfLen", "\x84"), ("OpHeadASNIDLen", "\x00\x00\x00\x57"), #87 ("ObjectName", "\x04\x00"), ("SearchAttribASNID", "\x30"), ("SearchAttribASNLenOfLen", "\x84"), ("SearchAttribASNLen", "\x00\x00\x00\x4f"), #79 ("SearchAttribASNID1", "\x30"), ("SearchAttribASN1LenOfLen", "\x84"), ("SearchAttribASN1Len", "\x00\x00\x00\x49"), #73 ("SearchAttribASN2ID", "\x04"), ("SearchAttribASN2Len", "\x17"), #23 ("SearchAttribASN2Str", "supportedSASLMechanisms"), ("SearchAttribASN3ID", "\x31"), ("SearchAttribASN3LenOfLen", "\x84"), ("SearchAttribASN3Len", "\x00\x00\x00\x2a"), #42 ("SearchAttrib1ASNID", "\x04"), ("SearchAttrib1ASNLen", "\x06"), #6 ("SearchAttrib1ASNStr", "GSSAPI"), ("SearchAttrib2ASNID", "\x04"), ("SearchAttrib2ASNLen", "\x0a"), #10 ("SearchAttrib2ASNStr", "GSS-SPNEGO"), ("SearchAttrib3ASNID", "\x04"), ("SearchAttrib3ASNLen", "\x08"), #8 ("SearchAttrib3ASNStr", "EXTERNAL"), ("SearchAttrib4ASNID", "\x04"), ("SearchAttrib4ASNLen", "\x0a"), #10 ("SearchAttrib4ASNStr", "DIGEST-MD5"), ("SearchDoneASNID", "\x30"), ("SearchDoneASNLenOfLen", "\x84"), ("SearchDoneASNLen", "\x00\x00\x00\x10"), #16 ("MessageIDASN2ID", "\x02"), ("MessageIDASN2Len", "\x01"), ("MessageIDASN2Str", "\x02"), ("SearchDoneStr", "\x65\x84\x00\x00\x00\x07\x0a\x01\x00\x04\x00\x04\x00"), ## No need to calculate anything this time, this packet is generic. ])
class WPADScript(Packet): fields = OrderedDict([ ("Code", "HTTP/1.1 200 OK\r\n"), ("ServerType", "Server: Microsoft-IIS/6.0\r\n"), ("Date", "Date: Wed, 12 Sep 2012 13:06:55 GMT\r\n"), ("Type", "Content-Type: application/x-ns-proxy-autoconfig\r\n"), ("PoweredBy", "X-Powered-By: ASP.NET\r\n"), ("ContentLen", "Content-Length: "), ("ActualLen", "76"), ("CRLF", "\r\n\r\n"), ("Payload", "function FindProxyForURL(url, host){return 'PROXY wpadwpadwpad:3141; DIRECT';}" ), ]) def calculate(self): self.fields["ActualLen"] = len(str(self.fields["Payload"]))
class SMBRelayNegoAns(Packet): fields = OrderedDict([ ("Wordcount", "\x11"), ("Dialect", ""), ("Securitymode", "\x03"), ("MaxMpx", "\x32\x00"), ("MaxVc", "\x01\x00"), ("MaxBuffSize", "\x04\x41\x00\x00"), ("MaxRawBuff", "\x00\x00\x01\x00"), ("SessionKey", "\x00\x00\x00\x00"), ("Capabilities", "\xfd\xf3\x01\x80"), ("SystemTime", "\x84\xd6\xfb\xa3\x01\x35\xcd\x01"), ("SrvTimeZone", "\xf0\x00"), ("KeyLen", "\x00"), ("Bcc", "\x10\x00"), ("Guid", os.urandom(16)), ])
class SMBDCESVCCTLOpenManagerW(Packet): fields = OrderedDict([ ("MachineNameRefID", "\xb5\x97\xb9\xbc"), ("MaxCount", "\x0f\x00\x00\x00"), ("Offset", "\x00\x00\x00\x00"), ("ActualCount", "\x0f\x00\x00\x00"), ("MachineName", ""), ("MachineNameNull", "\x00\x00"), ("DbPointer", "\x00\x00\x00\x00"), ("AccessMask", "\x3f\x00\x0f\x00"), ]) def calculate(self): ## Convert to UTF-16LE self.fields["MaxCount"] = struct.pack("<i",len(str(self.fields["MachineName"]))+1) self.fields["ActualCount"] = struct.pack("<i",len(str(self.fields["MachineName"]))+1) self.fields["MachineName"] = self.fields["MachineName"].encode('utf-16le')
class ARPWhoHas(Packet): fields = OrderedDict([ ("HwType", "\x00\x01"), ("ProtoType", "\x08\x00"), #IP ("MacLen", "\x06"), ("IPLen", "\x04"), ("OpCode", "\x00\x01"), ("SenderMac", ""), ("SenderIP", "\x00\xff\x53\x4d"), ("DstMac", "\x00\x00\x00\x00\x00\x00"), ("DstIP", "\x00\x00\x00\x00"), ]) def calculate(self): self.fields["DstIP"] = inet_aton( self.fields["DstIP"]).decode('latin-1') self.fields["SenderIP"] = inet_aton(OURIP).decode('latin-1')
class SMBDCESVCCTLOpenManagerW(Packet): fields = OrderedDict([ ("MachineNameRefID", "\xb5\x97\xb9\xbc"), ("MaxCount", "\x0f\x00\x00\x00"), ("Offset", "\x00\x00\x00\x00"), ("ActualCount", "\x0f\x00\x00\x00"), ("MachineName", "\\\\169.220.1.11"), ##This is not taken into consideration. ("MachineNameNull", "\x00\x00\x00\x00"), ("DbPointer", "\x00\x00\x00\x00"), ("AccessMask", "\x3f\x00\x0f\x00"), ]) def calculate(self): ## Convert to UTF-16LE self.fields["MachineName"] = self.fields["MachineName"].encode( 'utf-16le')
class ICMPRedir(Packet): fields = OrderedDict([ ("Type", "\x05"), ("OpCode", "\x01"), ("CheckSum", "\x00\x00"), ("GwAddr", ""), ("Data", ""), ]) def calculate(self): #Set the values self.fields["GwAddr"] = inet_aton(Responder_IP) # Then CheckSum this packet CheckSumCalc = str(self.fields["Type"]) + str( self.fields["OpCode"]) + str(self.fields["CheckSum"]) + str( self.fields["GwAddr"]) + str(self.fields["Data"]) self.fields["CheckSum"] = GenCheckSum(CheckSumCalc)
class SMBDCESVCCTLOpenService(Packet): fields = OrderedDict([ ("ContextHandle", ""), ("MaxCount", "\x00\x00\x00\x00"), ("Offset", "\x00\x00\x00\x00"), ("ActualCount", "\x00\x00\x00\x00"), ("ServiceName", ""), ("MachineNameNull", "\x00\x00"), ("AccessMask", "\xff\x01\x0f\x00"), ]) def calculate(self): ## Calculate first self.fields["MaxCount"] = struct.pack("<i",len(str(self.fields["ServiceName"]))+1) self.fields["ActualCount"] = struct.pack("<i",len(str(self.fields["ServiceName"]))+1) ## Then convert to UTF-16LE, yeah it's weird.. self.fields["ServiceName"] = self.fields["ServiceName"].encode('utf-16le')
def paramToDict(parameters=None): """ Split the parameters into names and values, check if these parameters are within the testable parameters and return in a dictionary. """ testableParameters = OrderedDict() """ if place in conf.parameters and not parameters: parameters = conf.parameters[place] """ parameters = re.sub( r"&(\w{1,4});", r"%s\g<1>%s" % (PARAMETER_AMP_MARKER, PARAMETER_SEMICOLON_MARKER), parameters) splitParams = parameters.split(None or '&') for element in splitParams: element = re.sub( r"%s(.+?)%s" % (PARAMETER_AMP_MARKER, PARAMETER_SEMICOLON_MARKER), r"&\g<1>;", element) parts = element.split("=") if len(parts) == 1: testableParameters[parts[0]] = '' continue if len(parts) >= 2: #print urlencode(parts[0].replace(" ", "")) #FixME:如果传过来的uri不经过encode,可能会带来问题 parameter = parts[0].replace(" ", "") if not parameter: continue """ if conf.paramDel and conf.paramDel == '\n': parts[-1] = parts[-1].rstrip() """ #if condition: testableParameters[parameter] = "=".join(parts[1:]) return testableParameters """
def next(self): header = [fType.toTuple() for fType in self.header] header.insert(0, ('DeletionFlag', 'C', 1, 0)) # read the string as a bunch of characters eg. 2s4s5s fmt = ''.join(['%ds' % fieldinfo[2] for fieldinfo in header]) self.recNo += 1 if self.recNo == self.numrec + 1: raise StopIteration fieldValues = unpack(fmt, self.bfstream.read( calcsize(fmt))) # the field values are stores as an array if fieldValues[0] != ' ': # deleted record return {} finalValues = [] for (name, typ, size, deci), value in izip(header, fieldValues): if name == 'DeletionFlag': continue try: if typ == "N" or typ == "F": value = value.replace('\0', '').lstrip() if value == '': value = 0 elif deci: value = decimal.Decimal(value) elif value == '*' * size: value = 0 # unknown!! else: value = int(value) elif typ == 'D': y, m, d = int(value[:4]), int(value[4:6]), int(value[6:8]) value = datetime.date(y, m, d) elif typ == 'L': value = (value in 'YyTt' and 'T') or (value in 'NnFf' and 'F') or '?' except: print "Exception caught with name %s type %s value %s" % ( name, typ, str(value)) raise finalValues.append(value) return OrderedDict(izip(self.fieldNames, finalValues))
class SMBSessTreeAns(Packet): fields = OrderedDict([ ("Wordcount", "\x03"), ("Command", "\x75"), ("Reserved", "\x00"), ("AndXoffset", "\x4e\x00"), ("Action", "\x01\x00"), ("Bcc", "\x25\x00"), ("NativeOs", "Windows 5.1"), ("NativeOsNull", "\x00"), ("NativeLan", "Windows 2000 LAN Manager"), ("NativeLanNull", "\x00"), ("WordcountTree", "\x03"), ("AndXCommand", "\xff"), ("Reserved1", "\x00"), ("AndxOffset", "\x00\x00"), ("OptionalSupport", "\x01\x00"), ("Bcc2", "\x08\x00"), ("Service", "A:"), ("ServiceNull", "\x00"), ("FileSystem", "NTFS"), ("FileSystemNull", "\x00"), ]) def calculate(self): ##AndxOffset CalculateCompletePacket = str(self.fields["Wordcount"]) + str( self.fields["Command"]) + str(self.fields["Reserved"]) + str( self.fields["AndXoffset"]) + str(self.fields["Action"]) + str( self.fields["Bcc"]) + str(self.fields["NativeOs"]) + str( self.fields["NativeOsNull"]) + str( self.fields["NativeLan"]) + str( self.fields["NativeLanNull"]) self.fields["AndXoffset"] = struct.pack( "<i", len(CalculateCompletePacket) + 32)[:2] #SMB Header is *always* 32. ##BCC 1 and 2 CompleteBCCLen = str(self.fields["NativeOs"]) + str( self.fields["NativeOsNull"]) + str(self.fields["NativeLan"]) + str( self.fields["NativeLanNull"]) self.fields["Bcc"] = struct.pack("<h", len(CompleteBCCLen)) CompleteBCC2Len = str(self.fields["Service"]) + str( self.fields["ServiceNull"]) + str(self.fields["FileSystem"]) + str( self.fields["FileSystemNull"]) self.fields["Bcc2"] = struct.pack("<h", len(CompleteBCC2Len))
class SMBNegoData(Packet): fields = OrderedDict([ ("wordcount", "\x00"), ("bcc", "\x54\x00"), ("separator1", "\x02"), ("dialect1", "\x50\x43\x20\x4e\x45\x54\x57\x4f\x52\x4b\x20\x50\x52\x4f\x47\x52\x41\x4d\x20\x31\x2e\x30\x00" ), ("separator2", "\x02"), ("dialect2", "\x4c\x41\x4e\x4d\x41\x4e\x31\x2e\x30\x00"), ]) def calculate(self): CalculateBCC = str(self.fields["separator1"]) + str( self.fields["dialect1"]) + str(self.fields["separator2"]) + str( self.fields["dialect2"]) self.fields["bcc"] = struct.pack("<h", len(CalculateBCC))
class SMBDCESVCCTLCreateService(Packet): fields = OrderedDict([ ("ContextHandle", ""), ("MaxCount", "\x0c\x00\x00\x00"), ("Offset", "\x00\x00\x00\x00"), ("ActualCount", "\x0c\x00\x00\x00"), ("ServiceName", "AyAGaxwLhCP"), ("MachineNameNull", "\x00\x00"), ("ReferentID", "\x9c\xfa\x9a\xc9"), ("MaxCountRefID", "\x11\x00\x00\x00"), ("OffsetID", "\x00\x00\x00\x00"), ("ActualCountRefID", "\x11\x00\x00\x00"), ("DisplayNameID", "DhhUFcsvrfJvLwRq"), ("DisplayNameIDNull", "\x00\x00\x00\x00"), ("AccessMask", "\xff\x01\x0f\x00"), ("ServerType", "\x10\x01\x00\x00"), ("ServiceStartType", "\x03\x00\x00\x00"), ("ServiceErrorCtl", "\x00\x00\x00\x00"), ("BinPathMaxCount", "\xb6\x00\x00\x00"), ("BinPathOffset", "\x00\x00\x00\x00"), ("BinPathActualCount", "\xb6\x00\x00\x00"), ("BinPathName", "%COMSPEC% /C \""), ("BinCMD", ""), ("BintoEnd", "\""), ("BinPathNameNull", "\x00\x00"), ("Nullz", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), ]) def calculate(self): BinDataLen = str(self.fields["BinPathName"])+str(self.fields["BinCMD"])+str(self.fields["BintoEnd"]) ## Calculate first self.fields["BinPathMaxCount"] = struct.pack("<i",len(BinDataLen)+1) self.fields["BinPathActualCount"] = struct.pack("<i",len(BinDataLen)+1) self.fields["MaxCount"] = struct.pack("<i",len(str(self.fields["ServiceName"]))+1) self.fields["ActualCount"] = struct.pack("<i",len(str(self.fields["ServiceName"]))+1) self.fields["MaxCountRefID"] = struct.pack("<i",len(str(self.fields["DisplayNameID"]))+1) self.fields["ActualCountRefID"] = struct.pack("<i",len(str(self.fields["DisplayNameID"]))+1) ## Then convert to UTF-16LE, yeah it's weird.. self.fields["ServiceName"] = self.fields["ServiceName"].encode('utf-16le') self.fields["DisplayNameID"] = self.fields["DisplayNameID"].encode('utf-16le') self.fields["BinPathName"] = self.fields["BinPathName"].encode('utf-16le') self.fields["BinCMD"] = self.fields["BinCMD"].encode('utf-16le') self.fields["BintoEnd"] = self.fields["BintoEnd"].encode('utf-16le')
class IIS_NTLM_Challenge_Ans(Packet): fields = OrderedDict([ ("Code", "HTTP/1.1 401 Unauthorized\r\n"), ("ServerType", "Server: Microsoft-IIS/6.0\r\n"), ("Date", "Date: Wed, 12 Sep 2012 13:06:55 GMT\r\n"), ("Type", "Content-Type: text/html\r\n"), ("WWWAuth", "WWW-Authenticate: NTLM "), ("Payload", ""), ("Payload-CRLF", "\r\n"), ("PoweredBy", "X-Powered-By: ASP.NC0CD7B7802C76736E9B26FB19BEB2D36290B9FF9A46EDDA5ET\r\n" ), ("Len", "Content-Length: 0\r\n"), ("CRLF", "\r\n"), ]) def calculate(self, payload): self.fields["Payload"] = b64encode(payload)
class IIS_Auth_Granted(Packet): fields = OrderedDict([ ("Code", "HTTP/1.1 200 OK\r\n"), ("ServerType", "Server: Microsoft-IIS/6.0\r\n"), ("Date", "Date: Wed, 12 Sep 2012 13:06:55 GMT\r\n"), ("Type", "Content-Type: text/html\r\n"), ("WWW-Auth", "WWW-Authenticate: NTLM\r\n"), ("PoweredBy", "X-Powered-By: ASP.NET\r\n"), ("ContentLen", "Content-Length: "), ("ActualLen", "76"), ("CRLF", "\r\n\r\n"), ("Payload", "<html>\n<head>\n</head>\n<body>\n<img src='file:\\\\\\\\\\\\shar\\smileyd.ico' alt='Loading' height='1' width='2'>\n</body>\n</html>\n" ), ]) def calculate(self): self.fields["ActualLen"] = len(str(self.fields["Payload"]))
def load(idx, lines): assert len(lines) > 0 assert not lines[idx].startswith('\t') assert not lines[idx].startswith(' ') filename = lines[idx].strip() idx += 1 tags = OrderedDict() while idx < len(lines) and (lines[idx].startswith('\t') or lines[idx].startswith(' ')): if ':' in lines[idx]: splitted = lines[idx].split(':') tag_key = splitted[0].strip() tag_val = splitted[1].strip() assert tag_key assert tag_val tags[tag_key] = tag_val idx += 1 return idx, DataIndexEntry(filename, tags)
class SMBNegoData(Packet): fields = OrderedDict([ ("Separator1", "\x02"), ("Dialect1", "\x50\x43\x20\x4e\x45\x54\x57\x4f\x52\x4b\x20\x50\x52\x4f\x47\x52\x41\x4d\x20\x31\x2e\x30\x00" ), ("Separator2", "\x02"), ("Dialect2", "\x4c\x41\x4e\x4d\x41\x4e\x31\x2e\x30\x00"), ("Separator3", "\x02"), ("Dialect3", "\x57\x69\x6e\x64\x6f\x77\x73\x20\x66\x6f\x72\x20\x57\x6f\x72\x6b\x67\x72\x6f\x75\x70\x73\x20\x33\x2e\x31\x61\x00" ), ("Separator4", "\x02"), ("Dialect4", "\x4c\x4d\x31\x2e\x32\x58\x30\x30\x32\x00"), ("Separator5", "\x02"), ("Dialect5", "\x4c\x41\x4e\x4d\x41\x4e\x32\x2e\x31\x00"), ("Separator6", "\x02"), ("Dialect6", "\x4e\x54\x20\x4c\x4d\x20\x30\x2e\x31\x32\x00"), ])
class SMBReadData(Packet): fields = OrderedDict([ ("Wordcount", "\x0a"), ("AndXCommand", "\xff"), ("Reserved", "\x00"), ("Andxoffset", "\x00\x00"), ("FID", "\x00\x00"), ("Offset", "\x19\x03\x00\x00"), ("MaxCountLow", "\xed\x01"), ("MinCount", "\xed\x01"), ("Hidden", "\xff\xff\xff\xff"), ("Remaining", "\x00\x00"), ("Bcc", "\x00\x00"), ("Data", ""), ]) def calculate(self): self.fields["Bcc"] = struct.pack("<h", len(str(self.fields["Data"])))
class LDAPSearchSupportedCapabilitiesPacket(Packet): fields = OrderedDict([ ("ParserHeadASNID", "\x30"), ("ParserHeadASNLenOfLen", "\x84"), ("ParserHeadASNLen", "\x00\x00\x00\x7e"), #126 ("MessageIDASNID", "\x02"), ("MessageIDASNLen", "\x01"), ("MessageIDASNStr", "\x02"), ("OpHeadASNID", "\x64"), ("OpHeadASNIDLenOfLen", "\x84"), ("OpHeadASNIDLen", "\x00\x00\x00\x75"), #117 ("ObjectName", "\x04\x00"), ("SearchAttribASNID", "\x30"), ("SearchAttribASNLenOfLen", "\x84"), ("SearchAttribASNLen", "\x00\x00\x00\x6d"), #109 ("SearchAttribASNID1", "\x30"), ("SearchAttribASN1LenOfLen", "\x84"), ("SearchAttribASN1Len", "\x00\x00\x00\x67"), #103 ("SearchAttribASN2ID", "\x04"), ("SearchAttribASN2Len", "\x15"), #21 ("SearchAttribASN2Str", "supportedCapabilities"), ("SearchAttribASN3ID", "\x31"), ("SearchAttribASN3LenOfLen", "\x84"), ("SearchAttribASN3Len", "\x00\x00\x00\x4a"), ("SearchAttrib1ASNID", "\x04"), ("SearchAttrib1ASNLen", "\x16"), #22 ("SearchAttrib1ASNStr", "1.2.840.113556.1.4.800"), ("SearchAttrib2ASNID", "\x04"), ("SearchAttrib2ASNLen", "\x17"), #23 ("SearchAttrib2ASNStr", "1.2.840.113556.1.4.1670"), ("SearchAttrib3ASNID", "\x04"), ("SearchAttrib3ASNLen", "\x17"), #23 ("SearchAttrib3ASNStr", "1.2.840.113556.1.4.1791"), ("SearchDoneASNID", "\x30"), ("SearchDoneASNLenOfLen", "\x84"), ("SearchDoneASNLen", "\x00\x00\x00\x10"), #16 ("MessageIDASN2ID", "\x02"), ("MessageIDASN2Len", "\x01"), ("MessageIDASN2Str", "\x02"), ("SearchDoneStr", "\x65\x84\x00\x00\x00\x07\x0a\x01\x00\x04\x00\x04\x00"), ## No need to calculate anything this time, this packet is generic. ])
def view(self, header_names, lines, vertical_header=None, initial_sort=[], decimal_points=3): sub_views = [] conv_lines = [] # turn lines to html for line in lines: conv_line, sub_sub_views = convert_to_html(line) conv_lines.append(conv_line) sub_views += sub_sub_views conv_sort = [] for s in initial_sort: dir = 0 if s[1] == 'asc': dir = 0 elif s[1] == 'desc': dir = 1 else: raise Exception('Sort must either be asc or desc') conv_sort.append('[%d,%d]' % (header_names.index(s[0]), dir)) conv_sort = ', '.join(conv_sort) conv_sort = '[%s]' % conv_sort data = OrderedDict() for i in xrange(len(header_names)): data[header_names[i]] = [l[i] for l in conv_lines] html = render( 'table.html', { 'vertical_header': vertical_header, 'data': data, 'id': self._get_unique_id(), 'header_names': header_names, 'lines': conv_lines, 'sort': conv_sort }) v = View(self, html, ['table.css'], ['jquery.tablesorter.js']) for sub in sub_views: v.append_view_files(sub) return v
class SMBSessionFingerData(Packet): fields = OrderedDict([ ("wordcount", "\x0c"), ("AndXCommand", "\xff"), ("reserved", "\x00"), ("andxoffset", "\x00\x00"), ("maxbuff", "\x04\x11"), ("maxmpx", "\x32\x00"), ("vcnum", "\x00\x00"), ("sessionkey", "\x00\x00\x00\x00"), ("securitybloblength", "\x4a\x00"), ("reserved2", "\x00\x00\x00\x00"), ("capabilities", "\xd4\x00\x00\xa0"), ( "bcc1", "\xb1\x00" ), #hardcoded len here and hardcoded packet below, no calculation, faster. ("Data", "\x60\x48\x06\x06\x2b\x06\x01\x05\x05\x02\xa0\x3e\x30\x3c\xa0\x0e\x30\x0c\x06\x0a\x2b\x06\x01\x04\x01\x82\x37\x02\x02\x0a\xa2\x2a\x04\x28\x4e\x54\x4c\x4d\x53\x53\x50\x00\x01\x00\x00\x00\x07\x82\x08\xa2\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05\x01\x28\x0a\x00\x00\x00\x0f\x00\x57\x00\x69\x00\x6e\x00\x64\x00\x6f\x00\x77\x00\x73\x00\x20\x00\x32\x00\x30\x00\x30\x00\x32\x00\x20\x00\x53\x00\x65\x00\x72\x00\x76\x00\x69\x00\x63\x00\x65\x00\x20\x00\x50\x00\x61\x00\x63\x00\x6b\x00\x20\x00\x33\x00\x20\x00\x32\x00\x36\x00\x30\x00\x30\x00\x00\x00\x57\x00\x69\x00\x6e\x00\x64\x00\x6f\x00\x77\x00\x73\x00\x20\x00\x32\x00\x30\x00\x30\x00\x32\x00\x20\x00\x35\x00\x2e\x00\x31\x00\x00\x00\x00\x00" ), ])
class SMBNegoData(Packet): fields = OrderedDict([ ("Separator", "\x02"), ("Dialect", "PC NETWORK PROGRAM 1.0\x00"), ("Separator1", "\x02"), ("Dialect1", "LANMAN1.0\x00"), ("Separator2", "\x02"), ("Dialect2", "Windows for Workgroups 3.1a\x00"), ("Separator3", "\x02"), ("Dialect3", "LM1.2X002\x00"), ("Separator4", "\x02"), ("Dialect4", "LANMAN2.1\x00"), ("Separator5", "\x02"), ("Dialect5", "NT LM 0.12\x00"), ("Separator6", "\x02"), ("Dialect6", "SMB 2.002\x00"), ("Separator7", "\x02"), ("Dialect7", "SMB 2.???\x00"), ("Data", ""), ])
class ServerExeFile(Packet): fields = OrderedDict([ ("Code", "HTTP/1.1 200 OK\r\n"), ("ContentType", "Content-Type: application/octet-stream\r\n"), ("LastModified", "Last-Modified: Wed, 24 Nov 2010 00:39:06 GMT\r\n"), ("AcceptRanges", "Accept-Ranges: bytes\r\n"), ("Server", "Server: Microsoft-IIS/7.5\r\n"), ("PoweredBy", "X-Powered-By: ASP.NET\r\n"), ("ContentLen", "Content-Length: "), ("ActualLen", "76"), ("Date", "\r\nDate: Thu, 24 Oct 2013 22:35:46 GMT\r\n"), ("Connection", "Connection: keep-alive\r\n"), ("X-CCC", "US\r\n"), ("X-CID", "2\r\n"), ("CRLF", "\r\n"), ("Payload", "jj"), ]) def calculate(self): self.fields["ActualLen"] = len(str(self.fields["Payload"]))
class SMBSession2Accept(Packet): fields = OrderedDict([ ("Wordcount", "\x04"), ("AndXCommand", "\xff"), ("Reserved", "\x00"), ("Andxoffset", "\xb4\x00"), ("Action", "\x00\x00"), ("SecBlobLen", "\x09\x00"), ("Bcc", "\x89\x01"), ("SSPIAccept","\xa1\x07\x30\x05\xa0\x03\x0a\x01\x00"), ("NativeOs","Windows Server 2003 3790 Service Pack 2"), ("NativeOsTerminator","\x00\x00"), ("NativeLAN", "Windows Server 2003 5.2"), ("NativeLANTerminator","\x00\x00"), ]) def calculate(self): self.fields["NativeOs"] = self.fields["NativeOs"].encode('utf-16le') self.fields["NativeLAN"] = self.fields["NativeLAN"].encode('utf-16le') BccLen = str(self.fields["SSPIAccept"])+str(self.fields["NativeOs"])+str(self.fields["NativeOsTerminator"])+str(self.fields["NativeLAN"])+str(self.fields["NativeLANTerminator"]) self.fields["Bcc"] = struct.pack("<h", len(BccLen))
class IPPacket(Packet): fields = OrderedDict([ ("VLen", "\x45"), ("DifField", "\x00"), ("Len", "\x00\x38"), ("TID", "\x25\x25"), ("Flag", "\x00"), ("FragOffset", "\x00"), ("TTL", "\x1d"), ("Cmd", "\x01"), #ICMP ("CheckSum", "\x00\x00"), ("SrcIP", ""), ("DestIP", ""), ("Data", ""), ]) def calculate(self): self.fields["TID"] = chr(randrange(256)) + chr(randrange(256)) self.fields["SrcIP"] = inet_aton(str( self.fields["SrcIP"])).decode('latin-1') self.fields["DestIP"] = inet_aton(str( self.fields["DestIP"])).decode('latin-1') # Calc Len First CalculateLen = str(self.fields["VLen"]) + str( self.fields["DifField"] ) + str(self.fields["Len"]) + str(self.fields["TID"]) + str( self.fields["Flag"]) + str(self.fields["FragOffset"]) + str( self.fields["TTL"]) + str(self.fields["Cmd"]) + str( self.fields["CheckSum"]) + str(self.fields["SrcIP"]) + str( self.fields["DestIP"]) + str(self.fields["Data"]) self.fields["Len"] = StructWithLenPython2or3(">H", len(CalculateLen)) # Then CheckSum this packet CheckSumCalc = str(self.fields["VLen"]) + str( self.fields["DifField"]) + str(self.fields["Len"]) + str( self.fields["TID"]) + str(self.fields["Flag"]) + str( self.fields["FragOffset"]) + str(self.fields["TTL"]) + str( self.fields["Cmd"]) + str( self.fields["CheckSum"]) + str( self.fields["SrcIP"]) + str( self.fields["DestIP"]) self.fields["CheckSum"] = GenCheckSum(CheckSumCalc)
class SMBDCEWinRegCreateKey(Packet): fields = OrderedDict([ ("ContextHandle", ""), ("KeySizeUnicode", "\x00\x00"), ("MaxKeySizeUnicode", "\x00\x00"), ("KeyPtr", "\xde\x01\x00\x00"), ("ActualKeyMaxSize", "\x00\x00\x00\x00"), ("ActualKeyOffset", "\x00\x00\x00\x00"), ("ActualKeySize", "\x00\x00\x00\x00"), ("KeyName", "SAM"), ("KeyTerminator", "\x00\x00"), ("KeyClassLen", "\x00\x00"), ("KeyClassMaxLen", "\x00\x00"), ("KeyClassPtr", "\x00\x00\x00\x00"), ("AccessOption", "\x01\x00\x00\x00"), ("AccessMask", "\x00\x00\x00\x02"), ("SecDescRefID", "\x29\xd8\x00\x00"), ("SecDescLen", "\x00\x00\x00\x00"), ("SecDescPtr", "\x00\x00\x00\x00"), ("SecDescMaxLen", "\x00\x00\x00\x00"), ("SecDescMaxSize", "\x00\x00\x00\x00"), ("Inherit", "\x00\x00\x00\x00"), ("AccessTakenRefID", "\x9c\x3e\x00\x00"), ("ActionTakenRefID", "\x01\x00\x00\x02"), ]) def calculate(self): #Padding if len(str(self.fields["KeyName"]))%2==0: self.fields["KeyTerminator"] = "\x00\x00\x00\x00" else: self.fields["KeyTerminator"] = "\x00\x00" #Calc first. self.fields["ActualKeyMaxSize"] = struct.pack("<i",len(str(self.fields["KeyName"]))+1) self.fields["ActualKeySize"] = struct.pack("<i",len(str(self.fields["KeyName"]))+1) #Convert to unicode. self.fields["KeyName"] = self.fields["KeyName"].encode('utf-16le') #Recalculate again, in unicode this time. self.fields["KeySizeUnicode"] = struct.pack("<h",len(str(self.fields["KeyName"]))+2) self.fields["MaxKeySizeUnicode"] = struct.pack("<h",len(str(self.fields["KeyName"]))+2)
def cytof54_values_from_filename(filename): filename = os.path.split(filename)[-1] filename = filename.replace('concatv3_curated', 'curated') vals = OrderedDict() vals['filename'] = filename filename = filename.replace('.', '_') tags = filename.split('_') assert tags[0] == 'CyTOF54' assert tags[1][:4] == 'Tube' vals['tube'] = int(tags[1][4]) assert tags[2][:3] == 'Day' vals['day'] = int(tags[2][3]) vals['stim'] = tags[3] assert tags[4] == 'curated' assert tags[5] == 'fcs' assert tags[6] == 'eventnum' assert tags[7] == 'Ungated' print tags[8] assert tags[8] == vals[' '] vals['normalized'] = 1 return vals