def add_security_with_criterion(self, shares, region, preferences, allSecurities, criterion='Sector'): preferences = self.filter_list_by_criterion(preferences, region, allSecurities, criterion) # give error if list is empty pop = recom.recommend_stock(preferences) security = next(pop) ok = True while self.security_exists_in_portfolio(self.portfolio, security): try: security = next(pop) except StopIteration: pyfancy.pyfancy().red( "Cound not find a new security in {} {}".format( criterion, region)).output() ok = False break if ok: self.addNewSecurity(security, shares, allSecurities) return True else: return False
def runpocwithsysname(self, keyword): try: poclist = list() self.loadmodule() sql = 'SELECT poc from hostexploit WHERE vulname like "%{}%"'.format( keyword) res = db().execute(sql) for item in res: poclist.append(item['poc']) self.queue.put_nowait(item['poc']) mylog('hostexploit', True).log.info(pyfancy().green( '[+]针对目标:{0}:{1} 加载{2} hostpoc {3}个'.format( self.host, self.port, keyword, len(poclist)))) threads = [gevent.spawn(self.Consumer, item) for item in poclist] gevent.joinall(threads) for vuln in self.vulns: sqlstr = 'INSERT INTO hostvulnlist (vulnhost, vulnport, vulnname, isvul, payload, proof, exception) VALUE ("{0}", "{1}", "{2}", "{3}", "{4}", "{5}", "{6}")'.format( vuln['vulnhost'], vuln['vulnport'], vuln['vulnname'], vuln['isvul'], pymysql.escape_string(str(vuln['payload'])), vuln['proof'], pymysql.escape_string(str(vuln['exception']))) db().execute(sqlstr) vuln = json.dumps(vuln, indent=4) mylog('hostexploit').log.debug(pyfancy().magenta( '[*] {0}'.format(vuln))) self.vulns = [] except Exception as e: mylog('hostexploit').log.critical(pyfancy().red(e))
def refine_exposure_to_5(client_portfolio, allSecurities): stocks_alloc, bonds_alloc, recommended_stock_alloc, recommended_bonds_alloc = getAllocations( client_portfolio, allSecurities, False) pyfancy.pyfancy().cyan("Started exposure refining... \n").output() time.sleep(1) client_portfolio.refine_exposures() return client_portfolio.portfolio
def runpocwithcmsname(self, keyword): try: poclist = list() pool = Pool(self.threads) self.loadmodule() sql = 'SELECT poc FROM webexploit WHERE vulname LIKE "%{}%"'.format( keyword) res = db().execute(sql) for item in res: poclist.append(item['poc']) mylog('webexploit', True).log.info(pyfancy().green( '[+]针对目标:{0} 加载{1} webpoc {2}个'.format(self.url, keyword, len(poclist)))) threads = [pool.spawn(self.pocexec, item) for item in poclist] gevent.joinall(threads) for vuln in self.vulns: db().execute( 'INSERT INTO webvulnlist (url, vulname, vulnurl, isvul, payload, proof, exception) VALUE ("{0}", "{1}", "{2}", "{3}", "{4}", "{5}", "{6}")' .format(self.url, vuln['vulnname'], pymysql.escape_string(str(vuln['vulnurl'])), vuln['isvul'], pymysql.escape_string(str(vuln['payload'])), pymysql.escape_string(str(vuln['proof'])), pymysql.escape_string(str(vuln['exception'])))) vuln = json.dumps(vuln, indent=4) mylog('webexploit').log.debug(pyfancy().magenta( '[*] {}'.format(vuln))) self.vulns = [] except Exception as e: mylog('webexploit').log.critical(pyfancy().red(e))
def free_wilson_enumeration(core_file_name: str, model_file_name: str, prefix: str, max_list: str = None) -> None: """ driver function - enumerate products from core + r-groups :param core_file_name: core molfile name :param model_file_name: file name with pickled model :param prefix: prefix for output :param max_list: comma delimited string with the maximum numbers of R-groups to enumerate e.g. "5,2,3" :return: None """ vector_file_name = f"{prefix}_vector.csv" core_mol = Chem.MolFromMolFile(core_file_name) reflect_rgroups(core_mol) core_smiles = Chem.MolToSmiles(core_mol) pyfancy.pyfancy().red().bold().add("Enumerating new products").output() lm, df = read_input(model_file_name, vector_file_name) bit_dict = dict([(x[1], x[0]) for x in enumerate(df.columns[1:])]) num_row, num_cols = df.shape vector_size = num_cols - 1 used, r_group_summary = get_rgroups(prefix, max_list) num_r_groups = len(r_group_summary) # handle the case where only 1 r-group is provided if num_r_groups > 1: stream_output(used, lm, core_smiles, r_group_summary, prefix, vector_size, bit_dict, 1000) else: print("only 1 R-group, no additional products possible")
def free_wilson_enumeration(core_file_name, model_file_name, vector_file_name, prefix): """ Driver function - enumerate products from core + R-groups @param core_file_name: core molfile name @param model_file_name: file name with pickled model @param vector_file_name: file with R-group descriptors @param prefix: prefix for output @return: Nothing """ core_mol = Chem.MolFromMolFile(core_file_name) reflect_rgroups(core_mol) core_smiles = Chem.MolToSmiles(core_mol) pyfancy.pyfancy().red().bold().add("Enumerating New Products").output() lm, df = read_input(model_file_name, vector_file_name) num_row, num_cols = df.shape vector_size = num_cols - 1 used, r_group_summary = parse_rgroups(df) num_r_groups, offset_list = build_offset_list(r_group_summary) # Handle the case where only 1 R-group is provided if num_r_groups > 1: output_list = build_output_list(core_smiles, used, r_group_summary, vector_size, offset_list) print("Generated %d products" % len(output_list)) if len(output_list) > 0: write_output(lm, df, num_r_groups, output_list, prefix) else: print("only 1 R-group, no additional products possible")
def getTodos(): todos = db.get('todos') for todo in todos: todoText = str(todo["id"]) + ". " + todo["title"] if todo["complete"] == True: print(pyfancy().dim(todoText + ' ✔').get()) # add a check mark else: print(pyfancy().green(todoText).get())
def eric_encoding_summary(): eric_encoding_function() if ENCODING_WINDOWS_1251: LOG.notice(pyfancy().green().bold("All files in Windows-1251 encoding")) if not ENCODING_WINDOWS_1251: LOG.critical(pyfancy().red_bg().bold( "One or more your files not in Windows-1251 encoding. Please, convert it (them) to Windows-1251."))
def setExposure(self, security, new_exposure): time.sleep(1) pyfancy.pyfancy().bold().yellow( "Modified exposure for security {} from {} % to {} % \n".format( security, self.portfolio[security], new_exposure)).output() self.cash = self.cash + self.portfolio[security] - new_exposure self.portfolio[security] = new_exposure print("You now have in CASH : {} % exposure".format(self.cash)) return self.portfolio[security]
def addNewSecurity(self, security, shares, allSecurities): self.portfolio[security] = shares pyfancy.pyfancy().bold().yellow( "Added to portfolio {} from sector {} and region {} with {} exposure \n" .format(security, allSecurities[security]['Sector'], allSecurities[security]['Region'], shares)).output() self.cash = self.cash - shares print("You now have in CASH : {} % exposure".format(self.cash)) return self.portfolio
def useWhois(self): mylog('webprint', True).log.info(pyfancy().green( '[+]执行whois检查注册信息: {}'.format(self.url))) judge = judgement(self.url).urlSplit()[1] try: domain_reg = whois.whois(judge) return domain_reg except Exception as e: mylog('webprint').log.critical(pyfancy().red(e)) return {}
def refine_exposure_to_5(client_portfolio, allSecurities, preferences): stocks_alloc, bonds_alloc, recommended_stock_alloc, recommended_bonds_alloc = getAllocations( client_portfolio, allSecurities, False) pyfancy.pyfancy().cyan("Started exposure refining... \n").output() for key, exposure in client_portfolio.portfolio.items(): if (key != 'nan'): if exposure > 5: client_portfolio.setExposure(key, 5) elif (key == 'nan'): break return client_portfolio.portfolio
def eric_body_summary(): """Report, contains <body> in all files or no. Use flags, see https://stackoverflow.com/a/48052480/5951529 """ eric_body_function() if BODY_EXIST: LOG.notice(pyfancy().green().bold("All files contains <body>")) if not BODY_EXIST: LOG.error(pyfancy().red().bold( "Not all files contains <body>. Please, correct it."))
def runexplore(self, url): mylog('webprint', True).log.info(pyfancy().green('[+]执行web信息收集: {}'.format(url))) runApp = explore(url) cdnheader = runApp.useCDNHeader() dig = runApp.useDig() getheaders = runApp.header whois = runApp.useWhois() builtwith = runApp.useBuiltwith() mycdn = runApp.myCdnWaf() wappalyzer = runApp.useWappalyzer() whatweb = runApp.useWhatweb() hsec = runApp.hsecscan() '''网页版模板 webinfo_html = '{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}'.format( self.html_start(), self.webinfo_header(), self.webinfo_template('通用CDN检测', cdnheader), self.webinfo_template('Dig CDN', dig), self.webinfo_template('Get Headers', getheaders), self.webinfo_template('Whois', whois), self.webinfo_template('builtwith', builtwith), self.webinfo_template('CDN/WAF Detect', mycdn), self.webinfo_template('Wappalyzer', wappalyzer), self.webinfo_template('Whatweb', whatweb), self.webinfo_template('headers sec', hsec), self.html_end()) dirpath = os.path.join(GlobalConf().progpath['location'], 'Heaven_Hell/webrecon') if not os.path.isdir(dirpath): os.makedirs(dirpath) filename = '{0}_{1}.html'.format(urlparse(url)[1], time.strftime('%Y-%m-%d_%H_%M_%S',time.localtime(time.time()))) pathname = os.path.join(dirpath, filename) with open(pathname, 'w') as f: f.write(webinfo_html) mylog('webprint', True).log.info(pyfancy().light_cyan('[+]web信息写入文件: {}'.format(pathname))) ''' #数据库归并 iprecon = judgement(self.url).iplocation() sqlstr = 'INSERT INTO webrecon (Project, URL, cdnheader, Dig, Headers, Whois, Builtwith, Mycdn, wappalyzer, Whatweb, Hsec, Iprecon) VALUE ("{0}", "{1}", "{2}", "{3}", "{4}", "{5}", "{6}", "{7}", "{8}", "{9}", "{10}", "{11}")'.format( self.project, self.url, cdnheader, dig, pymysql.escape_string(str(getheaders)), pymysql.escape_string(str(whois)), pymysql.escape_string(str(builtwith)), pymysql.escape_string(str(mycdn)), pymysql.escape_string(str(wappalyzer)), pymysql.escape_string(str(whatweb)), pymysql.escape_string(str(hsec)), pymysql.escape_string(str(iprecon))) db().execute(sqlstr) mylog('webprint', True).log.info(pyfancy().green('[*]结束web信息收集: {}'.format(url)))
def deleteSector(self, sector, security_dict): pyfancy.pyfancy().cyan( "Scanning portfolio to delete securities from sector {}".format( sector)).output() time.sleep(1) securities_to_del = [] for key, value in self.portfolio.items(): current_security = security_dict[key] if current_security['Sector'] == sector: securities_to_del.append(key) for key in securities_to_del: self.deleteSecurity(key, security_dict) print('Deleted sector {}'.format(sector))
def useWhatweb(self): out = '' mylog('webprint', True).log.info(pyfancy().green( '[+]执行whatweb识别后端组件: {}'.format(self.url))) try: cmd = ['whatweb', '-a', '3', '--color=never', self.url] process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = process.communicate() out = bytes.decode(out) except Exception as e: mylog('webprint').log.critical(pyfancy().red(e)) return out
def refine_alloc_with_risk_profile(client_portfolio, allSecurities, stock_preferences, bonds_preferences, economy): stocks_alloc, bonds_alloc, recommended_stock_alloc, recommended_bonds_alloc = getAllocations( client_portfolio, allSecurities, True) if (stocks_alloc == recommended_stock_alloc and bonds_alloc == recommended_bonds_alloc): print("Congrats! Asset allocation is set properly.") else: pyfancy.pyfancy().cyan("Started exposure refining... \n").output() balance_allocation(client_portfolio, allSecurities, bonds_alloc, stocks_alloc, recommended_bonds_alloc, recommended_stock_alloc, stock_preferences, bonds_preferences, economy, True) return client_portfolio.portfolio
def useDig(self): mylog('webprint', True).log.info(pyfancy().green( '[+]执行dig识别: {}'.format(self.url))) judge = judgement(self.url).urlSplit()[1] try: cmd = ['dig', judge] process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out, err = process.communicate() content_str = bytes.decode(out).lower() return content_str.strip() except Exception as e: mylog('webprint').log.critical(pyfancy().red(e)) return 'NULL'
def useWappalyzer(self): out = '' mylog('webprint', True).log.info(pyfancy().green( '[+]执行Wappalyzer识别目标指纹: {}'.format(self.url))) try: wappalyzer = Wappalyzer.latest() webpage = WebPage.new_from_url(self.url) webprints = wappalyzer.analyze(webpage) if len(webprints) > 0: return webprints else: return {} except Exception as e: mylog('webprint').log.critical(pyfancy().red(e)) return out
def useCDNHeader(self): mylog('webprint', True).log.info(pyfancy().green( '[+]执行通用CDN识别: {}'.format(self.url))) try: key = False cdn_headers = [ 'X-CDN', 'via', 'x-cache', 'x-swift-cachetime', 'X-Cache-Lookup', 'X-Via', 'Via', 'X-Via-CDN', 'X-Cdn', 'X-Cache', 'CDN-Cache', 'CDN-Server', 'X-Cdn-Srv', 'Cdn', 'CDN', 'Cache-Control', 'X-Cache-Error', 'X-Upper-Cache', "X-Cacheable", 'X-Cacheable-status', 'X-Status', 'X-DNS', 'X-Proxy', 'CacheStatus', 'X-Fastcgi-Cache', 'X-Backend', 'X-PingBack', 'X-Executed-By', 'X-Front', 'X-Server', 'CDN-Node', 'X-Rack-Cache', 'X-Request-Id', 'X-Runtime', ] for cdn_head in cdn_headers: if cdn_head in self.header: key = True return key except Exception as e: mylog('webprint').log.critical(pyfancy().red(e)) return False
def getAllocations(client_portfolio, allSecurities, text=True): stocks_alloc, bonds_alloc = client_portfolio.current_asset_allocation( allSecurities) recommended_stock_alloc, recommended_bonds_alloc = client_portfolio.recommended_asset_allocation( client_portfolio.client['Risk_profile']) if text: pyfancy.pyfancy().cyan( "Current allocation: {} stocks and {} bonds \n".format( stocks_alloc, bonds_alloc)).output() pyfancy.pyfancy().cyan( "Recommended allocation: {} stocks and {} bonds \n".format( recommended_stock_alloc, recommended_bonds_alloc)).output() return stocks_alloc, bonds_alloc, recommended_stock_alloc, recommended_bonds_alloc
def test_reset(self): expected = color('red', 'test') pf = pyfancy().red('test') self.assertEqual(pf.get(), expected) expected = '\x1b[0m' actual = pf.reset().get() self.assertEqual(actual, expected)
def sendrequestsget(self, url): headers = {'User-Agent': findProxy().randomUA()} targeturl = self.url + url try: req = requests.get(targeturl, headers=headers, verify=False, timeout=10, allow_redirects=False) if len(req.text) == self.length or len( req.text) - 32 == self.length: pass else: if req.status_code != 404 and req.status_code != 400 and req.status_code != 412 and req.status_code != 403: if url in self._403 and req.status_code == 403: pass else: tmpdict = { 'url': targeturl, 'status_code': req.status_code, 'length': len(req.text) } mylog('webpath', True).log.info(pyfancy().green( '[+]发现web路径: {0}'.format(str(tmpdict)))) self.webpath.append(tmpdict) except: pass gevent.sleep(0)
def sendrequestshead(self, url): headers = {'User-Agent': findProxy().randomUA()} targeturl = self.url + url self.count += 1 #cprint("[*] 加载") cprint('#Process: {}\t[{:.2%}]{}\r'.format( targeturl, (self.count / 9739), ' ' * (len(targeturl) - 90)), 'yellow', attrs=['bold'], end='', flush=True) sys.stdout.flush() try: req = requests.head(targeturl, headers=headers, verify=False, timeout=10, allow_redirects=False) if req.status_code != 404 and req.status_code != 400 and req.status_code != 412 and req.status_code != 403: if url in self._403 and req.status_code == 403: pass else: tmpdict = { 'url': targeturl, 'status_code': req.status_code } mylog('webpath', True).log.info(pyfancy().green( '[+]发现web路径: {0}'.format(str(tmpdict)))) self.webpath.append(tmpdict) except: pass gevent.sleep(0)
def useBuiltwith(self): # 全局取消SSL证书验证 ssl._create_default_https_context = ssl._create_unverified_context mylog('webprint', True).log.info(pyfancy().green( '[+]执行builtwith识别前端组件: {}'.format(self.url))) res = builtwith.builtwith(self.url) return res
def execute(self, sql, param=None): if self.connectdb(): try: if self.conn and self.cursor: self.cursor.execute(sql, param) self.conn.commit() except Exception as e: mylog('database').log.critical(pyfancy().red( '数据库错误: {}'.format(e))) # 发生错误时回滚 self.conn.rollback() mylog('database').log.info(pyfancy().green( 'SQL已执行: {}'.format(sql))) data = self.cursor.fetchall() self.close() return data
def useMasscanTCP(self): mylog('hostprint', True).log.info(pyfancy().green( '[+]执行masscan TCP端口扫描: {}'.format(self.host))) try: ports = list() cmd = [ 'masscan', '-sS', '-Pn', '-p21-25,53,80-90,99,110,113,119,121-123,137-139,\ 170,443-445,456,554,513-514,559,873,888,1080-1099,1200-1212,1234,1243-1255,\ 1433-1434,1521,2000,2049,2181,2200-2300,2375,2535,3127-3128,3300-3310,3389,\ 4443-4444,5000-5001,5432,5900-5901,5432,5984,6000,6370-6380,6984,7000-7010,\ 8000-8200,8443-8449,8880-8900,9000-9001,9043,9080-9100,9200-9210,9300,9668,\ 9876,9990-10000,10080,11211,12345,16379,18080,20000-20010,22220-23000,26379,\ 27010-27020,33060,50070', self.host ] out1, err1 = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() out2, err2 = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() pattern = re.compile('[0-9]+/tcp') list1 = re.findall(pattern, bytes.decode(out1)) list2 = re.findall(pattern, bytes.decode(out2)) listres = list(set(list1).union(set(list2))) for item in listres: ports.append(item.replace('/tcp', '')) return ports except Exception as e: mylog('hostprint').log.critical(e) return {}
def deleteSecurity(self, security, allSecurities): current_security = allSecurities[security] security_type = current_security['Asset'] exposure = self.portfolio.pop(security, None) time.sleep(1) if security_type == 'Equity': pyfancy.pyfancy().bold().yellow( "Deleted stock {} that had {} exposure \n".format( security, exposure)).output() elif security_type == 'Bond': pyfancy.pyfancy().bold().yellow( "Deleted bond {} that had {} exposure \n".format( security, exposure)).output() self.extra_exposure = self.extra_exposure + exposure print("You now have in CASH : {} % exposure".format( self.extra_exposure))
def addNewSecurity(self, security, shares, allSecurities): current_security = allSecurities[security] security_type = current_security['Asset'] self.portfolio[security] = shares time.sleep(1) if security_type == 'Equity': pyfancy.pyfancy().bold().yellow( "Added to portfolio stock {} with {} exposure \n".format( security, shares)).output() elif security_type == 'Bond': pyfancy.pyfancy().bold().yellow( "Added to portfolio bond {} with {} exposure \n".format( security, shares)).output() self.extra_exposure = self.extra_exposure - shares print("You now have in CASH : {} % exposure".format( self.extra_exposure))
def useMasscanUDP(self): mylog('hostprint', True).log.info(pyfancy().green( '[+]执行masscan UDP端口扫描: {}'.format(self.host))) try: ports = list() cmd = [ 'masscan', '-sS', '-Pn', '-pU:20-25,79,110,123,137-139,161,180,513-514,559,666,999,\ 1011-1032,1042-1054,1200-1201,1342-1349,2000-2002,3333,6666,26274,26374,26444,26573,\ 27184,27444,29589,29891,30103,31320-31340,34555,35555', self.host ] out1, err1 = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() out2, err2 = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() pattern = re.compile('[0-9]+/udp') list1 = re.findall(pattern, bytes.decode(out1)) list2 = re.findall(pattern, bytes.decode(out2)) listres = list(set(list1).union(set(list2))) for item in listres: ports.append(item.replace('/udp', '')) return ports except Exception as e: mylog('hostprint').log.critical(e) return {}