이때 침착하게 다음 스탭을 따라보자.
pip install crytpto pip install pycrypto
이렇게 설치하였나?
그럼 당신은 실패하였다........
crypto 패키지를 깔게 되면 cipher (lower case)가 생성되고 참조가 안된다 ㅡㅡ
pip uninstall crypto pip uninstall pycrypto pip install pycrypto
반드시 하나만! 해주자. 내생각에는 pip 이슈인것같다.
pip install crytpto pip install pycrypto
pip uninstall crypto pip uninstall pycrypto pip install pycrypto
#!/usr/bin/env python
# -*- coding: utf8 -*-
import re
from subprocess import call
path = "/var/log/mysql/"
log = "error.log"
logContents = ""
excludeText = "etc ips............................"
with open("{}{}".format(path,log)) as f:
logContents = f.read()
extractedIP = {}
for ip in re.findall( r'\'?\'@\'[0-9]+(?:\.[0-9]+){3}\'', logContents):
ip = ip.replace('@','').replace('\'','')
try:
if type(extractedIP[ip]):
extractedIP[ip] = extractedIP[ip] + 1
except:
extractedIP[ip] = 1
for index in extractedIP:
if extractedIP[index] > 2 and index not in excludeText:
call("iptables -A INPUT -s {} -j DROP".format(index), shell=True)
f = open("{}{}".format(path,log),"w")
f.write('')
f.close()
print "Done"
2016-02-07T18:46:47.070457Z 519468 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:47.330309Z 519469 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:47.521742Z 519470 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:47.670816Z 519471 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:47.796906Z 519472 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:47.915819Z 519473 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:48.037179Z 519475 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:48.146458Z 519476 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:48.253486Z 519477 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:48.355799Z 519478 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:48.470777Z 519480 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:48.670311Z 519481 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:48.891320Z 519482 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:49.034734Z 519483 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:49.167179Z 519486 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:49.361079Z 519487 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:49.555965Z 519489 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES) 2016-02-07T18:46:49.800100Z 519490 [Note] Access denied for user 'root'@'180.97.215.141' (using password: YES)
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCcuHEVMRqY/Co/RJ5o5RTZmpl6sZ7U6w39WAvM7Scl7nGvr5mS4MRRIDaoAZpw7sPjmBHz2HwvAPYGCekcIVk8Xzc3p31v79fWeLXXyxts0jFZ8YZhYMZiugOgCKvRIs63DFf1gFoM/OHUyDHosi8E6BOi7ANqupScN8cIxDGsXMFr4EbQn4DoFeRTKLg5fHL9qGamaXXZRECkWHmjFYUZGjgeAiSYdZR49X36jQ6nuFBM18cEZe5ZkxbbtubnbAOMrB52tQX4RrOqmuWVE/Z0uCOBlbbG+9sKyY9wyp/aHLnRiyC8GBvbrZqQmyn9Yu1zBp3tY8Tt6DWmo6BLZV4/ crack@redis.io
iptables -A INPUT -p tcp -s IP --dport PORT -j ACCEPT; iptables -A INPUT -p tcp --dport PORT -j DROP iptables -nvL
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os,sys,timeit,string,base64
from Crypto.Cipher import AES,DES
__author__ = "Song Myeong-Uk"
__date__ = "2015.05.27"
#global value
md5_keys = {}
target_plaintext = ""
encrypted_plaintext = ""
start_time = timeit.default_timer() #when program start
#@func: download_data
#@desc: If there is no md5 hash passwords, download it
def download_data():
print "[-] passwords.txt not found! downloading now..."
url = "http://www.ece.ubc.ca/~hyoung/passwords.txt"
urllib.urlretrieve(url, 'passwords.txt')
print "[-] passwords.txt download complete"
#@func: save_input_data
#@desc: Insert input data to global variable
def save_input_data():
global target_plaintext
global encrypted_plaintext
i = 0
with open('PlaintextCiphertext.txt', 'r') as f:
i = 0
for line in f:
line = line.replace("\r\n","").replace("\n","")
if i is 0:
target_plaintext = line
elif i is 1:
encrypted_plaintext = line
i = i + 1
print "[-] Done!"
#@func: download_data
#@desc: To faster calculate. insert file data in memory instead IO system
def md5_to_memory():
global md5_keys
with open('passwords.txt', 'r') as f:
for line in f:
key_hash = line.split(" ")[0]
password = line.split(" ")[1].replace("\r\n","").replace("\n","")
md5_keys[key_hash] = password
print "[-] Done!"
#@func: DES_encrypt
#@param hash: first selected key of text file
#@desc: Simple DES(ECB) encryptor. return encrypted text
def DES_encrypt(hash):
global target_plaintext
des = DES.new(hash[:8], DES.MODE_ECB)
text = pad(target_plaintext,8)
cipher_text = des.encrypt(text)
return cipher_text
#@func: AES_encrypt
#@param DESed: DES encrypted text
#@param hash: Second selected key of text file
#@desc: Simple AES(CBC) Encryptor. return encoded base64 text
def AES_encrypt(DESed,hash):
global target_plaintext
#128 bits = 16bytes
mode = AES.MODE_CBC
encryptor = AES.new(hash, mode, IV='\x00'*16)
text = pad(DESed,16)
ciphertext = encryptor.encrypt(text)
return ciphertext.encode('base64')
def s2b(x):
return ''.join(c.encode('hex') for c in x)
#@func: decrypt
#@param enc: encrpyted text
#@param k1: First selected key of text file
#@param k2: First selected key of text file
#@desc: just test case
def decrypt(enc,k1,k2):
# enc = "W7n515icc+1dW5+82CYgPOGyFqgaFLA0FTCgB/jw1DZBeKUUF2h4z7yRWb03ZIeE"
# k1 = "0001245350b5eb0a1548fc6d27d3b4d1"
# k2 = "00009965"
text = enc
dec = AES.new(k1, AES.MODE_CBC, IV='\x00'*16)
des = DES.new(k2[:8], DES.MODE_ECB)
return unpad(des.decrypt(pad(unpad(dec.decrypt(text.decode('base64'))),8)))
#@func: pad
#@param x: text
#@param b: limit
#@desc: add null character to text untill b
def pad(x,b):
pad1 = lambda s: s + (b - len(s) % b) * chr(b - len(s) % b)
return pad1(x)
def unpad(x):
try:
unpad1 = lambda s : s[0:-ord(s[-1])]
return unpad1(x)
except Exception,err:
return x
#@func: progress
#@desc: execute calc when it ready
def progress():
global md5_keys
try:
for hash in md5_keys:
for hash2 in md5_keys:
DESed_text = DES_encrypt(hash)
if encrypted_plaintext is AES_encrypt(DESed_text,hash2):
print "[-] find keys ... !!"
print md5_keys[hash]
print md5_keys[hash2]
raise BreakAllTheLoops()
else:
#debug to print. you can pass it
#pass
print "[-] Trying key : {} - {},{}".format(AES_encrypt(DESed_text,hash2),md5_keys[hash],md5_keys[hash2]).replace("\r\n","").replace("\n","")
print "[-] Can not find keys ... "
except BreakAllTheLoops:
pass
# for hash in md5_keys:
# for hash2 in md5_keys:
# if target_plaintext is decrypt(encrypted_plaintext,hash2,hash):
# print hash
# print hash2
# main execution
if __name__ == "__main__":
print "[+] starting {}...".format(os.path.basename(__file__))
# check if data file exists
#check input file exists
cipher_exists = os.path.isfile('PlaintextCiphertext.txt')
print "[+] checking input file: PlaintextCiphertext.txt"
if not cipher_exists:
print "[+] PlaintextCiphertext.txt not found!"
sys.exit()
else:
print "[-] Done!"
print "[+] checking password data: passwords.txt"
txt_exists = os.path.isfile('passwords.txt')
if not txt_exists:
download_data()
else:
print "[-] Done!"
print "[+] Process init ... [1/2]"
save_input_data()
print "[+] Process init ... [2/2]"
md5_to_memory()
print "[+] Start processing ... "
print "[-] Target plaintext : {}".format(target_plaintext)
print "[-] Target Encrpyted key : {}".format(encrypted_plaintext)
progress()
stop_time = timeit.default_timer() #when program end
print "Done!!"
print "Program Run Time : " + str(stop_time - start_time)
#!/usr/bin/env python # -*- coding: utf-8 -*- import os, sys, time, base64, string, urllib from Crypto.Cipher import AES, DES from Crypto.Hash import MD5 # author information __author__ = "Song Myeong-Uk" __email__ = "mwsong@imtl.skku.ac.kr" __date__ = "2015.05.27" # constants DATA_FILE_URL = "http://www.ece.ubc.ca/~hyoung/passwords.txt" DATA_FILE_NAME = "passwords.txt" INPUT_FILE_NAME = "PlaintextCiphertext.txt" OUTPUT_FILE_NAME = "keys.txt" # global variables md5_keys = {} target_plaintext = "" encrypted_plaintext = "" DES_keys = {} AES_keys = {} # pad value with given block size: 16 def pad(x): BLOCK_SIZE = 16 if len(x) == BLOCK_SIZE: return x c = (len(x) // BLOCK_SIZE) + 1 c *= BLOCK_SIZE return "{:\x00<{l}}".format(x, l=c) # returns value of md5 for given value def hashMD5 (v): m = MD5.new(v) return m.digest() # download base md5 hash password list from url def download_data(): global DATA_FILE_URL, DATA_FILE_NAME print "[-] {} not found! downloading now...".format(DATA_FILE_NAME) urllib.urlretrieve(DATA_FILE_URL, DATA_FILE_NAME) print "[-] {} download complete".format(DATA_FILE_NAME) # calculate run time def print_time(t): diff = time.time() - t print "[*] total run time: {:.4f} seconds".format(diff) # load data from input file def save_input_data(): global target_plaintext, encrypted_plaintext global INPUT_FILE_NAME with open(INPUT_FILE_NAME, 'r') as f: data = [ l.strip() for l in f.readlines() ] target_plaintext = data[0] encrypted_plaintext = data[1] # load md5 data into dictionary def md5_to_memory(): global md5_keys global DATA_FILE_NAME with open(DATA_FILE_NAME, 'r') as f: data = [ l.strip() for l in f.readlines() ] for line in data: line = line.split(' ') md5_keys[line[0]] = line[1] # encrypts plaintext with DES using different keys def plaintext_to_des(): global md5_keys, DES_keys for k in md5_keys: KEY = DES_encrypt(hashMD5(md5_keys[k])[:8]).encode('hex')[:64] DES_keys[KEY] = md5_keys[k] # decrypts ciphertext with AES128 using different keys def enc_to_decrypt(): global encrypted_plaintext, md5_keys, AES_keys, DES_keys for k in md5_keys: KEY = AES_decrypt(encrypted_plaintext, hashMD5(md5_keys[k])).encode('hex')[:64] AES_keys[KEY] = md5_keys[k] try: if DES_keys[KEY]: print "[!] solution key found" print "[-] key1: {}".format(DES_keys[KEY]) print "[-] key2: {}".format(AES_keys[KEY]) write_key(DES_keys[KEY], AES_keys[KEY]) else: print "[!] solution key not found" except Exception, err: pass # writes found key to file def write_key(k1, k2): global OUTPUT_FILE_NAME print "[+] writing key file...", with open(OUTPUT_FILE_NAME, 'wb') as f: f.write(k1 + '\n' + k2) print "done" # DES-ECB encrypt with given key def DES_encrypt(key): global target_plaintext return DES.new(key, DES.MODE_ECB).encrypt(pad(target_plaintext)) # AES-CBC decrypt text with given key def AES_decrypt(text, key): return AES.new(key, AES.MODE_CBC, IV='\x00'*16).decrypt(base64.b64decode(text)) # main execution block def main(): print "[+] starting {}...".format(os.path.basename(__file__)) # time function execution start_time = time.time() # check if base data file exists txt_exists = os.path.isfile(DATA_FILE_NAME) print "[+] checking password data: {}".format(DATA_FILE_NAME) if not txt_exists: download_data() # check if input file exists cipher_exists = os.path.isfile(INPUT_FILE_NAME) print "[+] checking input file: {}".format(INPUT_FILE_NAME) if not cipher_exists: print "[!] {} not found! terminating...".format(INPUT_FILE_NAME) print_time(start_time) sys.exit() # load input data print "[+] loading input file data..." save_input_data() # load md5 to memory print "[+] preparing md5 dictionary..." md5_to_memory() # DES encrypt print "[+] processing with DES... [1/2]" plaintext_to_des() # AES decrypt print "[+] processing with AES... [2/2]" enc_to_decrypt() # time execution print_time(start_time) if __name__ == "__main__": main()
i = 1
with open("file.txt") as f:
for line in f:
raw_pw = line.split(":")[0].replace('\r\n','').replace('\n','')
if len(raw_pw) < 20 and len(raw_pw) >= 8:
if re.search("[\!\-\~]",raw_pw) > -1:
if re.search("[0-9]",raw_pw) > -1:
if re.search("[A-Z]",raw_pw) > -1:
hash_text = hashlib.sha256(raw_pw).hexdigest()
fp = open("aa.out","a")
fp.write(str(i)+":"+raw_pw+":"+hash_text+"\r\n")
fp.close()
i += 1
import timeit
start = timeit.default_timer()
i = 1
with open("hashedPasswords.txt") as target:
for line in target:
user = line.split(":")[1].replace("\r\n","")
target_word = line.split(":")[2].replace("\r\n","")
with open("out_a.out") as dic:
for dic_word in dic:
if target_word == dic_word.split("|")[1].replace("\r\n",""):
findit = dic_word.replace("\r\n","")
fp = open("Passwords.txt","a")
fp.write(str(i)+":"+user+":"+findit.split("|")[0]+"\r\n")
fp.close()
i+=1
break
stop = timeit.default_timer()
print "Program Run Time : " + str(stop - start)
#!/usr/bin/env python # Author : Myeong-Uk (mwsong@imtl.skku.ac.kr) # Date : 2015. 03. 30 # Desc : Python program to dictionary attack SHA256 # Dependency : out_c.out (Password dictionary), hashedPasswords.txt(input) # Command : <python find.py> # OUTPUT : Passwords.txt import timeit,os from multiprocessing import Process,Lock start_time = timeit.default_timer() #when program start def do_work(type): hash_count = 0 with open('aa.out') as target: for line in target: findit = "" if type == "1" and hash_count%2 == 0: findit = search_word(line) elif type== "2" and hash_count%2 > 0: findit = search_word(line) hash_count +=1 if findit != "" and findit != None: user = findit.split("|")[0] crackedPassword = findit.split("|")[1] fp = open("Passwords_tmp.txt","a") fp.write(user+":"+crackedPassword+"\r\n") fp.close() def search_word(word): user = str(word.split(":")[1].replace("\r\n","")) target_hash = word.split(":")[2].replace("\r\n","") with open("out_c.out") as dic: for dic_line in dic: dictionary_hash = dic_line.split("|")[1].replace("\r\n","") dictionary_word = dic_line.split("|")[0].replace("\r\n","") if target_hash == dictionary_hash: return user + "|" + dictionary_word break def numbering(): i = 0 with open("Passwords_tmp.txt") as dic: for find_word in dic: fp = open("Passwords.txt","a") fp.write(str(i)+":"+find_word) fp.close() i += 1 print str(i) + " Password(s) found." if __name__ == '__main__': pr1 = Process(target=do_work,args=(str(1))) pr2 = Process(target=do_work,args=(str(2))) pr1.start() pr2.start() pr1.join() pr2.join() stop_time = timeit.default_timer() #when program end print "Dictionary matching done" print "Numbering.." numbering() os.remove("Passwords_tmp.txt") print "Done!!" print "Program Run Time : " + str(stop_time - start_time)
import timeit,re
from multiprocessing import Process
start_time = timeit.default_timer() #when program start
pass_dump = ""
hash_dump = ""
def init():
global pass_dump, hash_dump
f = open("out_c.out","r")
pass_dump = f.read()
f = open("aa.out")
hash_dump = f.read()
def do_work():
lines = hash_dump.split("\r\n")
for i in lines:
search_word(i.split(":")[2])
def search_word(word):
match = re.search(r'.*'+word,pass_dump)
if match:
print match.group(0)
if __name__ == '__main__':
init()
do_work()
stop_time = timeit.default_timer() #when program end
print "Dictionary matching done"
print "Numbering.."
#numbering()
#os.remove("Passwords_tmp.txt")
print "Done!!"
print "Program Run Time : " + str(stop_time - start_time)
import timeit
start_time = timeit.default_timer() #when program start
pass_dump = {}
hash_dump = {}
count = 0
def init():
global pass_dump, hash_dump
with open("dictionary.out","r") as dic:
for a in dic:
key = a.split("|")[1].replace("\r\n","").replace("\n","") #hash
value = a.split("|")[0] #original text
pass_dump[key] = value
with open("hashedPasswords.out","r") as dic:
for a in dic:
key = str(a.split(":")[2].replace("\r\n","").replace("\n","")) #hash
value = a.split(":")[1] #user
hash_dump[key] = value
print "Init finished.."
def do_work():
global hash_dump,pass_dump,count
for a in hash_dump:
try:
if pass_dump[a]:
count += 1
fp = open("Passwords.txt","a")
fp.write(str(count)+":"+hash_dump[a]+":"+pass_dump[a]+"\r\n")
fp.close()
except KeyError:
pass
def search_word(word):
match = re.search(r'.*'+word,pass_dump)
if match:
print match.group(0)
if __name__ == '__main__':
init()
do_work()
stop_time = timeit.default_timer() #when program end
print "Done!!"
print str(count) + " Password(s) found. Recorded at Passwords.txt"
print "Program Run Time : " + str(stop_time - start_time)