안녕하세요, mj입니다. 오늘은 보안 관련 로그를 분석하는 스크립트를 작성해 보겠습니다. 보안 로그 분석은 시스템의 이상 징후를 조기에 발견하고, 데이터 유출 및 해킹 시도를 방지하는 데 매우 중요합니다. 이번 포스팅에서는 기본적인 스크립트 작성법과 몇 가지 예시를 통해 실제 활용 방안을 소개하겠습니다.
보안 로그는 시스템에서 발생하는 모든 이벤트를 기록합니다. 이러한 로그는 해커의 침입 시도를 추적하고, 시스템의 이상 동작을 식별하는 데 필수적입니다. 로그 분석을 통해 네트워크의 취약점을 발견하고, 보안 정책을 강화할 수 있습니다.
보안 로그를 분석하기 위해 파이썬을 사용한 간단한 스크립트를 작성해 보겠습니다. 아래의 코드는 특정 패턴을 가진 로그를 필터링하여 출력합니다.
import re
# 로그 파일 경로
log_file_path = 'security_log.txt'
# 필터링할 패턴
pattern = r'ERROR|WARNING'
def analyze_logs(file_path):
with open(file_path, 'r') as file:
for line in file:
if re.search(pattern, line):
print(line.strip())
# 로그 분석 실행
analyze_logs(log_file_path)
위 스크립트를 실행했을 때의 예시 출력입니다:
[2023-04-01 12:00:00] ERROR: Unauthorized access attempt detected.
[2023-04-01 12:05:00] WARNING: Disk space running low.
[2023-04-01 12:10:00] ERROR: Failed login attempt from IP 192.168.1.1.
다음은 다양한 상황에서 활용할 수 있는 보안 로그 분석 스크립트 예시입니다.
def filter_by_ip(file_path, ip_address):
with open(file_path, 'r') as file:
for line in file:
if ip_address in line:
print(line.strip())
# 특정 IP 주소 필터링 실행
filter_by_ip(log_file_path, '192.168.1.1')
def filter_by_date(file_path, start_date, end_date):
with open(file_path, 'r') as file:
for line in file:
date = line.split()[0]
if start_date <= date <= end_date:
print(line.strip())
# 날짜 범위 필터링 실행
filter_by_date(log_file_path, '2023-04-01', '2023-04-30')
def log_summary(file_path):
error_count = 0
warning_count = 0
with open(file_path, 'r') as file:
for line in file:
if 'ERROR' in line:
error_count += 1
elif 'WARNING' in line:
warning_count += 1
print(f'Errors: {error_count}, Warnings: {warning_count}')
# 로그 요약 실행
log_summary(log_file_path)
def search_by_keyword(file_path, keyword):
with open(file_path, 'r') as file:
for line in file:
if keyword in line:
print(line.strip())
# 특정 키워드 검색 실행
search_by_keyword(log_file_path, 'Failed')
def create_log_file(file_path):
with open(file_path, 'w') as file:
file.write('[2023-04-01 12:00:00] ERROR: Unauthorized access attempt detected.\n')
file.write('[2023-04-01 12:05:00] WARNING: Disk space running low.\n')
# 로그 파일 생성 실행
create_log_file(log_file_path)
이와 같은 스크립트를 활용하면 다양한 방식으로 보안 로그를 분석할 수 있습니다. 필요에 따라 추가적인 기능을 구현하여 더욱 효과적인 분석이 가능합니다.
보안 로그 분석은 시스템 보안을 강화하는 데 필수적인 작업입니다. 오늘 소개한 스크립트를 활용하여 다양한 로그 분석을 시도해 보시기 바랍니다. 추가적인 질문이나 필요하신 부분이 있다면 언제든지 댓글로 남겨주세요.
마무리하겠습니다. 감사합니다!