본문 바로가기

Python/Forensic

Python Forensics (1)

들어가기에 앞서, 해당 포스팅 시리즈는 tutorialspoint를 기반으로 작성된 포스팅입니다.


 파이썬은 읽기 쉽고 이해하기 쉬운 프로그래밍언어이다. <- 이 문장은 거의 파이썬 책 사면 첫 줄에 항상 적혀있더라. 물론 JAVA나 C에 비해 입문하기는 쉬운데 객체지향이란 개념에 대해 모르면 와장창인 언어인 것은 똑같다. 문법이 정해져 있어서 괄호와 TAB으로 백날 싸우는 부먹찍먹 논쟁은 파이썬과 거리가 멀다. 내가 사랑해마지않는 bs4나 request처럼 많은 라이브러리를 가지고 있기 때문에 다양한 곳에서 사용하고 있다. 이런 앙큼한 파이썬은 digital or computation forensics에도 쓰인다.

으아악

 그래서 Computational Forensics이 무엇인가? 설명할 수 있지만 포스팅이 길어지므로 적지 않겠다. 한창 이슈였던 숙명여고 사건에서도 포렌식이 쓰였고 버닝썬 게이트에서도 계속 쓰이고 있다. 문명의 디지털화가 가속된 만큼 포렌식 기술은 더욱 더 발전할 것이고 전자 정보또한 중요해질 것이다. 님들도 항상 본인을 구글링하는 습관을 들여 디지털 세계에 자신을 지우고 살아야 한다. 아래 표가 포렌식에 대해 간략하게 설명해주고 있다.

 법에 따라 패턴을 만들고 포렌식을 진행하려면 다음 naming conventions and patterns를 이해 할 필요가 있다.

Constants Uppercase with underscore separation HIGH_TEMPERATURE
Local variable name Lowercase with bumpy caps (underscores are optional) currentTemperature
Gloabl variable name Prefix gl lowercase with bumpy caps gl_maximumRecordedTemperature
Functions name Uppercase with bumpy caps with active voice ConvertFarenheitToCentigrade
Object name Prefix ob_lowercase with bumpy caps ob_myTempRecorder
Module An underscore followed by lowercase with bumpy caps _tempRecorder
Class names Prefix class_ then bumpy caps and keep brief class_TempSystem

 먼 소린지 모르겠다. 오! 방금 이해함. 이름 짓는 규칙이다. 규칙을 왜 정해두어야 하는걸까? 이는 데이터의 무결성을 위해서이다. 데이터가 조금이라도 바뀌면 암호화가 진행되었을 때 같다는 것을 보증할 수 없기 때문이다. 예를 들어보자.

import hashlib

name = input('Enter your full name\n')
temp = hashlib.md5(name.encode())
hex = temp.hexdigest()

print(hex)

#Enter your full name
#NAME
#ad32e604e17467fc435538334fbddf3e

#Enter your full name
#NAME_
#e460bca0da0d7398f6688aeebbcf359e

 hashlib 라이브러리를 다운받아서 내 이름을 암호화해본다. 이 경우 같은 이름은 계속 같은 방식으로 암호화되고 한 번이라도 바뀌었을 때에는 다른 암호가 되는 것을 확인할 수 있다. 이렇게 일관적인 암호화는 법정 증거나 패턴으로 쓰일 수 있기 때문에 naming rule이 필요하다. 여기서 사용된 hash function에 대해 알아보자.


hash function은 어떤 길이의 데이터를 고정된 길이의 데이터로 매핑하는 함수이다. 같은 입력을 하면 항상 같은 출력을 보장하며 어떠한 특징을 가지고 있다. 반복을 통해 결과->입력으로 돌아갈 수 없어서 brute force attack로 복호화를 하는것은 불가능하다. 참고로 brute force attack이란 암호가 4글자로 이루어진 비밀번호라면 0000~9999까지 대입하여 암호를 알아내는 방식이다. 이런 종류의 알고리즘을 one-way cryptographic algorithm이라고 한다. 이러한 해시 알고리즘은 네 가지 중요 속성을 갖는다.

- 주어진 입력에 대해 해쉬 값 계산이 쉽다.

- 해시에서 입력을 생성할 수 없다.

- 해시를 변경하지 않고 입력을 수정할 수 없다.

- 동일한 해시를 가진 두 개의 입력은 없어야 한다. (해시가 중복되지 않아야 한다)

 암호가 correct 인지 아닌지 판단하는 예제를 만들어본다.

 

 플로우차트를 먼저 보자.

 

import uuid
import hashlib
  
def hash_password(password):
   # userid is used to generate a random number
   salt = uuid.uuid4().hex #salt is stored in hexadecimal value
   return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt
     
def check_password(hashed_password, user_password):
   # hexdigest is used as an algorithm for storing passwords
   password, salt = hashed_password.split(':')
   return password == hashlib.sha256(salt.encode()+ user_password.encode()).hexdigest()

new_pass = input('Please enter required password ')
hashed_password = hash_password(new_pass)
print('The string to store in the db is: ' + hashed_password)
old_pass = input('Re-enter new password ')

if check_password(hashed_password, old_pass):
   print('Yuppie!! You eantered the right password')
else:
   print('Oops! I am sorry but the password does not match')
>sample.py
Please enter required password 1234
The string to store in the db is: 4dd5558c0469b8c0a586969963005eff445734c481d5b323b64cb04d219a36ff:97fc8ac04b214fb28f05b151b769ced3
Re-enter new password 1234
Yuppie!! You entered the right password

>sample.py
Please enter required password 1357
The string to store in the db is: 97086626c3e0354d9307fa11e960b859332963207fd4786e6916a703a63ad3a5:577076f83eb6491aa0fd70f60da49562
Re-enter new password 1234
Oops! I am sorry but the password does not match

'Python > Forensic' 카테고리의 다른 글

Python Forensics (2)  (0) 2019.05.10