weightify 1.0.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,12 @@
1
+ Metadata-Version: 2.4
2
+ Name: weightify
3
+ Version: 1.0.0
4
+ Summary: A lightweight security scanner for AI model weights.
5
+ Project-URL: Homepage, https://github.com/Dur-E-Nayab-Khan/weightify
6
+ Author-email: Dur E Nayab <durenayabkhan459@gmail.com>
7
+ License: MIT
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Operating System :: OS Independent
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Topic :: Security
12
+ Requires-Python: >=3.8
File without changes
@@ -0,0 +1,22 @@
1
+ # pyproject.toml
2
+ [build-system]
3
+ requires = ["hatchling"]
4
+ build-backend = "hatchling.build"
5
+
6
+ [project]
7
+ name = "weightify"
8
+ version = "1.0.0"
9
+ description = "A lightweight security scanner for AI model weights."
10
+ readme = "README.md"
11
+ requires-python = ">=3.8"
12
+ license = {text = "MIT"}
13
+ authors = [{ name = "Dur E Nayab", email = "durenayabkhan459@gmail.com" }]
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "License :: OSI Approved :: MIT License",
17
+ "Operating System :: OS Independent",
18
+ "Topic :: Security",
19
+ ]
20
+
21
+ [project.urls]
22
+ Homepage = "https://github.com/Dur-E-Nayab-Khan/weightify"
File without changes
@@ -0,0 +1,11 @@
1
+ # setup.py
2
+ from setuptools import setup, find_packages
3
+
4
+ setup(
5
+ name="weightify",
6
+ version="0.1.0",
7
+ packages=find_packages(),
8
+ description="A security scanner for AI model weights",
9
+ author="Your Name",
10
+ python_requires=">=3.8",
11
+ )
@@ -0,0 +1,23 @@
1
+ # weightify/__init__.py
2
+ import io
3
+ import zipfile
4
+ from .engine import WeightScanner
5
+
6
+ def scan(byte_data: bytes):
7
+ """The main API for Weightify."""
8
+ scanner = WeightScanner()
9
+ results = []
10
+
11
+ # 1. Check if it's a Zip (Standard for PyTorch .pt files)
12
+ stream = io.BytesIO(byte_data)
13
+ if zipfile.is_zipfile(stream):
14
+ with zipfile.ZipFile(stream, 'r') as z:
15
+ for file_info in z.infolist():
16
+ if file_info.filename.endswith(('.pkl', '.bin')) or "data.pkl" in file_info.filename:
17
+ with z.open(file_info) as f:
18
+ results.extend(scanner.scan_stream(f.read()))
19
+ else:
20
+ # 2. Otherwise scan as raw bytes
21
+ results.extend(scanner.scan_stream(byte_data))
22
+
23
+ return results
@@ -0,0 +1,16 @@
1
+ # weightify/archive_handler.py
2
+ import zipfile
3
+ import io
4
+
5
+ def get_pickle_streams(byte_data):
6
+ """Detects if data is a zip (PyTorch) or raw pickle and yields streams."""
7
+ stream = io.BytesIO(byte_data)
8
+
9
+ if zipfile.is_zipfile(stream):
10
+ with zipfile.ZipFile(stream, 'r') as z:
11
+ for file_name in z.namelist():
12
+ # PyTorch stores weights in 'data.pkl' or files ending in .pkl
13
+ if file_name.endswith('.pkl') or 'data.pkl' in file_name:
14
+ yield z.read(file_name)
15
+ else:
16
+ yield byte_data
@@ -0,0 +1,68 @@
1
+ # weightify/constants.py
2
+
3
+ """
4
+ Weightify Threat Database
5
+ -------------------------
6
+ This file contains the known dangerous Python globals that can be
7
+ used to achieve Remote Code Execution (RCE) via serialized model weights.
8
+ """
9
+
10
+ # CRITICAL: These modules/functions trigger an immediate 'Dangerous' finding.
11
+ # We use "*" to block an entire module if any function within it is a risk.
12
+ UNSAFE_GLOBALS = {
13
+ # Core Python builtins that allow code execution or file manipulation
14
+ "__builtin__": {
15
+ "eval", "compile", "getattr", "apply",
16
+ "exec", "open", "breakpoint", "input"
17
+ },
18
+ "builtins": {
19
+ "eval", "compile", "getattr", "apply",
20
+ "exec", "open", "breakpoint", "input"
21
+ },
22
+
23
+ # System & Process Control (The most common RCE vectors)
24
+ "os": "*",
25
+ "subprocess": "*",
26
+ "posix": "*", # os alias for Linux/Mac
27
+ "nt": "*", # os alias for Windows
28
+ "pty": "*", # Pseudo-terminal utilities
29
+
30
+ # Network & Data Exfiltration
31
+ "socket": "*",
32
+ "requests.api": "*",
33
+ "urllib.request": "*",
34
+ "http.client": "*",
35
+
36
+ # Advanced code reconstruction (used by hackers to hide logic)
37
+ "types": {"CodeType", "FunctionType", "ModuleType"},
38
+ "runpy": "*",
39
+ "code": "*",
40
+
41
+ # File System manipulation
42
+ "shutil": "*",
43
+ "pathlib": "*",
44
+ "tempfile": "*",
45
+
46
+ # Tools often used for obfuscation/persistence
47
+ "sys": "*",
48
+ "importlib": "*",
49
+ "pickle": "*",
50
+ "shelve": "*"
51
+ }
52
+
53
+ # INNOCUOUS: These are allowed math/data structures.
54
+ # Anything NOT in this list and NOT in the UNSAFE list is marked as 'Suspicious'.
55
+ SAFE_GLOBALS = {
56
+ "numpy": {
57
+ "dtype", "ndarray", "core.multiarray._reconstruct",
58
+ "_core.multiarray._reconstruct"
59
+ },
60
+ "torch": {
61
+ "FloatStorage", "LongStorage", "HalfStorage", "ByteStorage",
62
+ "DoubleStorage", "CharStorage", "ShortStorage", "IntStorage",
63
+ "BoolStorage", "BFloat16Storage", "ComplexFloatStorage",
64
+ "_utils._rebuild_tensor_v2"
65
+ },
66
+ "collections": {"OrderedDict"},
67
+ "builtins": {"set", "list", "dict", "tuple"}
68
+ }
@@ -0,0 +1,27 @@
1
+ # weightify/engine.py
2
+ import pickletools
3
+ import io
4
+ from .constants import UNSAFE_GLOBALS
5
+
6
+ class WeightScanner:
7
+ def __init__(self):
8
+ self.blocklist = UNSAFE_GLOBALS
9
+
10
+ def scan_stream(self, data_stream):
11
+ """Analyzes a single byte stream for vulnerabilities."""
12
+ findings = []
13
+ try:
14
+ for opcode, arg, pos in pickletools.genops(data_stream):
15
+ # Standard Global calls
16
+ if opcode.name in ("GLOBAL", "INST"):
17
+ module, name = arg.split(" ", 1)
18
+ if self._is_unsafe(module, name):
19
+ findings.append({"module": module, "op": name, "at": pos})
20
+ except Exception:
21
+ pass
22
+ return findings
23
+
24
+ def _is_unsafe(self, module, name):
25
+ if module in self.blocklist:
26
+ return self.blocklist[module] == "*" or name in self.blocklist[module]
27
+ return False
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.4
2
+ Name: weightify
3
+ Version: 1.0.0
4
+ Summary: A lightweight security scanner for AI model weights.
5
+ Author: Your Name
6
+ Author-email: Your Name <you@example.com>
7
+ Requires-Python: >=3.8
8
+ Description-Content-Type: text/markdown
9
+ Dynamic: author
10
+ Dynamic: requires-python
@@ -0,0 +1,12 @@
1
+ README.md
2
+ pyproject.toml
3
+ setup.py
4
+ weightify/__init__.py
5
+ weightify/archive_handler.py
6
+ weightify/constants.py
7
+ weightify/engine.py
8
+ weightify.egg-info/PKG-INFO
9
+ weightify.egg-info/SOURCES.txt
10
+ weightify.egg-info/dependency_links.txt
11
+ weightify.egg-info/entry_points.txt
12
+ weightify.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ weightify-scan = weightify.cli:main
@@ -0,0 +1 @@
1
+ weightify