winreg 모듈은 윈도우 레지스트리를 다룰 수 있게 해 주는 모듈입니다.
내장 모듈이기 때문에 별도의 설치 과정은 필요 없습니다.
아래의 링크에서 자세한 정보를 확인할 수 있습니다.
https://docs.python.org/3/library/winreg.html
winreg — Windows registry access — Python 3.8.2 documentation
winreg — Windows registry access These functions expose the Windows registry API to Python. Instead of using an integer as the registry handle, a handle object is used to ensure that the handles are closed correctly, even if the programmer neglects to expl
docs.python.org
http://forensic-proof.com/archives/5908 를 확인해보면 MS오피스의 최근 사용 목록은 HKEY_CURRENT_USER 하위에 위치한다고 한다. 레지스트리 편집기로 MRU의 위치를 확인해본뒤 이를 파일로 저장하는 코드를 작성해보자.
MS 오피스 2013 사용 흔적 (MS Office 2013 User Activities) | FORENSIC-PROOF
forensic-proof.com
import winreg as reg
import json
#기록의 경로가 다를 수 있으므로 확인후 변경필요
key = reg.HKEY_CURRENT_USER
targets = ["PowerPoint", "Word", "Excel"]
#결과가 저장되는 딕셔너리로 레코드 데이터를 각키에 맞는 타입에 리스트로 저장한다.
result = {
"Excel" : [],
"PowerPoint":[],
"Word":[]
}
for i in targets:
#for문을 돌려 스트링포맷팅을 사용해 경로를 바꿔준다.
regPath = "Software\\Microsoft\\Office\\16.0\\%s\\File MRU" % (i)
#OpenKey로 HKEY_CURRENT_USER의 해당하는 경로를 오픈한다.
op = reg.OpenKey(key, regPath, 0,reg.KEY_ALL_ACCESS)
try:
count = 0
#레지스트리 내의 파일이 몇개인지 모르므로 무한루프를 사용한다.
while 1:
#오픈한 레지스트리 경로에서 name, value를 가져온다.
name, value, type = reg.EnumValue(op, count)
#가져온 데이터중 name에 Item이 들어가 있는 데이터를 *로 스플릿하여
#경로만을 뽑아 결과를 담을 변수에 추가한다.
if "Item" in name:
recordpath = value.split("*")[1]
result[i].append(recordpath)
count += 1
except WindowsError:
pass
print (result)
result = json.dumps(result)
f = open('경로\\officelog.json', 'w')
f.write(result)
f.close()
# now close the opened key
reg.CloseKey(op)
위의 코드를 돌렸을때 json파일이 생성되고 이를 json뷰어로 확인해보면 아래의 이미지처럼 엑셀, 파워포인트, 워드별로 사용 기록(파일명)을 보기쉽게 확인할 수 있다.
'프로그래밍 언어&프레임워크 > python' 카테고리의 다른 글
파이썬으로 이메일 보내기(SMTP) (1) | 2021.08.02 |
---|---|
openpyxl을 이용한 엑셀 데이터 추출하기 (0) | 2021.07.26 |