Last active
March 3, 2017 06:00
-
-
Save intrd/ec66d765864dcee98d5166cee69fe83f to your computer and use it in GitHub Desktop.
Script to correctly answer prog200-alien_dna2 @ 3dsctf-2k16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Script to correctly answer prog200-alien_dna2 @ 3dsctf-2k16 | |
# @author intrd - http://dann.com.br/ | |
# @license Creative Commons Attribution-ShareAlike 4.0 International License - http://creativecommons.org/licenses/by-sa/4.0/ | |
# int_netcat.py - https://gist.github.com/intrd/00a39c83f752acf81775bfa9721e745a | |
import socket, re, sys, time, operator, difflib | |
sys.path.append("../../LIBS/") | |
from int_netcat import Netcat | |
def get_char_repeated(str1,str2): | |
a=0 | |
chars="".join(set(str2)) | |
check_string=str1 | |
for char in chars: | |
count = check_string.count(char) | |
if count >= 1: | |
print char | |
a+=1 | |
return a | |
## 2nd dna chall function (dany fix) | |
def dna_compare_fixed(samp1, samp2): | |
samp1len=len(samp1) | |
samp2len=len(samp2) | |
result=0 | |
xi=0 | |
for i in range(0,samp2len): | |
ii=samp2len | |
for i in range(0,samp2len): | |
out=samp2[xi:ii] | |
if len(out)<=1: break | |
if out in samp1 and len(out) >= result: | |
result = len(out) | |
#result+=1 | |
#return len(out) | |
ii-=1 | |
xi+=1 | |
if result<=1: | |
same=get_char_repeated(samp1,samp2) | |
if result==0: same=1 | |
result=same | |
return result | |
# offline tests | |
#res=dna_compare_fixed("AACAC","ACCC") #2 | |
#res=dna_compare_fixed("UGCUCA","ACAUG") #2 | |
#res=dna_compare_fixed("CCCACC","ACCCA") #4 | |
#res=dna_compare_fixed("UCCAA","UUAUC") #2 | |
#print res | |
#sys.exit(0) | |
nc = Netcat('209.190.1.131', 9002) | |
data=nc.read_until(':') | |
print(data) | |
number=re.search(r'type (.*):', data).group(1) | |
print(number) | |
nc.write(number + '\n') | |
data=nc.read() | |
for x in range(0, 201): | |
try: | |
sample1=re.search(r'Sample 01: (.*) - Sa', data).group(1) | |
print(sample1) | |
sample2=re.search(r'Sample 02: (.*)', data).group(1) | |
print(sample2) | |
answer=dna_compare_fixed(sample1,sample2) | |
print(sample1+"-"+sample2+"="+str(answer)) | |
nc.write(str(answer) + '\n') | |
except: | |
pass | |
data=nc.read() | |
print(data) | |
#time.sleep(1) | |
if "Leaving" in data: | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment