sihunh / yarateste

웹사이트 내 Yara Rule를 이용해 webshell을 탐지하는 툴입니다 및 사용자가 다운로드 받은 파일에 대해 검사를 진행합니다
0 stars 0 forks source link

스테가노그래피 #5

Open sihunh opened 2 years ago

sihunh commented 2 years ago

ps 2 에서 숨겨진 파일 찾을때 쓸거임

import os from socket import from math import ceil from sig import import itertools import argparse

def hex_group_formatter(iterable): chunks = [iter(iterable)] 4 # (header의 perv_size, size) 청크 4개씩 분할 32byte return ' '.join( ' '.join(format(x, '0>2x') for x in chunk) # 데이터 바꾸는 부분 for chunk in itertools.zip_longest(chunks, fillvalue=0)) # 길이가 다른 자료형이라 zip_logest사용 해서 분할시킴(0x20,0x30,0x40,0x50)...(0,-,-,-) 이런식

def hex_view(filename, chunk_size=16): # 파일 hex값으로 읽어옴 template = ' {:<53}' with open(filename, 'rb') as stream: for chunk_count in itertools.count(0): chunk = stream.read(chunk_size) if not chunk: return yield template.format( hex_group_formatter(chunk) )

def file_output(header_sig): global out_file_cnt out_file_cnt += 1 print('.',file_extension[header_sig],'파일 검출','\t')

def size_rt(bytes): size_result = str(bytes) + 'Byte' if (bytes > 1024): size_result = str(int(ceil(bytes/1024))) + 'KB' elif (bytes > 10242): size_result = str(int(ceil(bytes/(10242)))) + 'MB' elif (bytes > 10243): size_result = str(ceil(bytes/(10243))) + 'GB' elif (bytes > 10244): size_result = str(int(ceil(bytes/(10244)))) + 'TB' return size_result

def stg(): file_name = input('Write file name : ') parser = argparse.ArgumentParser() parser.add_argument('file', nargs='?', default=file_name) args = parser.parse_args()

file_size = os.path.getsize(file_name)

print('\n검사 파일:', file_name,'\t','Size :', size_rt(file_size),'\n')

cnt = -1
out_file_cnt = 0
check_flag = 0
chck = 0
string = ''

for line in hex_view(args.file):
    string += line.replace("   ", " ")

while (cnt < len(file_sig)-1):
    cnt += 1

    if (string.find(file_sig[cnt]) != -1):      
        file_index = string.index(file_sig[cnt])
        header_sig = string[file_index:file_index+len(file_sig[cnt])]
        file_output(header_sig)
        chck = 1
        if (check_flag == 0):
            check_flag == 1
            string = string[file_index + len(file_sig[cnt]):]
            cnt -= 1
    else:
        check_flag = 0
        continue        
if chck == 0:
    print('숨겨진 파일이 없습니다.')
else:
    print(out_file_cnt,'개의 파일 검출\t')
sihunh commented 2 years ago

file_sig = ['ff d8 ff e0', 'ff d8 ff e8', '89 50 4e 47 0d 0a 1a 0a', '50 4b 03 04', '47 49 46 38 37 61', '47 49 46 38 39 61', '25 50 44 46 2d 31 2e', '4D 5A 90' ]

file_extension = {'ff d8 ff e0':'jpeg', 'ff d8 ff e8':'jpeg', '89 50 4e 47 0d 0a 1a 0a':'png', '50 4b 03 04':'zip', '47 49 46 38 37 61':'gif', '47 49 46 38 39 61':'gif', '25 50 44 46 2d 31 2e':'pdf', '4D 5A 90':'exe' }