def main(): url = "https://www.virustotal.com/vtapi/v2/file/report" try: with open("setting.conf", "r") as log_file: source_path = log_file.readline().strip() destination_path = log_file.readline().strip() log_path = log_file.readline().strip() apikey = log_file.readline().strip() print "Your source path is "+source_path print "Your destination path is "+destination_path print "Your log path is "+log_path print "Your API key is "+apikey except: print "Usage: You need to make setting.conf first." print "In the file, you should put this content in order:" print "source path" print "destination path" print "log path" print "apikey" print "./get_mal_result.py" sys.exit(0) print "Now let's read from "+source_path try: flist = Popen(["/bin/ls",source_path],stdout=PIPE).stdout.read().split("\n") flist.pop() except: print "Fail to load the source path" print "Please check the path!" sys.exit(0) for fn in flist: md5 = os.path.splitext(fn)[0] if "_" in md5: md5 = md5[0:-2] jsonMD5 = destination_path+md5+".json" parameters = {"resource":md5,"apikey":apikey} try: print "It's "+md5+" turns." data = urllib.urlencode(parameters) req = urllib2.Request(url, data) response = urllib2.urlopen(req) jsonResult = response.read() print "Now writing data into "+jsonMD5 with open(jsonMD5, 'w') as outfile: json.dump(jsonResult, outfile, sort_keys = True, indent = 4, ensure_ascii=False) except: print "Some problems happened with "+md5+"!" with open(log_path,"a") as file_handle: file_handle.write(md5+"\n") time.sleep(15)
def get_local_info(interface): # I f*****g miss ifconfig ''' Uses CLI commands to grab the local IP with subnet prefix (CIDR) and the MAC address of the NIC. The subnet prefix (CIDR) is a count of the 1 bits in the binary notation, this means we must convert it into dotted decimals to get the subnet mask. ''' mac = Popen(['cat', f'/sys/class/net/{interface}/address'], stdout=PIPE).communicate()[0].decode('utf-8').rstrip() get_lhost = Popen(['ip', 'a', 'show', interface], stdout=PIPE) lhost = Popen(['awk', '/inet.*brd/ {print $2}'], stdin = get_lhost.stdout, \ stdout = PIPE).communicate()[0].decode('utf-8') lhost = lhost.split('/') cidr_prefix = lhost[1] lhost = str(lhost.pop(0)) bin_netmask = '1' * int(cidr_prefix) + '0' * (32 - int(cidr_prefix)) netmask_parts = [] scannable_octets = 0 for octet_bit in range(0, len(bin_netmask), 8): bin_octet = bin_netmask[octet_bit:][:8] octet = int(bin_octet, 2) if octet != 255: scannable_octets += 1 netmask_parts.append(str(octet)) return lhost, mac, '.'.join(netmask_parts), calculate_hosts( cidr_prefix), cidr_prefix, scannable_octets
def available_images(request): running = is_running('/usr/local/bin/tklpatch-getimage') if len(running) > 0: return HttpResponseRedirect('/baseimages/getimage/') if os.path.exists(settings.TKLPATCH_BASEIMAGES_FILE): imagelist = Popen(['tklpatch-getimage','--list'],stdout=PIPE).communicate()[0] imagelist = imagelist.split("\n") imagelist.pop() #Remove empty element baseimagelist = list_images() for x in baseimagelist: image = x[:-4] try: imagelist.remove(image) except: pass else: imagelist = '' return render_to_response('baseimages/listimages.html',{"imagelist": imagelist}, context_instance=RequestContext(request))
def df(cmd): data = {} r1 = re.compile('^/(dev/|)') output = Popen([cmd], stdout=PIPE, shell=True).communicate()[0].splitlines() output.pop(0) for line in output: fields = line.split() key = r1.sub('', fields[0]).replace('/', '_') data[key] = { 'total': fields[1], 'used': fields[2], 'available': fields[3], 'percent_used': fields[4].replace('-', '0').replace('%', ''), 'mountpoint': fields[5] } return data
def update_gunicorns(): """ Updates the dict of gunicorn processes. Run the ps command and parse its output for processes named after gunicorn, building up a dict of gunicorn processes. When new gunicorns are discovered, run the netstat command to determine the ports they're serving on. """ global tick tick += 1 if (tick * screen_delay) % ps_delay != 0: return tick = 0 for pid in gunicorns: gunicorns[pid].update({"workers": 0, "mem": 0}) ps = Popen(PS_ARGS, stdout=PIPE).communicate()[0].split("\n") headings = ps.pop(0).split() name_col = headings.index("CMD") num_cols = len(headings) - 1 for row in ps: cols = row.split(None, num_cols) if cols and cols[name_col].startswith("gunicorn: "): is_worker = cols[name_col].startswith("gunicorn: worker") if is_worker: pid = cols[headings.index("PPID")] else: pid = cols[headings.index("PID")] if pid not in gunicorns: gunicorns[pid] = {"workers": 0, "mem": 0, "port": None, "name": cols[name_col].strip().split("[",1)[1][:-1]} gunicorns[pid]["mem"] += int(cols[headings.index("RSS")]) if is_worker: gunicorns[pid]["workers"] += 1 # Remove gunicorns that were not found in the process list. for pid in gunicorns.keys()[:]: if gunicorns[pid]["workers"] == 0: del gunicorns[name] # Determine ports if any are missing. if not any([g for g in gunicorns.values() if g["port"] is None]): return netstat = Popen(["netstat","-lp"], stdout=PIPE, stderr=PIPE).communicate()[0].split("\n") addr_pos = None pid_pos = None addr_heading = "Local Address" pid_heading = "PID/Program name" for row in netstat : if addr_heading in row and pid_heading in row: addr_pos = row.index(addr_heading) pid_pos = row.index(pid_heading) if addr_pos is not None: pid = row[pid_pos:].split("/")[0] if pid in gunicorns: port = row[addr_pos:].split(" ", 1)[0].split(":")[1] gunicorns[pid]["port"] = port
def get_data(self): command = 'ss -tuna' data = self.gendict() output = Popen([command], stdout=PIPE, shell=True)\ .communicate()[0]\ .splitlines() output.pop(0) for line in output: fields = line.split() proto = fields[0] if proto == 'tcp': state = fields[1].lower() data[proto][state].setdefault('value', 0) data[proto][state]['value'] += 1 else: data[proto].setdefault('value', 0) data[proto]['value'] += 1 return dict(data)
def preprocessFile(filename): global CASource, protectedFunctions headerfile = filename.split('/')[-1][:-2] # Remove comments data = Popen(["gcc", "-fpreprocessed", "-dD", "-E", filename], stdout=PIPE).communicate()[0].splitlines(True) if not len(data): print("Error: Not a valid input file") sys.exit(0) # preprocessor comments data.remove(data[0]) while('#include' in data[0]): CASource[0] += data.pop(0) CASource[0] += '\n#include "autogen_ca_header.h"\n\n' data[0] += "typedef struct aes_key_struct {\n\tint dummy;\n} AES_KEY;\ntypedef struct rsa_key_struct {\n\tint dummy;\n} RSA;\ntypedef unsigned long int uint32_t;\ntypedef unsigned long size_t;\n" # Search secure annotations for line in reversed(data): if '#pragma secure' in line: name = line.split()[-1] if ' function ' in line: protectedFunctions.append(FunctionObject(name)) # Currently appends a name, not an object elif ' global ' in line: globalVariables.append(name) data.remove(line) for i in range(len(data)-1, -1, -1): if '#pragma shared var' in data[i]: funcIndex = i # Find name of the function while '#pragma shared var' in data[funcIndex]: funcIndex -= 1 # Determine whether the function is marked as secure for func in protectedFunctions: if func.name in data[funcIndex]: obj = ParameterObject(data[i].split()[-1], '', 1) sharedVariables.append((func.name, obj)) elif '#pragma shared header' in data[i]: TASource.append('\n#include "'+line.split()[-1]+'"\n') protectedFunctions = protectedFunctions[::-1] return ''.join(data)
def update_gunicorns(): """ Updates the dict of gunicorn processes. Run the ps command and parse its output for processes named after gunicorn, building up a dict of gunicorn processes. When new gunicorns are discovered, run the netstat command to determine the ports they're serving on. """ global tick tick += 1 if (tick * screen_delay) % ps_delay != 0: return tick = 0 for pid in gunicorns: gunicorns[pid].update({"workers": 0, "mem": 0}) ps = Popen(PS_ARGS, stdout=PIPE).communicate()[0].split("\n") headings = ps.pop(0).split() name_col = headings.index(cmd_heading) num_cols = len(headings) - 1 for row in ps: cols = row.split(None, num_cols) if cols and "gunicorn: " in cols[name_col]: if "gunicorn: worker" in cols[name_col]: is_worker = True else: is_worker = False if is_worker: pid = cols[headings.index("PPID")] else: pid = cols[headings.index("PID")] if pid not in gunicorns: gunicorns[pid] = {"workers": 0, "mem": 0, "port": None, "name": cols[name_col].strip().split("[",1)[1].split("]",1)[:-1]} gunicorns[pid]["mem"] += int(cols[headings.index("RSS")]) if is_worker: gunicorns[pid]["workers"] += 1 # Remove gunicorns that were not found in the process list. for pid in gunicorns.keys()[:]: if gunicorns[pid]["workers"] == 0: del gunicorns[pid] # Determine ports if any are missing. if not [g for g in gunicorns.values() if g["port"] is None]: return for (pid, port) in ports_for_pids(gunicorns.keys()): if pid in gunicorns: gunicorns[pid]["port"] = port
def rcfile(rcfile, debug=False): """ Execute commands using a rcfile. """ p = os.path.join("testsploit", rcfile) if not os.path.isfile(p): raise ValueError("Bad rc file") cmd = "cd testsploit && python main.py --rcfile %s" % rcfile if debug: cmd += " -v" out, err = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True).communicate() out = re.split(r"\+{10,}\s.*?\s\+{10,}", out.decode())[1:] err = "\n".join(l for l in err.decode().splitlines() if not l.startswith("Warning: ")).strip() c = [] with open(p) as f: for l in f: l = l.strip() try: c.append((l, re.sub(r"\x1b\[\??\d{1,3}[hm]", "", out.pop(0)).strip())) except IndexError: c.append((l, None)) if c[-1][0] == "exit": c.pop(-1) return c, err
def eeprom_afc(tela_leds,ip_crate): tela_leds.ui.progressBar.setValue(0) tela_leds.repaint() QApplication.processEvents() #escrita! print("Iniciou o processo de Escrita!") posicao_memoria=[] valor_escrito=[] posicao_memoria_str=[] i=0 while (i<256): posicao_memoria.append(format(i,'02x')) valor_escrito.append(format(i,'02x')) posicao_memoria_str.append("0x"+posicao_memoria[i]) comando1="ipmitool -I lan -H "+str(ip_crate)+" -P '' -T 0x82 -m 0x20 -t 0x76 raw 0x30 0x00 0x03 0x00 0x08 0x02 0x"+str(valor_escrito[i])+" 0x"+str(valor_escrito[i])+"" #teste manual de escrita " ipmitool -I lan -H 10.0.18.14 -P '' -T 0x82 -m 0x20 -t 0x76 raw 0x30 0x00 0x03 0x00 0x08 0x10 0x02 0x0f " #teste manual de leitura! " ipmitool -I lan -H 10.0.18.14 -P '' -T 0x82 -m 0x20 -t 0x76 raw 0x30 0x00 0x03 0x00 0x08 0x01 0x02 0x10 " command_stdout = Popen(comando1,shell=True,stdin=PIPE,stdout=PIPE,stderr=PIPE).communicate()[0] command_stdout=command_stdout.splitlines() tela_leds.ui.progressBar.setValue((i+1)*70/256) tela_leds.repaint() QApplication.processEvents() i=i+1 print("Fim da Escrita") #Padrao a comparar com a memoria comando_enviado=[] j=0 while(j<241): posicao_memoria=" "+valor_escrito[0+j]+" "+valor_escrito[1+j]+" "+valor_escrito[2+j]+" "+valor_escrito[3+j]+" "+valor_escrito[4+j]+" "+valor_escrito[5+j]+" "+valor_escrito[6+j]+" "+valor_escrito[7+j]+" "+valor_escrito[8+j]+" "+valor_escrito[9+j]+" "+valor_escrito[10+j]+" "+valor_escrito[11+j]+" "+valor_escrito[12+j]+" "+valor_escrito[13+j]+" "+valor_escrito[14+j]+" "+valor_escrito[15+j] comando_enviado.append(posicao_memoria) j=j+16 print("Iniciou o processo de LEITURA!") #leitura! c=[] resultado_lido=[] f=0 i=0 while (i<16): c.append(format(f,'02x')) comando1="ipmitool -I lan -H "+str(ip_crate)+" -P '' -T 0x82 -m 0x20 -t 0x76 raw 0x30 0x00 0x03 0x00 0x08 0x01 0x"+str(c[i])+" 0x10" command_stdout = Popen(comando1,shell=True,stdin=PIPE,stdout=PIPE,stderr=PIPE).communicate()[0] command_stdout=command_stdout.splitlines() s1 = command_stdout.pop(1).decode("utf-8") teste=str(command_stdout).replace("[","") teste=str(teste).replace("]","") teste=str(teste).replace("'","") teste=teste[4:] resultado_lido.append(str(teste)+str(s1)) i=i+1 f=f+16 tela_leds.ui.progressBar.setValue((i+1)*30/16+70) tela_leds.repaint() QApplication.processEvents() print("Fim da Leitura") aux_lido=[] for i in range (0,len(resultado_lido)): aux_lido.append(resultado_lido[i].split()) valores_lidos=[] for i in range (0,len(aux_lido)): for j in range (0, len(aux_lido[i])): valores_lidos.append(aux_lido[i][j]) aux_escrito_padrao=[] for i in range (0,len(comando_enviado)): aux_escrito_padrao.append(comando_enviado[i].split()) valores_escrito_padrao=[] for i in range (0,len(aux_escrito_padrao)): for j in range (0, len(aux_escrito_padrao[i])): valores_escrito_padrao.append(aux_escrito_padrao[i][j]) eeprom_total_valores=len(valores_escrito_padrao) eeprom_valores_problemas=0 if(resultado_lido==comando_enviado): print("MEMORY TEST SUCCESS!") eeprom_afc_aprovacao="OK" eeprom_afc_result="EEPROM_AFC - RESULTADO DO TESTE: OK" else: print("MEMORY TEST FAILED!") eeprom_afc_aprovacao="FAIL" eeprom_afc_result="EEPROM_AFC - RESULTADO DO TESTE: FAIL" for i in range (0, eeprom_total_valores): if(valores_escrito_padrao[i]!=valores_lidos[i]): eeprom_valores_problemas=eeprom_valores_problemas+1 eeprom_valores_corretos= eeprom_total_valores-eeprom_valores_problemas if (eeprom_afc_aprovacao=="OK"): tela_leds.ui.kled_EEPROM.setState(1) tela_leds.ui.kled_EEPROM.setColor(QtGui.QColor(0, 255, 0)) tela_leds.repaint() QApplication.processEvents() print("Led EEPROM_AFC",tela_leds.repaint()) print("Led EEPROM_AFC",QApplication.processEvents()) else: tela_leds.ui.kled_EEPROM.setState(1) tela_leds.ui.kled_EEPROM.setColor(QtGui.QColor(255, 0, 0)) tela_leds.repaint() QApplication.processEvents() print("Led EEPROM_AFC",tela_leds.repaint()) print("Led EEPROM_AFC",QApplication.processEvents()) tela_leds.ui.progressBar.setValue(100) tela_leds.repaint() QApplication.processEvents() #print("Memória Acessada") #print(posicao_memoria_str) #print("Valores Escritos") #print(valores_escrito_padrao) #print("Valores Lidos") #print(valores_lidos) return (eeprom_afc_aprovacao,eeprom_afc_result,eeprom_total_valores,eeprom_valores_corretos,eeprom_valores_problemas,posicao_memoria_str,valores_escrito_padrao,valores_lidos)
# -*- coding: iso-8859-15 -*- from sys import argv from subprocess import Popen, PIPE from shlex import split from mailer import MailTransport, Email # list files files = Popen(split('find "'+argv[1]+'" -name "*.exe"'), stdout=PIPE).communicate()[0].strip() open('exe.list','w').write(files) files = files.split('\n') for i in range(len(files)): if files[i] == '': files.pop(i) # scan two files at once def scan(filename): return Popen(split('clamscan --infected --no-summary "'+filename+'"'), stdout=PIPE) def sendmail(log): Email(From='Kafka <*****@*****.**>', To='*****@*****.**', Subject='clamscan @ Kafka: FOUND', Text=log).send( MailTransport(Account='*****@*****.**') ) log = '' found = False while len(files) > 0: p1 = scan(files.pop()) p2 = None if len(files) > 0:
def exonerate(self,qryfas, genome, model,exonerate='exonerate',bestn=0): ''' Runs exonerate and parses output into lists for processing. { query: {'gff':[outputlines], 'cigar':[outputlines], 'alignment':[outputlines], 'vulgar':[[headerlist], {header:value}, {header:value}, ...] } ''' try:### ~ [1] Setup ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ### EXFILE = None exfile = '%s.%s' % (self.baseFile(),model) # Used in memsaver mode query_dic = {} header_list = ['query_id', 'query_start', 'query_end', 'query_strand', 'target_id', 'target_start', 'target_end', 'target_strand', 'score', '<label, query_length, target_length> triplets'] excmd = [exonerate, qryfas, genome, '--showtargetgff', '--showcigar'] if model: excmd += ['--model', model] if bestn: excmd += ['--bestn', '%d' % bestn] if self.getStrLC('ExOpt'): excmd += string.split(self.getStr('ExOpt')) self.printLog('#RUN',string.join(excmd)) extext = [] if self.getBool('MemSaver'): gzfile = '%s.gz' % exfile if rje.exists(gzfile): self.gUnzip(gzfile) if rje.exists(exfile) and not self.force(): self.printLog('#EXFILE','Found %s (force=F). Assuming complete.' % exfile) else: rje.backup(self,exfile) self.printLog('#SAVER','memsaver=T: Exonerate output directed to %s.' % exfile) EXFILE = open(exfile,'w') if subprocess.call(excmd, stdout=EXFILE): raise IOError('Exonerate call did not complete!') EXFILE.close() self.printLog('#EXFILE','%s generated.' % exfile) EXFILE = open(exfile,'r') else: extext = Popen(excmd, stdout=PIPE).stdout.readlines() output_format = '' while extext or EXFILE: #line = process.stdout.readline().rstrip() if EXFILE: line = EXFILE.readline() if not line: break line = rje.chomp(line) else: line = rje.chomp(extext.pop(0)) if line: if line.startswith(' Query:'): query = line.split(':', 1)[1].split(' ')[1] #for q in rje.sortKeys(query_dic): # self.bugPrint('%s: %s' % (q,rje.sortKeys(query_dic[q]))) #self.debug(query) if line == 'C4 Alignment:': output_format = 'alignment' elif line == '# --- START OF GFF DUMP ---': output_format = 'gff' elif line.startswith('vulgar:'): output_format = 'vulgar' fields = line.split(' ', 10)[1:] if output_format in query_dic[query]: query_dic[query][output_format].append({}) else: query_dic[query][output_format] = [header_list, {}] for header, field in zip(header_list, fields): query_dic[query][output_format][-1][header] = field #self.debug(query_dic[query][output_format]) elif line.startswith('cigar:'): output_format = 'cigar' if output_format in query_dic[query]: query_dic[query][output_format].append(line.replace('cigar: ', '')) else: query_dic[query][output_format] = [line.replace('cigar: ', '')] elif line == '------------' or line.startswith('Command line:') or line.startswith('Hostname:') or line == '# --- END OF GFF DUMP ---' or line == '#' or line.startswith('-- completed exonerate analysis'): pass elif output_format: if query in query_dic: if output_format in query_dic[query]: query_dic[query][output_format].append(line) else: query_dic[query][output_format] = [line] else: query_dic[query] = {output_format:[line]} #elif process.poll() is not None: # break elif output_format == 'alignment': try: query_dic[query][output_format].append(line) except: pass self.vPrint(line,v=1) if EXFILE: EXFILE.close() if self.getBool('Cleanup'): os.unlink(exfile) self.printLog('#CLEAN','%s deleted.' % exfile) elif self.getBool('GZip'): self.gZip(exfile) return query_dic except: self.errorLog('%s.exonerate error' % self.prog()); raise