woo! It's a command line tool and it will tell you if you've got an obfuscated zip or tar file!

This commit is contained in:
Catherine Oborski 2024-09-06 23:35:35 -05:00
parent 15465006e8
commit d094164c84
3 changed files with 50 additions and 16 deletions

7
app.py Normal file
View file

@ -0,0 +1,7 @@
from typer import Typer
import identify_file_type
app = Typer()
VERSION = "0.1.0-alpha"
app.add_typer(identify_file_type.app, name="identify_file_type")

View file

@ -0,0 +1,40 @@
from typer import Typer, Argument
from rich import print, console
from log import create_rich_logger
from pprint import pprint
import argparse
import pathlib
import shutil
import tempfile
import zipfile
import magic
app = Typer()
logger = create_rich_logger()
@app.command()
def identify(files: list[str] = Argument(..., help="File paths to identify")):
"""Identifies the file type of provided files with incorrect extensions."""
for file_path in files:
try:
with open(file_path, "rb") as f:
file_type = magic.from_buffer(f.read(2048))
if file_type.startswith("ZIP archive"):
with zipfile.ZipFile(file_path, "r") as z:
print(f"{file_path}: ZIP archive containing:")
for member in z.namelist():
Text.print(f" - {member}")
elif file_type.startswith("TAR archive"):
with tarfile.open(file_path, "r") as tar:
print(f"{file_path}: TAR archive containing:")
for member in tar.getmembers():
print(f" - {member.name}")
else:
print(f"{file_path}: {file_type}")
except FileNotFoundError:
log.error(f"Error: File not found: {file_path}")
print(f"[bold green]Identified file type: {file_type}[/bold green]")

19
main.py
View file

@ -1,25 +1,12 @@
from typer import Typer, Argument
from rich import print, console
from pprint import pprint
import argparse
import pathlib
import shutil
import tempfile
import zipfile
from typer import Typer
from log import create_rich_logger
import identify_file_type
app = Typer()
VERSION = "0.1.0-alpha"
logger = create_rich_logger()
@app.command()
def identify(files: list[str] = Argument(..., help="File paths to identify")):
"""Identifies the file type of provided files with incorrect extensions."""
# ... (file identification logic)
print("[bold green]Identified file type:[/bold green]")
app.add_typer(identify_file_type.app, name="identify_file_type")
if __name__ == "__main__":
app()