From ce01e712d5b0af26404a4947bbdc11c35348d3c2 Mon Sep 17 00:00:00 2001 From: Wang Yihang Date: Tue, 9 Jul 2019 20:29:52 +0800 Subject: [PATCH] [+] v2.1 --- SourceLeakHacker.py | 2 ++ lib/core/dispatcher.py | 9 ++++++--- lib/core/spider.py | 40 +++++++++++++++------------------------- lib/util/color.py | 10 +++++----- lib/util/logger.py | 18 ++++++++++++++++-- lib/util/output.py | 24 +++++++++++++----------- lib/util/terminal.py | 3 ++- 7 files changed, 59 insertions(+), 47 deletions(-) diff --git a/SourceLeakHacker.py b/SourceLeakHacker.py index 9403155..42db566 100644 --- a/SourceLeakHacker.py +++ b/SourceLeakHacker.py @@ -40,7 +40,9 @@ def main(): prompt.show(sys.argv[0]) exit(1) listFile = open('list.txt', 'r') + dispatcher.start(website, threadNumber, listFile, timeout) + output.asTable() output.asCSV("result.csv") diff --git a/lib/core/dispatcher.py b/lib/core/dispatcher.py index 8f82131..63d9e14 100644 --- a/lib/core/dispatcher.py +++ b/lib/core/dispatcher.py @@ -29,16 +29,19 @@ def start(website, threadNumber, listFile, timeout): logger.detail("%d tasks dispatched, starting threads..." % (len(threads))) # Start threads - counter = 0 + # counter = 0 for thread in threads: if context.CTRL_C_FLAG: break - # print("[%d / %d] Finished" % (counter, len(threads))) + # logger.detail("[%d / %d] Finished" % (counter, len(threads))) thread.start() + thread.join() while True: if context.CTRL_C_FLAG: break # -1 for main thread if ((len(threading.enumerate()) - 1) < threadNumber): break - counter += 1 + # counter += 1 + + diff --git a/lib/core/spider.py b/lib/core/spider.py index 9d2bfab..224d78c 100644 --- a/lib/core/spider.py +++ b/lib/core/spider.py @@ -14,47 +14,37 @@ def sniff(url, timeout): def check(url, timeout): try: - context.screenLock.acquire() - if timeout <= 0: timeout = 4 start_time = time.time() response = requests.head(url, timeout = timeout) + logger.correct(response.status_code) + end_time = time.time() code = response.status_code - content_length = int(response.headers["Content-Length"]) - content_type = response.headers["Content-Type"] + if "Content-Length" in response.headers: + content_length = response.headers["Content-Length"] + else: + content_length = "0" + if "Content-Type" in response.headers: + content_type = response.headers["Content-Type"] + else: + content_type = "UNKNOWN" time_used = end_time - start_time context.result[url] = { "code":code, "headers":response.headers, "time":time_used, + "Content-Length": content_length, + "Content-Type": content_type, } - - if (code / 100) == 1: - logger.http("[%d]\t%d\t%02f\t%s\t%s" % (code, content_length, time_used, content_type, url), code) - elif (code / 100) == 2: - logger.http("[%d]\t%d\t%02f\t%s\t%s" % (code, content_length, time_used, content_type, url), code) - # Some site use one response for pages not exists. - # Try to avoid that situation - if "404" in response.text: - logger.error(url + "\tMaybe every page same!") - elif (code / 100) == 3: - logger.http("[%d]\t%d\t%02f\t%s\t%s" % (code, content_length, time_used, content_type, url), code) - elif (code / 100) == 4: - logger.http("[%d]\t%d\t%02f\t%s\t%s" % (code, content_length, time_used, content_type, url), code) - elif (code / 100) == 5: - logger.http("[%d]\t%d\t%02f\t%s\t%s" % (code, content_length, time_used, content_type, url), code) - else: - logger.error("[%d]\t%d\t%02f\t%s\t%s" % (code, content_length, time_used, content_type, url)) + + logger.http("[%d]\t%s\t%02f\t%s\t%s" % (code, content_length, time_used, content_type, url), code) except Exception as e: - context.screenLock.acquire() - logger.error(e) - finally: + logger.detail(e) pass - context.screenLock.release() class Spider(threading.Thread): def __init__(self, url, timeout): diff --git a/lib/util/color.py b/lib/util/color.py index fa7c519..9685bbe 100644 --- a/lib/util/color.py +++ b/lib/util/color.py @@ -1,15 +1,15 @@ from termcolor import colored def colorProjection(code): - if (code / 100) == 1: + if int(code / 100) == 1: return ("blue", "on_grey") - elif (code / 100) == 2: + elif int(code / 100) == 2: return ("green", "on_grey") - elif (code / 100) == 3: + elif int(code / 100) == 3: return ("yellow", "on_grey") - elif (code / 100) == 4: + elif int(code / 100) == 4: return ("red", "on_grey") - elif (code / 100) == 5: + elif int(code / 100) == 5: return ("magenta", "on_grey") else: return ("cyan", "on_grey") diff --git a/lib/util/logger.py b/lib/util/logger.py index 379148d..6ab6144 100644 --- a/lib/util/logger.py +++ b/lib/util/logger.py @@ -1,18 +1,32 @@ from termcolor import colored from lib.util import color +from lib.context import context def error(content): + context.screenLock.acquire() print(colored(content, 'red', 'on_grey')) + context.screenLock.release() def correct(content): + context.screenLock.acquire() print(colored(content, 'green', 'on_grey')) + context.screenLock.release() + def detail(content): + context.screenLock.acquire() print(colored(content, 'blue', 'on_grey')) + context.screenLock.release() + def plain(content): - print(colored(content, 'cyan', 'on_grey')) + context.screenLock.acquire() + print(colored(content, 'white', 'on_grey')) + context.screenLock.release() + def http(content, code): + context.screenLock.acquire() color_config = color.colorProjection(code) - print(colored(content, color_config[0], color_config[1])) \ No newline at end of file + print(colored(content, color_config[0], color_config[1])) + context.screenLock.release() diff --git a/lib/util/output.py b/lib/util/output.py index a897596..b23155b 100644 --- a/lib/util/output.py +++ b/lib/util/output.py @@ -3,6 +3,7 @@ from lib.util import string from lib.util import color +from lib.util import logger from lib.context import context headers = ["Code", "Length", "Time", "Type", "URL"] @@ -11,21 +12,22 @@ def asTable(): table = prettytable.PrettyTable() table.field_names = headers for k, v in context.result.items(): - if v["code"] != 404: - table.add_row([ - # color.colorByStatusCode(v["code"], v["code"]), - v["code"], - v["headers"]["Content-Length"], - v["time"], - v["headers"]["Content-Type"], - string.fixLength(k, 0x20)] - ) + # if v["code"] != 404: + table.add_row([ + color.colorByStatusCode(v["code"], v["code"]), + # v["code"], + v["Content-Length"], + "%02f" % v["time"], + v["Content-Type"], + # string.fixLength(k, 0x20) + k, + ]) table.set_style(prettytable.MSWORD_FRIENDLY) - print(table) + logger.plain(table) def asCSV(filename): with open(filename, 'w') as f: cvs_writer = csv.writer(f) cvs_writer.writerow(headers) for k, v in context.result.items(): - cvs_writer.writerow([v["code"], v["headers"]["Content-Length"], v["time"], v["headers"]["Content-Type"], k]) \ No newline at end of file + cvs_writer.writerow([v["code"], v["Content-Length"], v["time"], v["Content-Type"], k]) \ No newline at end of file diff --git a/lib/util/terminal.py b/lib/util/terminal.py index f9fd2f0..4811d75 100644 --- a/lib/util/terminal.py +++ b/lib/util/terminal.py @@ -1,8 +1,9 @@ import subprocess import platform +import sys def clear(): if platform.system()=="Windows": subprocess.Popen("cls", shell=True).communicate() else: - print("\033c", end="") \ No newline at end of file + sys.stdout.write("\033c") \ No newline at end of file