read-evidence-logs 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,11 @@
1
+ Metadata-Version: 2.3
2
+ Name: read-evidence-logs
3
+ Version: 0.1.0
4
+ Summary: Add your description here
5
+ Author: 6vi7ms
6
+ Author-email: 6vi7ms <6vi7ms@gmail.com>
7
+ Requires-Dist: beautifulsoup4>=4.15.0
8
+ Requires-Dist: click>=8.5.1
9
+ Requires-Python: >=3.13
10
+ Description-Content-Type: text/markdown
11
+
File without changes
@@ -0,0 +1,21 @@
1
+ [project]
2
+ name = "read-evidence-logs"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ requires-python = ">=3.13"
7
+ dependencies = [
8
+ "beautifulsoup4>=4.15.0",
9
+ "click>=8.5.1",
10
+ ]
11
+
12
+ [[project.authors]]
13
+ name = "6vi7ms"
14
+ email = "6vi7ms@gmail.com"
15
+
16
+ [project.scripts]
17
+ read-evidence-logs = "read_evidence_logs.main:main"
18
+
19
+ [build-system]
20
+ requires = ["uv_build>=0.12.18,<0.13.0"]
21
+ build-backend = "uv_build"
@@ -0,0 +1,18 @@
1
+ [project]
2
+ name = "read-evidence-logs"
3
+ version = "0.1.0"
4
+ description = "Add your description here"
5
+ readme = "README.md"
6
+ authors = [{ name = "6vi7ms", email = "6vi7ms@gmail.com" }]
7
+ requires-python = ">=3.13"
8
+ dependencies = [
9
+ "beautifulsoup4>=4.15.0",
10
+ "click>=8.5.1",
11
+ ]
12
+
13
+ [project.scripts]
14
+ read-evidence-logs = "read_evidence_logs.main:main"
15
+
16
+ [build-system]
17
+ requires = ["uv_build>=0.12.18,<0.13.0"]
18
+ build-backend = "uv_build"
@@ -0,0 +1,2 @@
1
+ def main() -> None:
2
+ print("Hello from read-evidence-logs!")
@@ -0,0 +1,219 @@
1
+ # /// script
2
+ # requires-python = ">=3.11"
3
+ # dependencies = [
4
+ # "click",
5
+ # "beautifulsoup4",
6
+ # ]
7
+ # ///
8
+
9
+ import re
10
+ from datetime import datetime
11
+ from pathlib import Path
12
+
13
+ import click
14
+ from bs4 import BeautifulSoup
15
+
16
+ TARGET_EXTENSIONS = {".txt", ".log", ".html", ".xml"}
17
+ SEPARATOR_PATTERN = re.compile(r"^-+\s*(.+?)\s*-+$")
18
+
19
+
20
+ def list_matching_files(folder: Path, extensions: set[str]) -> list[str]:
21
+ return sorted(
22
+ entry.name
23
+ for entry in folder.iterdir()
24
+ if entry.is_file() and entry.suffix.lower() in extensions
25
+ )
26
+
27
+
28
+ def prompt_select_file(files: list[str]) -> str | None:
29
+ click.echo(click.style("\nรายการไฟล์ที่พบ:", fg="cyan", bold=True))
30
+ for idx, filename in enumerate(files, start=1):
31
+ num_str = click.style(f" {idx}.", fg="yellow", bold=True)
32
+ ext_color = {
33
+ ".txt": "white",
34
+ ".log": "magenta",
35
+ ".html": "green",
36
+ ".xml": "blue",
37
+ }.get(Path(filename).suffix.lower(), "white")
38
+ name_str = click.style(filename, fg=ext_color)
39
+ click.echo(f"{num_str} {name_str}")
40
+
41
+ valid_choices = [str(i) for i in range(1, len(files) + 1)] + ["q"]
42
+ choice = click.prompt(
43
+ click.style("\nเลือกหมายเลขไฟล์ (หรือ q เพื่อยกเลิก)", fg="bright_cyan"),
44
+ type=click.Choice(valid_choices, case_sensitive=False),
45
+ )
46
+
47
+ if choice.lower() == "q":
48
+ return None
49
+
50
+ return files[int(choice) - 1]
51
+
52
+
53
+ def parse_txt_key_value(filepath: Path) -> dict[str, str]:
54
+ """อ่านไฟล์ .txt แบบ Key: Value โดยแยก prefix ของ Source Disk และ Image Destination"""
55
+ kv_data: dict[str, str] = {}
56
+ last_key: str | None = None
57
+ section_prefix: str = ""
58
+
59
+ with filepath.open("r", encoding="utf-8", errors="replace") as f:
60
+ for raw_line in f:
61
+ line = raw_line.rstrip("\r\n")
62
+ stripped = line.strip()
63
+
64
+ if not stripped:
65
+ continue
66
+
67
+ sec_match = SEPARATOR_PATTERN.match(stripped)
68
+ if sec_match:
69
+ section_title = sec_match.group(1).strip()
70
+ if section_title == "Source Disk":
71
+ section_prefix = "Source."
72
+ elif section_title == "Image Destination":
73
+ section_prefix = "Destination."
74
+ elif not section_title.startswith(("Start of ", "End of ")):
75
+ section_prefix = ""
76
+ last_key = None
77
+ continue
78
+
79
+ if ":" in stripped:
80
+ key, value = stripped.split(":", 1)
81
+ key = key.strip()
82
+ value = value.strip()
83
+
84
+ scoped_key = (
85
+ f"{section_prefix}{key}"
86
+ if section_prefix and key in {"Interface", "Model", "Serial number", "Capacity in bytes"}
87
+ else key
88
+ )
89
+ kv_data[scoped_key] = value
90
+ last_key = scoped_key
91
+ elif last_key is not None:
92
+ prev = kv_data[last_key]
93
+ kv_data[last_key] = f"{prev} {stripped}".strip() if prev else stripped
94
+
95
+ return kv_data
96
+
97
+
98
+ def parse_html_key_value(filepath: Path) -> dict[str, str]:
99
+ """อ่านไฟล์ .html รายงานของ TaskForce ด้วย BeautifulSoup แล้วแปลงเป็นคีย์มาตรฐาน"""
100
+ soup = BeautifulSoup(filepath.read_text(encoding="utf-8", errors="replace"), "html.parser")
101
+
102
+ def get_id_text(elem_id: str) -> str:
103
+ el = soup.find(id=elem_id)
104
+ return re.sub(r"\s+", " ", el.get_text(" ", strip=True)).strip() if el else ""
105
+
106
+ md5_row = soup.find(id="hash-result-MD5")
107
+ md5_val = ""
108
+ if md5_row:
109
+ val_el = md5_row.find(class_="value")
110
+ md5_val = val_el.get_text(strip=True) if val_el else ""
111
+
112
+ mapped: dict[str, str] = {}
113
+ if location_val := get_id_text("DeviceLocation"):
114
+ mapped["Source.Interface"] = location_val.split()[0]
115
+ if serial := get_id_text("DeviceSerial"):
116
+ mapped["Source.Serial number"] = serial
117
+ if model := get_id_text("DeviceModel"):
118
+ mapped["Source.Model"] = model
119
+ if size := get_id_text("DeviceSize"):
120
+ mapped["Source.Capacity in bytes"] = size
121
+ if md5_val:
122
+ mapped["Acquisition Md5"] = md5_val
123
+
124
+ return mapped
125
+
126
+
127
+ def parse_input_file(filepath: Path) -> dict[str, str]:
128
+ """เลือก parser ตามนามสกุลไฟล์"""
129
+ if filepath.suffix.lower() in {".html", ".htm"}:
130
+ return parse_html_key_value(filepath)
131
+ return parse_txt_key_value(filepath)
132
+
133
+
134
+ def extract_capacity_short(capacity_raw: str) -> str:
135
+ """ดึงขนาดความจุแบบสั้น เช่น '2 TB (2,000,398,934,016 bytes)' -> '2 TB' หรือ '1,000,204,886,016 (1.0 TB)' -> '1.0 TB'"""
136
+ if "(" in capacity_raw:
137
+ before_paren = capacity_raw.split("(", 1)[0].strip()
138
+ if before_paren:
139
+ return before_paren
140
+ match = re.search(r"\(([^()]+)\)\s*$", capacity_raw)
141
+ return match.group(1).strip() if match else capacity_raw.strip()
142
+
143
+
144
+ def build_information_content(kv_data: dict[str, str]) -> str:
145
+ """จัดรูปแบบข้อความตามเทมเพลตข้อมูลอุปกรณ์จัดเก็บข้อมูลภายใน"""
146
+ item_type = kv_data.get("Source.Interface") or kv_data.get("Interface", "-")
147
+ serial_number = kv_data.get("Source.Serial number") or kv_data.get("Serial number", "-")
148
+ model_full = kv_data.get("Source.Model") or kv_data.get("Model", "-")
149
+
150
+ parts = model_full.split(None, 1)
151
+ brand = parts[0] if len(parts) > 1 else "-"
152
+ model = model_full
153
+
154
+ capacity_raw = kv_data.get("Source.Capacity in bytes") or kv_data.get("Capacity in bytes", "-")
155
+ capacity = extract_capacity_short(capacity_raw)
156
+
157
+ md5 = kv_data.get("Acquisition Md5") or kv_data.get("Verification Md5") or "-"
158
+ sha1 = kv_data.get("Acquisition Sha1") or kv_data.get("Verification Sha1") or "-"
159
+
160
+ lines = [
161
+ "อุปกรณ์จัดเก็บข้อมูลภายใน",
162
+ f"\tประเภทอุปกรณ์จัดเก็บข้อมูล {item_type} หมายเลขประจำอุปกรณ์ {serial_number} ",
163
+ f"\tยี่ห้อ {brand} รุ่น {model} ขนาดความจุ {capacity}",
164
+ "\tค่า Hash Value ของไฟล์สำเนาหลักฐาน\t",
165
+ f"\t\tMD5\t\t{md5}",
166
+ f"\t\tSHA-1\t\t{sha1}",
167
+ ]
168
+ return "\n".join(lines) + "\n"
169
+
170
+
171
+ def export_information_txt(source_path: Path, kv_data: dict[str, str]) -> Path:
172
+ """บันทึกไฟล์ <ชื่อไฟล์ต้นฉบับ>_information_yyyy-mm-dd_hh-mm-ss.txt"""
173
+ timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
174
+ output_filename = f"{source_path.stem}_information_{timestamp}.txt"
175
+ output_path = source_path.parent / output_filename
176
+
177
+ content = build_information_content(kv_data)
178
+ output_path.write_text(content, encoding="utf-8")
179
+ return output_path
180
+
181
+
182
+ @click.command()
183
+ def main() -> None:
184
+ """ลิสต์ไฟล์ในโฟลเดอร์ปัจจุบัน ให้เลือกไฟล์ อ่านข้อมูล และ export ไฟล์ .txt"""
185
+ current_dir = Path.cwd()
186
+ files = list_matching_files(current_dir, TARGET_EXTENSIONS)
187
+
188
+ click.echo(
189
+ click.style("Folder: ", fg="bright_black", bold=True)
190
+ + click.style(str(current_dir), fg="cyan")
191
+ )
192
+ if not files:
193
+ click.echo(click.style("ไม่พบไฟล์ที่ตรงกับนามสกุลที่กำหนด", fg="red", bold=True))
194
+ return
195
+
196
+ selected_name = prompt_select_file(files)
197
+ if selected_name is None:
198
+ click.echo(click.style("ยกเลิกการเลือกไฟล์", fg="yellow"))
199
+ return
200
+
201
+ selected_path = current_dir / selected_name
202
+ click.echo(
203
+ click.style("\nคุณเลือกไฟล์: ", fg="green", bold=True)
204
+ + click.style(str(selected_path), fg="bright_green", underline=True)
205
+ )
206
+
207
+ kv_data = parse_input_file(selected_path)
208
+ exported_path = export_information_txt(selected_path, kv_data)
209
+
210
+ click.echo(
211
+ click.style("\nExport สำเร็จ: ", fg="green", bold=True)
212
+ + click.style(str(exported_path), fg="bright_cyan", underline=True)
213
+ )
214
+ click.echo(click.style("\nเนื้อหาที่บันทึก:", fg="yellow", bold=True))
215
+ click.echo(exported_path.read_text(encoding="utf-8"))
216
+
217
+
218
+ if __name__ == "__main__":
219
+ main()