def main(): settings = None try: meta, settings, profile, debug, benchmarks, name, port = setup() while True: data = ui.main(meta, settings) if data is None: break if data['local']: # Local Server server_obj = server_interface.LocalInterface(name, data['save'], port, settings) else: # Remote Server server_obj = server_interface.RemoteInterface(name, data['ip'], data['port']) if not server_obj.error: render_interface.setup_render_module(settings) if profile: cProfile.runctx('game(server_obj, settings, benchmarks)', globals(), locals(), filename='game.profile') elif debug: pdb.run('game(server_obj, settings, benchmarks)', globals(), locals()) else: game(server_obj, settings, benchmarks) if server_obj.error: ui.error(server_obj.error) finally: setdown()
def exit_if_path_exists(self): """ Exit early if the path cannot be found. """ if os.path.exists(self.output_path): ui.error(c.MESSAGES["path_exists"], self.output_path) sys.exit(1)
def status(publish_path): u''' 检查发布库的编译状态 ''' publish_path, root_path = StaticPackage.get_roots(publish_path) if not publish_path: ui.error(u'不是发布库') return 1 package = StaticPackage(root_path, publish_path) files = package.get_publish_files() for filename in files: filetype = os.path.splitext(filename)[1] source, mode = package.parse(filename) try: rfiles = package.get_relation_files(source, all = True) except PackageNotFoundException, e: ui.error(u'%s package not found' % e.url) else: modified, not_exists = package.listener.check(filename, rfiles) if len(modified) or len(not_exists): for modified_file in modified: ui.msg('M ' + modified_file) for not_exists_file in not_exists: ui.msg('! ' + not_exists_file)
def exit_if_path_not_found(path): """ Exit if the path is not found. """ if not os.path.exists(path): ui.error(c.MESSAGES["path_missing"], path) sys.exit(1)
def link(path, link_path, force = False): u''' 将发布库与源库进行映射 如果库设置了url,则同时使用.package文件进行连接,需要工作区支持,如果没有url,则只进行本地连接。''' publish_path, root_path = StaticPackage.get_roots(path) if not publish_path and not root_path and link_path: publish_path, root_path = StaticPackage.get_roots(link_path) path, link_path = link_path, path if not publish_path: publish_path = os.path.realpath(link_path) else: root_path = os.path.realpath(link_path) if not root_path: ui.error('package not found') package = StaticPackage(root_path, publish_path = publish_path) if not os.path.exists(publish_path): if force: os.makedirs(publish_path) else: ui.msg(u'%s path not exists, run opm link path -f to create it.' % publish_path) return 1 package.link() ui.msg(u'linked publish %s to %s' % (publish_path, root_path))
def serve(workspace_path, fastcgi = False, port = 8080, debug = False, noload = False, hg = False, hg_port = 8000): u''' 启动一个可实时编译的静态服务器 请指定工作区路径''' if Workspace.is_root(workspace_path): workspace = Workspace(os.path.realpath(workspace_path)) if not noload: load(workspace = workspace) else: ui.error(u'工作区无效'); workspace = None def print_request(environ, start_response): ''' 输出fastcgi本次请求的相关信息 ''' import cgi start_response('200 OK', [('Content-Type', 'text/html')]) yield '<html><head><title>Hello World!</title></head>\n' \ '<body>\n' \ '<p>Hello World!</p>\n' \ '<table border="1">' names = environ.keys() names.sort() for name in names: yield '<tr><td>%s</td><td>%s</td></tr>\n' % ( name, cgi.escape(`environ[name]`)) form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ, keep_blank_values=1) if form.list: yield '<tr><th colspan="2">Form data</th></tr>' for field in form.list: yield '<tr><td>%s</td><td>%s</td></tr>\n' % ( field.name, field.value) yield '</table>\n' \ '</body></html>\n' def listen(environ, start_response): ''' 监听请求 ''' if environ['DOCUMENT_URI'].endswith('/net.test'): return print_request(environ, start_response) DEBUG = debug filename = os.path.realpath(environ['REQUEST_FILENAME']) url = environ['DOCUMENT_URI'] force = False # 是否强制重新编译 # 没有 referer 时强制重新编译 if not 'HTTP_REFERER' in environ.keys(): force = True try: publish_path, root_path = StaticPackage.get_roots(filename, workspace = workspace) except PackageNotFoundException, e: ui.error(u'%s package not found' % e.url) else:
def main(): try: meta, settings, profile, name, port = setup() while True: data = ui.main(meta, settings) if data is None: break if data['local']: # Local Server server_obj = server_interface.LocalInterface(name, data['save'], port) else: # Remote Server server_obj = server_interface.RemoteInterface(name, data['ip'], data['port']) if not server_obj.error: if profile: cProfile.runctx('game(server_obj, settings)', globals(), locals(), filename='game.profile') else: game(server_obj, settings) if server_obj.error: ui.error(server_obj.error) finally: setdown()
def exit_if_missing_graphviz(self): """ Detect the presence of the dot utility to make a png graph. """ (out, err) = utils.capture_shell("which dot") if "dot" not in out: ui.error(c.MESSAGES["dot_missing"])
def validate_format(self, allowed_formats): """ Validate the allowed formats for a specific type. """ if self.format in allowed_formats: return ui.error("Export type '{0}' does not accept '{1}' format, only: " "{2}".format(self.type, self.format, allowed_formats)) sys.exit(1)
def file_to_string(path): """ Return the contents of a file when given a path. """ if not os.path.exists(path): ui.error(c.MESSAGES["path_missing"], path) sys.exit(1) with codecs.open(path, "r", "UTF-8") as contents: return contents.read()
def url_to_string(url): """ Return the contents of a web site url as a string. """ try: page = urllib2.urlopen(url) except (urllib2.HTTPError, urllib2.URLError) as err: ui.error(c.MESSAGES["url_unreachable"], err) sys.exit(1) return page
def mkdir_p(path): """ Emulate the behavior of mkdir -p. """ try: os.makedirs(path) except OSError as err: if err.errno == errno.EEXIST and os.path.isdir(path): pass else: ui.error(c.MESSAGES["path_unmakable"], err) sys.exit(1)
def file_to_list(path): """ Return the contents of a file as a list when given a path. """ if not os.path.exists(path): ui.error(c.MESSAGES["path_missing"], path) sys.exit(1) with codecs.open(path, "r", "UTF-8") as contents: lines = contents.read().splitlines() return lines
def graph_png(self): """ Export a graph of the data in png format using graphviz/dot. """ if not self.out_file: ui.error(c.MESSAGES["png_missing_out"]) sys.exit(1) cli_flags = "-Gsize='{0}' -Gdpi='{1}' {2} ".format(self.size, self.dpi, self.flags) cli_flags += "-o {0}".format(self.out_file) (out, err) = utils.capture_shell( "ansigenome export -t graph -f dot | dot -Tpng {0}" .format(cli_flags)) if err: ui.error(err)
def yaml_load(path, input="", err_quit=False): """ Return a yaml dict from a file or string with error handling. """ try: if len(input) > 0: return yaml.load(input) elif len(path) > 0: return yaml.load(file_to_string(path)) except Exception as err: file = os.path.basename(path) ui.error("", c.MESSAGES["yaml_error"].replace("%file", file), err, "") if err_quit: sys.exit(1) return False
def compile(filename, package = None, force = False, no_build_files = False): u'编译一个css/js文件' filename = os.path.realpath(filename) if not package: publish_path, root_path = StaticPackage.get_roots(filename) if not root_path: ui.error(u'没有找到源文件') return 1 else: package = StaticPackage(root_path, publish_path) try: modified, not_exists = package.compile(filename, force = force) except IOError, e: ui.error('%s file not found' % e.filename) return 1
def listen(environ, start_response): ''' 监听请求 ''' if environ['DOCUMENT_URI'].endswith('/net.test'): return print_request(environ, start_response) DEBUG = debug filename = os.path.realpath(environ['REQUEST_FILENAME']) url = environ['DOCUMENT_URI'] force = False # 是否强制重新编译 # 没有 referer 时强制重新编译 if not 'HTTP_REFERER' in environ.keys(): force = True try: publish_path, root_path = StaticPackage.get_roots(filename, workspace = workspace) except PackageNotFoundException, e: ui.error(u'%s package not found' % e.url)
def packages(workspace_path, show_url = False): u''' 本工作区中所有源库 ''' if os.path.isfile(workspace_path): workspace_path = os.path.dirname(workspace_path) # 有可能不是workspace跟路径,而是某个子路径 workspace_path = Workspace.get_workspace(workspace_path) if not workspace_path: ui.error(u'没有工作区') return 1 else: workspace = Workspace(workspace_path) if show_url: for url in workspace.url_packages.keys(): ui.msg(url) else: for local_path in workspace.local_packages.keys(): ui.msg(os.path.realpath(os.path.join(workspace.root, local_path)))
def incs(filename, all = False, reverse = False): u''' 某文件所有依赖的文件 ''' filename = os.path.realpath(filename) root_path = StaticPackage.get_root(filename) package = StaticPackage(root_path) filetype = os.path.splitext(filename)[1] if reverse: if filetype == '.css': ui.error(u'Not support yet, sorry.') return 1 else: files = package.get_included(filename, all = all) else: files = package.get_relation_files(filename, all = all) for file in files: ui.msg(file)
def get(workspace, url): u''' 从公共代码库获取源库 当前目录会被作为工作区,所有获取到的源库代码都会放到此工作区内 ''' if workspace.__class__ == str: try: workspace = Workspace(workspace) except ConfigError as e: ui.error(u'config error %s' % e.path) return 1 load(workspace) try: packages = workspace.fetch_packages(url) except PackageNotFoundException, e: ui.error('%s package not found' % e.url) return 1
def load(workspace): u''' 加载本地工作区 ''' if workspace.__class__ == str: workspace_path = Workspace.get_workspace(workspace) if not workspace_path: workspace_path = workspace try: workspace = Workspace(workspace_path) except ConfigError as e: ui.error(u'config error %s' % e.path) return 1 old_count = len(workspace.local_packages) workspace.load() added_count = len(workspace.local_packages) - old_count if len(workspace.useless_packages): for package in workspace.useless_packages: ui.msg(u'删除无用package %s' % package) ui.msg(u'已加入 %s 个源库' % added_count)
def execute_command(self): """ Execute the shell command. """ stderr = "" role_count = 0 for role in utils.roles_dict(self.roles_path): self.command = self.command.replace("%role_name", role) (_, err) = utils.capture_shell("cd {0} && {1}". format(os.path.join( self.roles_path, role), self.command)) stderr = err role_count += 1 utils.exit_if_no_roles(role_count, self.roles_path) if len(stderr) > 0: ui.error(c.MESSAGES["run_error"], stderr[:-1]) else: if not self.config["options_quiet"]: ui.ok(c.MESSAGES["run_success"].replace( "%role_count", str(role_count)), self.options.command)
def init(root_path, publish_path = None, force = False): u''' 初始化一个新的库 初始化一个新的库,建立template-config.xml配置文件及常用的目录,如果指定了 -p 参数,还可以自动建立与发布目录的连接''' # 确保不要init了一个工作区 if Workspace.get_workspace(root_path) == root_path: ui.error(u'工作区不能被初始化') return 1 # 确保不要init了一个publish库 if StaticPackage.get_publish(root_path): ui.error(u'发布目录不能被初始化') return 1 ui.msg(u'初始化%s' % root_path) try: StaticPackage.init(root_path) ui.msg(u'创建配置文件') except PackageExistsException: ui.error(u'已经存在') return 1 pathnames = ['test', 'doc', 'src', 'lib', 'res'] for name in pathnames: path = os.path.join(root_path, name) if not os.path.exists(path): os.makedirs(path) ui.msg(u'生成默认目录 %s' % name) workspace_path = Workspace.get_workspace(root_path) if not workspace_path: ui.msg(u'没有工作区,请参照 opm help load') else: workspace = Workspace(workspace_path) if not workspace.has_package(root_path): workspace.add_package(root_path) ui.msg(u'加入本地工作区') else: ui.msg(u'本地工作区中已存在') ui.msg(u'成功!') if publish_path: link(root_path, publish_path, force = force)
def libs(root_path, show_url = False, all = False, reverse = False): u''' 库的相关依赖库 ''' root_path = StaticPackage.get_root(root_path) if not root_path: ui.error(u'不是源库') return 1 package = StaticPackage(root_path) # 这个库没有设置url,不可能被别的库依赖 if not package.url: ui.error(u'no url') return 1 # 显示依赖自己的 if reverse: try: libs = package.get_reverse_libs(all = all) except PackageNotFoundException, e: ui.error(u'%s package not found' % e.url) return 1
if workspace.__class__ == str: try: workspace = Workspace(workspace) except ConfigError as e: ui.error(u'config error %s' % e.path) return 1 load(workspace) try: packages = workspace.fetch_packages(url) except PackageNotFoundException, e: ui.error('%s package not found' % e.url) return 1 except ConfigError as e: ui.error(u'config error %s' % e.path) return 1 else: for package in packages: try: workspace.fetch(package) except ImportError, e: ui.error(u'mercurial module not found.') return 1 except PackageExistsException, e: ui.error(u'%s package already exists' % e.root) @cwdarg @usage(u'opm workspace [源库路径]') def workspace(root_path): u''' 源库所在工作区 '''
def source(publish_path): u''' 映射的源库路径 ''' if StaticPackage.get_publish(publish_path): StaticPackage.get_root(publish_path) else: ui.error(u'不是一个发布库')
def template(path, extend_path, out): """ Return a jinja2 template instance with extends support. """ files = [] # add the "extender" template when it exists if len(extend_path) > 0: # determine the base readme path base_path = os.path.dirname(extend_path) new_base_path = os.path.join(base_path, "README.{0}.j2".format(out)) if os.path.exists(new_base_path): path = new_base_path if os.path.exists(extend_path): files = [path, extend_path] else: ui.error(c.MESSAGES["template_extender_missing"]) ui.error(extend_path) sys.exit(1) else: files = [path] try: # Use the subclassed relative environment class env = RelEnvironment(trim_blocks=True) # Add a unique dict filter, by key. # DEPRECATION WARNING: This is only used for backwards compatibility, # please use the unique filter instead. def unique_dict(items, key): return {v[key]: v for v in items}.values() env.filters["unique_dict"] = unique_dict def unique(a): # Don’t use the following commented out optimization which is used # in ansible/lib/ansible/plugins/filter/mathstuff.py in Ansigenome # as it resorts the role dependencies: # if isinstance(a,collections.Hashable): # c = set(a) c = [] for x in a: if x not in c: c.append(x) return c env.filters["unique"] = unique # create a dictionary of templates templates = dict( (name, codecs.open(name, "rb", 'UTF-8').read()) for name in files) env.loader = DictLoader(templates) # return the final result (the last template in the list) return env.get_template(files[len(files) - 1]) except Exception as err: ui.error(c.MESSAGES["template_error"], err) sys.exit(1)
def main(): print_banner() parser = argparse.ArgumentParser(description='Decode supplied shellcode automatically with the unicorn engine') parser.add_argument('-f', '--file', dest='file', help='file to shellcode binary file', required=False, type=file) parser.add_argument('-m', '--mode', dest='mode', help='mode of the emulator (--show-modes)', required=False, default="w86_32") parser.add_argument('-i', '--instructions', dest='max_instruction', help='max instructions to emulate', required=False) parser.add_argument('-d', '--debug', dest='debug', help='Enable extra hooks for debugging of shellcode', required=False, default=False, action='store_true') parser.add_argument('-o', '--output', dest='output', help='Where to write the decoded shellcode', required=False) parser.add_argument('-s', '--show-modes', dest='show', action='store_true', help='show available modes and exit', required=False) args = parser.parse_args() if args.show: for decoder in decoders: print(decoder) sys.exit(0) if not args.file or not args.mode or args.mode not in decoders: ui.error("bad commandline") sys.exit(0) bin_code = args.file.read() const = decoders[args.mode] cur_arch = const[0] cur_mode = const[1] stack_reg = const[2] stack_val = const[3] instr_ptr = const[4] cs_arch = const[5] cs_mode = const[6] PAGE_SIZE = 5 * 1024 * 1024 START_RIP = 0x0 disas_engine = SimpleEngine(cs_arch, cs_mode) # setup engine and write the memory there. emu = Uc(cur_arch, cur_mode) emu.disasm = disas_engine # python is silly but it works. emu.mem_map(0, PAGE_SIZE) # write machine code to be emulated to memory emu.mem_write(START_RIP, bin_code) # write a INT 0x3 near the end of the code blob to make sure emulation ends emu.mem_write(len(bin_code) + 0xff, "\xcc\xcc\xcc\xcc") emu.hook_add(UC_HOOK_MEM_INVALID, hook_mem_invalid) emu.hook_add(UC_HOOK_MEM_WRITE, hook_smc_check) emu.hook_add(UC_HOOK_INTR, hook_intr) if args.debug: emu.hook_add(UC_HOOK_MEM_READ, hook_mem_read) emu.hook_add(UC_HOOK_CODE, hook_code) # arbitrary address for ESP. emu.reg_write(stack_reg, stack_val) if args.max_instruction: end_addr = -1 else: args.max_instruction = 0x1000 end_addr = len(bin_code) try: emu.emu_start(START_RIP, end_addr, 0, int(args.max_instruction)) except UcError as e: ui.bug("%s" % e) if write_bounds[0] != None: ui.out("{{RED}}Shellcode address ranges:") ui.out(" low: {{BLUE}}0x%X{{RED}}" % write_bounds[0]) ui.out(" high: {{BLUE}}0x%X{{CLEAR}}\n\n" % write_bounds[1]) ui.out("{{GREEN}}Decoded shellcode:{{CLEAR}}") mem = emu.mem_read(write_bounds[0], (write_bounds[1] - write_bounds[0])) emu.disasm.disas_all(str(mem), write_bounds[0]) # Write the decoded shellcode if args.output: with open(args.output, "wb") as f: f.write(mem) else: ui.error("no SMC hits, no encoder detected")