def check_liveness(container_name, host_port): for _ in range(3): _, _, e = run_command('nc -z 127.0.0.1 %d' % host_port, None) if e != 0: print("[*] %s service is not running. e = %d." % (container_name, e)) time.sleep(1) else: print("[*] %s service looks well." % container_name) break
def get_next_commit_hash(dir, branch, commit_hash): command = 'git -C %s rev-list --reverse --ancestry-path %s..origin/%s' \ % (dir, commit_hash, branch) output, err, r = run_command(command, os.getcwd()) if r != 0: print("[*] Failed to get the next commit after %s" % commit_hash) print err sys.exit() output = output.split('\n')[0] return output.strip()
def exec_exploit(name, exploit_dir, ip, port, timeout): docker_cleanup(name) script = os.path.join(base_dir(), "launch_exploit.sh") _, err, e = run_command('%s "%s" %s %d %d' % \ (script, name, ip, port, \ timeout), exploit_dir) if e != 0: print(err) print('[*] Failed to execute the service.') else: print('[*] Service is up.')
def clone(repo_owner, repo_name, prompt=False, target_dir=None): target = repo_name if target_dir is None else target_dir if prompt: prompt_rmdir_warning(target) rmdir(target) url = '[email protected]:%s/%s' % (repo_owner, repo_name) _, err, r = run_command("git clone %s %s" % (url, target), os.getcwd()) if r!= 0: print '[*] Failed to clone: "%s"' % url print err sys.exit()
def get_cpu(cls): cpu_speed = 0 cpu_max = 0 cpu_name = "unknown" cpu_used = 0 system_name = OsHelper.name() if system_name == "Windows": cpu_data = run_command( "wmic cpu get loadPercentage, CurrentClockSpeed, maxClockSpeed, name" )[1] log.debug("CPU DATA: %s" % cpu_data) cpu = cpu_data.split() cpu_speed = cpu.pop(0) + "Mhz" cpu_used = cpu.pop(0) cpu_max = cpu.pop(0) cpu_name = " ".join(cpu) if "@" in cpu_name: cpu_name = cpu_name.split("@")[0].strip() elif system_name == "Linux": cpus = run_command( 'top -b -n2 -p 1 | fgrep "Cpu(s)" | tail -1')[0].split() index = 0 idle = 0 for val in cpus: if val == "id,": idle = cpus[index - 1] print "Idle: %s" % idle index += 1 clock = run_command( "dmidecode -t processor | grep Current")[0].split() clock_string = run_command( "dmidecode -t 4 | grep Version")[0].split(" CPU @ ") cpu_speed = (clock[2] + clock[3]) + "Mhz" cpu_max = clock_string[1] cpu_used = 100 - float(idle) cpu_name = clock_string[0] elif system_name == "MacOSX": log.debug("OSX CPU QUERY") clock_string = run_command( "sysctl -n machdep.cpu.brand_string")[0].split(" @ ") cpu_speed = run_command("sysctl hw.cpufrequency")[0].split(": ")[1] cpu_max = clock_string[1] cpu_used = run_command( "ps -A -o %cpu | awk '{s+=$1} END {print s}'")[0] cpu_name = clock_string[0] result = { "cpu_clock_current": cls.normalize_value(cpu_speed, "hz"), "cpu_clock_max": cls.normalize_value(cpu_max, 'hz'), "cpu_pct_used": cls.normalize_value(cpu_used, '%'), "cpu_name": cpu_name } return result
def exec_service(name, service_dir, host_port, service_port): docker_cleanup(name) script = os.path.join(base_dir(), "setup_service.sh") host_port = int(host_port) service_port = int(service_port) _, err, e = run_command('%s "%s" %d %d' % \ (script, name, host_port, service_port), service_dir) if e != 0: print(err) print('[*] Failed to execute the service.') else: print('[*] Service is up.')
def decrypt_exploit(encrypted_exploit_path, config, team, out_dir=None, \ expected_signer=None): if out_dir is None: out_dir = "exploit" rmdir(out_dir) tmpzip = "/tmp/gitctf_%s.zip" % random_string(6) tmpdir = "/tmp/gitctf_%s" % random_string(6) tmpgpg = "/tmp/gitctf_%s.gpg" % random_string(6) if expected_signer == None: decrypt_cmd = 'gpg -o %s %s' % (tmpzip, encrypted_exploit_path) else: instructor_id = config['teams']['instructor']['pub_key_id'] team_id = config['teams'][team]['pub_key_id'] expected_signer_id = config['individual'][expected_signer][ 'pub_key_id'] # Make keyring run_command("gpg -o %s --export %s %s %s" % (tmpgpg, \ expected_signer_id, instructor_id, team_id), os.getcwd()) decrypt_cmd = "gpg --no-default-keyring --keyring %s -o %s %s" \ % (tmpgpg, tmpzip, encrypted_exploit_path) _, err, r = run_command(decrypt_cmd, os.getcwd()) if r != 0: print "[*] Failed to decrypt/verify %s" % encrypted_exploit_path print err return None run_command('unzip %s -d %s' % (tmpzip, tmpdir), os.getcwd()) shutil.move(tmpdir, out_dir) rmfile(tmpzip) rmfile(tmpgpg) rmdir(tmpdir) return out_dir
def export_public_key(config, signer): signer_pubkey = config["individual"][signer]['pub_key_id'] cmd = "gpg --armor --export %s " % signer_pubkey output, err, ret = run_command(cmd, None) if ret != 0: print("[*] Failed to export public key") print(err) return None return output
def run_exploit(exploit_dir, container_name, timeout, log=None): log = print_and_log("[*] Running exploit", log) script = os.path.join(base_dir(), "launch_exploit.sh") cmdline = \ "%s %s %s %d %d" % \ (script, container_name, SERVICE_IP, SERVICE_PORT, timeout) output, err, e = run_command(cmdline, exploit_dir) if log is not None: log = log + output if e != 0: log = print_and_log("[*] Failed to run exploit", log) log = print_and_log(err, log) log = print_and_log("==========================", log) return None, log # Exploit prints out the flag string at the end. tokens = [_f for _f in output.split('\n') if _f] # Filter out empty strings flag_candidate = [_f for _f in tokens if _f][-1] # Read the last line return flag_candidate, log
def get_repo_name(): name = cmd.run_command('basename $(git remote get-url origin)') if '.git' in name: return name[0:len(name) - 5] else: return name
def checkout(branch): if 'remotes/origin/' in branch: cmd.run_command('git checkout ' + branch[len('remotes/origin/'):]) else: cmd.run_command("git checkout " + branch)
def delete_branch(branch): cmd.run_command('git branch -D ' + branch)
def list_branches(dir): external_path = os.path.join(base_dir(), "list_branches.sh") s, _, _ = run_command("%s \"%s\"" % (external_path, dir), os.getcwd()) branches = s.splitlines() return branches
def get_current_branch(): result = cmd.run_command("git rev-parse --abbrev-ref HEAD") return result.split("\n")[0].strip()
def unit_test(): project = cmd.run_command("pwd -P").split('/')[-1].replace('\n', '') branch = git_util.get_current_branch().replace('/', '%252F') link = 'job/idnowgmbh/job/' + project + '/job/' + branch print(link) open_jenkins(link)
def sync_scoreboard(scoreboard_dir): run_command('git reset --hard', scoreboard_dir) run_command('git pull', scoreboard_dir)
key_id = matches.group(2) net_id_folder = "{}/{}".format(destination, net_id) if not os.path.isdir(net_id_folder): mkdir(net_id_folder) issue_folder = "{}/{}/".format(net_id_folder, issue_id) create_or_empty_folder(issue_folder) with open(issue_folder + "answer.zip.pgp", "w") as f: f.write(body) with open(issue_folder + "pub_key.asc", "w") as f: f.write(comments[1]) run_command("gpg --import pub_key.asc", issue_folder) run_command("gpg -o answer.zip answer.zip.pgp", issue_folder) run_command("unzip answer.zip -d ./", issue_folder) rmfile(issue_folder + "answer.zip") shutil.make_archive("{}/{}".format(net_id_folder, issue_id), "zip", issue_folder) # GitHub has a limitation for how many requests we can make per minute time.sleep(random.randint(1, 3))
def get_net(cls): nic_info = {} if OsHelper.name() == "Windows": log.debug("Get windows stuff here...") net_data = run_command("wmic path Win32_PerfRawData_Tcpip_NetworkInterface get Name,BytesReceivedPersec," "BytesSentPersec,CurrentBandwidth") time.sleep(1) net_data2 = run_command("wmic path Win32_PerfRawData_Tcpip_NetworkInterface get Name,BytesReceivedPersec," "BytesSentPersec") net_data.pop(0) net_data2.pop(0) for line in net_data: info = line.split() info2 = net_data2.pop(0).split() tx1 = int(info2.pop(0)) tx2 = int(info.pop(0)) rx1 = int(info2.pop(0)) rx2 = int(info.pop(0)) tx = tx1 - tx2 rx = rx1 - rx2 nic_max = (int(info.pop(0)) * 1024) + .0 log.debug("Tx1 txt2 and rx1 and rx2 are %s and %s and %s and %s" % (tx1, tx2, rx1, rx2)) device = { "net_rx": rx, "net_tx": tx, "net_max": to_base(str(nic_max) + " KB") } interface = " ".join(info) nic_info[interface] = device elif OsHelper.name() == "Linux": log.debug("Getting 'nix net info") net_data = run_command("cat /proc/net/dev") net_data.pop(0) for line in net_data: info = line.split() interface = info[0].strip(":") net_max = run_command("ethtool eth0 | grep Speed:")[0].split(": ")[1].strip("/s") device = { "net_rx": int(info[1]), "net_tx": int(info[9]), "net_max": to_base(net_max) } nic_info[interface] = device elif OsHelper.name() == "MacOSX": net_data = run_command("netstat -ib") net_data.pop(0) for line in net_data: info = line.split() interface = info[0] tx = info[6] rx = info[9] log.debug("Values: %s and %s and %s" % (interface, tx, rx)) tx = int(tx) rx = int(rx) nic = nic_info.get(interface) or { "net_tx": 0, "net_rx": 0 } tx += nic['net_tx'] rx += nic['net_rx'] nic["net_tx"] = tx nic["net_rx"] = rx nic_info[interface] = nic nic_list = [] for interface, nic in nic_info.items(): nic["net_tx"] = cls.normalize_value(nic['net_tx']) nic["net_rx"] = cls.normalize_value(nic['net_rx']) nic["nic_name"] = interface nic_list.append(nic) nic_list = sorted(nic_list, key=lambda z: z['net_rx'], reverse=True) return nic_list
matches = re.match( r"My NetID is (\w+), ?([\w| ]+), and my pub key id is (\w+)", comments[0]['body']) net_id = matches.group(1) name = matches.group(2) key_id = matches.group(3) print( "Found github username [{}], student id [{}], student name [{}], and key id [{}]" .format(github_name, net_id, name, key_id)) config['individual'][github_name] = {'pub_key_id': key_id, 'team': net_id} # import public key public_key = comments[1]['body'] public_key_path = "/tmp/{}.key".format(github_name) f = open(public_key_path, "w") f.write(public_key) f.close() cmd = "gpg --import " + public_key_path run_command(cmd, None) issues = [(args.repo, args.issue, 0, int(time.time()))] for repo, num, id, gen_time in issues: process_issue(repo, num, id, config, gen_time, github, scoreboard)
def checkout(dir, br): _, err, r = run_command("git -C %s checkout -f %s" % (dir, br), os.getcwd()) if r != 0: print("[*] Failed to checkout the branch %s" % br) print err sys.exit()
def open_path(path): cmd.run_command('open ' + GRAYLOG + path)
def check_liveness(container_name, host_port): _, _, e = run_command('nc -z 127.0.0.1 %d' % host_port, None) if e != 0: print("[*] %s service is not running." % container_name) else: print("[*] %s service looks well." % container_name)
def open_jenkins(path): cmd.run_command("open " + JENKINS + path)
cmd.run("qvm start %s"%(vm_name)) # Command if len(sys.argv) > 2: remote_cmd = ' '.join(sys.argv[2:]) else: remote_cmd = None # Get username for the session user, vm_name = ssh.get_username(vm_name) # Get IP ip = network.get_ip(vm_name) if not ip: print "ERROR: Could not get the IP of %s"%(vm_name) raise SystemExit # SSH command line command = ssh.build_ssh_command(ip, user) if remote_cmd: command += ' %s'%(remote_cmd) # Make sure the SSH service is ready network.wait_vm_net_service (vm_name, 22) # Execute if not sys.stdin.isatty(): cmd.run_command(command, stdin=sys.stdin.read()) else: cmd.run(command)
import sys import cmd username = '******' input = sys.argv[1:] name = input[0] + '/' + username + '/SGN-' + input[1].upper() for text in input[2:]: name = name + '_' + text cmd.run_command('git checkout master') cmd.run_command('git pull') cmd.run_command('git checkout -b ' + name)
def run_command(command): codigo_resultado,texto_resultado=cmd.run_command(command) logs.append([command,codigo_resultado,texto_resultado]) return codigo_resultado,texto_resultado
def docker_cleanup(container_name): print "[*] Clean up container '%s'" % container_name script = os.path.join(base_dir(), "cleanup.sh") cmdline = "%s %s" % (script, container_name) run_command(cmdline, None)