mlsec 2.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.
Files changed (36) hide show
  1. mlsec-2.0.0/.github/workflows/ci.yml +55 -0
  2. mlsec-2.0.0/.github/workflows/publish.yml +24 -0
  3. mlsec-2.0.0/.gitignore +50 -0
  4. mlsec-2.0.0/CHANGELOG.md +39 -0
  5. mlsec-2.0.0/LICENSE +21 -0
  6. mlsec-2.0.0/PKG-INFO +261 -0
  7. mlsec-2.0.0/README.md +213 -0
  8. mlsec-2.0.0/SECURITY.md +80 -0
  9. mlsec-2.0.0/distributed_poison_monitor.py +434 -0
  10. mlsec-2.0.0/fgsm_regression_harness.py +365 -0
  11. mlsec-2.0.0/model-inspection.py +268 -0
  12. mlsec-2.0.0/pyproject.toml +92 -0
  13. mlsec-2.0.0/src/mlsec/__init__.py +3 -0
  14. mlsec-2.0.0/src/mlsec/cli.py +106 -0
  15. mlsec-2.0.0/src/mlsec/tools/__init__.py +1 -0
  16. mlsec-2.0.0/src/mlsec/tools/adversarial.py +365 -0
  17. mlsec-2.0.0/src/mlsec/tools/checkpoint_triage.py +372 -0
  18. mlsec-2.0.0/src/mlsec/tools/export_guard.py +375 -0
  19. mlsec-2.0.0/src/mlsec/tools/model_inspect.py +269 -0
  20. mlsec-2.0.0/src/mlsec/tools/poison_monitor.py +434 -0
  21. mlsec-2.0.0/src/mlsec/tools/triton_auditor.py +290 -0
  22. mlsec-2.0.0/tensorrt_export_guard.py +375 -0
  23. mlsec-2.0.0/tests/__init__.py +0 -0
  24. mlsec-2.0.0/tests/conftest.py +137 -0
  25. mlsec-2.0.0/tests/test_checkpoint_triage.py +220 -0
  26. mlsec-2.0.0/tests/test_distributed_poison_monitor.py +383 -0
  27. mlsec-2.0.0/tests/test_export_guard.py +191 -0
  28. mlsec-2.0.0/tests/test_fgsm_regression_harness.py +443 -0
  29. mlsec-2.0.0/tests/test_model_inspection.py +381 -0
  30. mlsec-2.0.0/tests/test_poison_monitor.py +221 -0
  31. mlsec-2.0.0/tests/test_tensorrt_export_guard.py +426 -0
  32. mlsec-2.0.0/tests/test_torch_checkpoint_triage.py +437 -0
  33. mlsec-2.0.0/tests/test_triton_auditor.py +192 -0
  34. mlsec-2.0.0/tests/test_triton_config_auditor.py +403 -0
  35. mlsec-2.0.0/torch_checkpoint_triage.py +374 -0
  36. mlsec-2.0.0/triton_config_auditor.py +290 -0
@@ -0,0 +1,55 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ permissions:
10
+ contents: read
11
+
12
+ jobs:
13
+ lint:
14
+ runs-on: ubuntu-latest
15
+ steps:
16
+ - uses: actions/checkout@v4
17
+ - uses: actions/setup-python@v5
18
+ with:
19
+ python-version: "3.12"
20
+ - run: pip install ruff
21
+ - run: ruff check .
22
+ - run: ruff format --check .
23
+
24
+ test:
25
+ runs-on: ${{ matrix.os }}
26
+ strategy:
27
+ fail-fast: false
28
+ matrix:
29
+ os: [ubuntu-latest, macos-latest]
30
+ python-version: ["3.10", "3.11", "3.12"]
31
+ steps:
32
+ - uses: actions/checkout@v4
33
+ - uses: actions/setup-python@v5
34
+ with:
35
+ python-version: ${{ matrix.python-version }}
36
+ - name: Install dependencies
37
+ run: pip install -e ".[dev,all]"
38
+ - name: Run tests
39
+ run: python -m pytest tests/ --ignore=tests/test_model_inspection.py -v --tb=short --cov=. --cov-report=xml
40
+ - name: Upload coverage
41
+ if: matrix.python-version == '3.12' && matrix.os == 'ubuntu-latest'
42
+ uses: codecov/codecov-action@v4
43
+ with:
44
+ file: coverage.xml
45
+ fail_ci_if_error: false
46
+
47
+ typecheck:
48
+ runs-on: ubuntu-latest
49
+ steps:
50
+ - uses: actions/checkout@v4
51
+ - uses: actions/setup-python@v5
52
+ with:
53
+ python-version: "3.12"
54
+ - run: pip install -e ".[dev,all]"
55
+ - run: mypy src/mlsec/ --ignore-missing-imports
@@ -0,0 +1,24 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ permissions:
8
+ id-token: write
9
+
10
+ jobs:
11
+ publish:
12
+ runs-on: ubuntu-latest
13
+ environment: pypi
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+ - uses: actions/setup-python@v5
17
+ with:
18
+ python-version: "3.12"
19
+ - name: Install build tools
20
+ run: pip install build
21
+ - name: Build package
22
+ run: python -m build
23
+ - name: Publish to PyPI
24
+ uses: pypa/gh-action-pypi-publish@release/v1
mlsec-2.0.0/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ # Python
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+ *.so
6
+ .Python
7
+ *.egg-info/
8
+ dist/
9
+ build/
10
+
11
+ # Virtual environments
12
+ venv/
13
+ env/
14
+ ENV/
15
+ .venv
16
+
17
+ # IDEs
18
+ .vscode/
19
+ .idea/
20
+ *.swp
21
+ *.swo
22
+ *~
23
+
24
+ # OS
25
+ .DS_Store
26
+ Thumbs.db
27
+
28
+ # Test artifacts
29
+ .coverage
30
+ htmlcov/
31
+ .pytest_cache/
32
+ .mypy_cache/
33
+ .ruff_cache/
34
+
35
+ # Content assets
36
+ linkedin-article.md
37
+ linkedin-post.md
38
+ mlsecops-gap-header.png
39
+ mlsec-toolkit-overview.png
40
+
41
+ # AI tool artifacts
42
+ .claude/
43
+ CLAUDE.md
44
+ claude.md
45
+ planning.md
46
+ status.md
47
+ notes.md
48
+ todo.md
49
+ *.chat
50
+ *.conversation
@@ -0,0 +1,39 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [2.0.0] - 2026-03-30
9
+
10
+ ### Added
11
+ - Proper Python package structure (`pip install mlsec`)
12
+ - Unified CLI entry point (`mlsec <tool>`) with subcommands and aliases
13
+ - `pyproject.toml` with optional dependency groups (`vision`, `transformers`, `onnx`, `safetensors`, `all`, `dev`)
14
+ - Comprehensive test suite (289 tests, 79% coverage)
15
+ - GitHub Actions CI pipeline (lint, test matrix, type checking)
16
+ - All tools now accept `argv` parameter for programmatic use
17
+
18
+ ### Fixed
19
+ - **PGD attack gradient crash** — `adv.grad.zero_()` on `None` after tensor reassignment in projection step
20
+ - **Checkpoint NaN masking** — `tensor.abs().max()` returned NaN when tensor contained NaN, masking extreme finite values. Now uses finite-only filtering.
21
+ - **Checkpoint histogram crash** — `torch.histc` crashed on tensors containing NaN/Inf values. Now filters non-finite values before computing histograms.
22
+ - **Dead code in `extract_state_dict`** — `"state_dict"` key check was unreachable because generic dict check matched first. Reordered to check `"state_dict"` key before generic dict extraction.
23
+ - **Triton config quote stripping** — Parameter keys from pbtxt retained surrounding quotes, causing auth/guard/redact pattern matching to fail.
24
+ - **Triton auth check** — Changed from exact match to substring match for auth parameter detection (consistent with guard and redact checks).
25
+
26
+ ### Changed
27
+ - Restructured from 6 loose scripts to `src/mlsec/` package layout
28
+ - Original standalone scripts preserved at root for backward compatibility
29
+
30
+ ## [1.0.0] - 2025-10-30
31
+
32
+ ### Added
33
+ - Initial release of ML security tools
34
+ - Six specialized security analysis scripts
35
+ - Support for PyTorch, TensorRT, and Triton models
36
+ - Model inspection and auditing capabilities
37
+
38
+ [2.0.0]: https://github.com/scthornton/ml-security-tools/compare/v1.0.0...v2.0.0
39
+ [1.0.0]: https://github.com/scthornton/ml-security-tools/releases/tag/v1.0.0
mlsec-2.0.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Scott Thornton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
mlsec-2.0.0/PKG-INFO ADDED
@@ -0,0 +1,261 @@
1
+ Metadata-Version: 2.4
2
+ Name: mlsec
3
+ Version: 2.0.0
4
+ Summary: Security analysis toolkit for machine learning models and infrastructure
5
+ Project-URL: Homepage, https://github.com/scthornton/ml-security-tools
6
+ Project-URL: Documentation, https://github.com/scthornton/ml-security-tools#readme
7
+ Project-URL: Repository, https://github.com/scthornton/ml-security-tools
8
+ Project-URL: Issues, https://github.com/scthornton/ml-security-tools/issues
9
+ Author-email: Scott Thornton <scott@perfecxion.ai>
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: adversarial,ai-safety,machine-learning,mlops,model-security,security
13
+ Classifier: Development Status :: 4 - Beta
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Intended Audience :: Science/Research
16
+ Classifier: License :: OSI Approved :: MIT License
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
23
+ Classifier: Topic :: Security
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: >=3.10
26
+ Requires-Dist: torch>=2.0
27
+ Provides-Extra: all
28
+ Requires-Dist: onnx>=1.14; extra == 'all'
29
+ Requires-Dist: onnxruntime>=1.15; extra == 'all'
30
+ Requires-Dist: safetensors>=0.3; extra == 'all'
31
+ Requires-Dist: torchvision>=0.15; extra == 'all'
32
+ Requires-Dist: transformers>=4.30; extra == 'all'
33
+ Provides-Extra: dev
34
+ Requires-Dist: mypy>=1.8; extra == 'dev'
35
+ Requires-Dist: pytest-cov>=4.0; extra == 'dev'
36
+ Requires-Dist: pytest>=7.0; extra == 'dev'
37
+ Requires-Dist: ruff>=0.4; extra == 'dev'
38
+ Provides-Extra: onnx
39
+ Requires-Dist: onnx>=1.14; extra == 'onnx'
40
+ Requires-Dist: onnxruntime>=1.15; extra == 'onnx'
41
+ Provides-Extra: safetensors
42
+ Requires-Dist: safetensors>=0.3; extra == 'safetensors'
43
+ Provides-Extra: transformers
44
+ Requires-Dist: transformers>=4.30; extra == 'transformers'
45
+ Provides-Extra: vision
46
+ Requires-Dist: torchvision>=0.15; extra == 'vision'
47
+ Description-Content-Type: text/markdown
48
+
49
+ # mlsec — ML Security Toolkit
50
+
51
+ [![CI](https://github.com/scthornton/ml-security-tools/actions/workflows/ci.yml/badge.svg)](https://github.com/scthornton/ml-security-tools/actions/workflows/ci.yml)
52
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue.svg)](https://python.org)
53
+ [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
54
+
55
+ Security analysis toolkit for machine learning models and infrastructure. Covers the full ML lifecycle — from adversarial robustness testing through training-time poisoning detection to deployment hardening.
56
+
57
+ ```
58
+ pip install mlsec # core (torch only)
59
+ pip install mlsec[all] # all optional dependencies
60
+ ```
61
+
62
+ ## Why mlsec?
63
+
64
+ Most ML security tools focus on a single attack surface. **mlsec covers six critical areas in one toolkit**, designed to integrate into existing MLOps pipelines:
65
+
66
+ | Tool | What it catches | When to use it |
67
+ |------|----------------|----------------|
68
+ | `mlsec adversarial` | Robustness regressions from FGSM, PGD, CW attacks | Before every model release — CI gate |
69
+ | `mlsec inspect` | Suspicious weights, activation anomalies, FGSM vulnerability | Quick triage of new/third-party models |
70
+ | `mlsec poison` | Gradient divergence in distributed training (DDP) | During training — live or post-hoc |
71
+ | `mlsec export-guard` | ONNX graph tampering, supply-chain integrity | Before deploying optimized models |
72
+ | `mlsec checkpoint` | NaN/Inf injection, weight magnitude anomalies, backdoor fingerprints | Before loading any checkpoint |
73
+ | `mlsec triton` | Missing rate limits, auth gaps, DoS vectors | Before deploying inference servers |
74
+
75
+ ## Quick Start
76
+
77
+ ### Adversarial Robustness Testing
78
+
79
+ Track robustness drift across model versions with baseline regression:
80
+
81
+ ```bash
82
+ mlsec adversarial \
83
+ --model-script models.py \
84
+ --factory create_resnet \
85
+ --input-shape 1,3,224,224 \
86
+ --num-classes 1000 \
87
+ --attacks fgsm pgd cw \
88
+ --epsilon 0.01 \
89
+ --baseline-file baselines.json \
90
+ --update-baseline
91
+ ```
92
+
93
+ ### Checkpoint Security Triage
94
+
95
+ Scan checkpoints for anomalies, generate fingerprints, convert to safe formats:
96
+
97
+ ```bash
98
+ mlsec checkpoint /path/to/checkpoints/ \
99
+ --convert-safetensors \
100
+ --write-fingerprint fingerprints/ \
101
+ --reference-fingerprint reference.json \
102
+ --json
103
+ ```
104
+
105
+ ### Distributed Poisoning Detection
106
+
107
+ Monitor gradient divergence during distributed training:
108
+
109
+ ```bash
110
+ # Generate demo data
111
+ mlsec poison simulate --log-dir logs/ --workers 8 --steps 500
112
+
113
+ # Analyze for poisoning signals
114
+ mlsec poison monitor --log-dir logs/ --threshold 3.0
115
+
116
+ # Live monitoring via UDP broadcast
117
+ mlsec poison listen --port 5454 --expected-workers 8
118
+ ```
119
+
120
+ ### ONNX/TensorRT Export Validation
121
+
122
+ Validate the full export pipeline with provenance tracking:
123
+
124
+ ```bash
125
+ mlsec export-guard \
126
+ --model-script models.py \
127
+ --input-shape 1,3,224,224 \
128
+ --enable-onnxruntime \
129
+ --build-engine \
130
+ --hash-record attestation.json
131
+ ```
132
+
133
+ ### Triton Server Hardening
134
+
135
+ Audit inference server configs for security gaps:
136
+
137
+ ```bash
138
+ mlsec triton models/**/config.pbtxt --summary
139
+ ```
140
+
141
+ ### Model Inspection
142
+
143
+ Quick security triage of HuggingFace models:
144
+
145
+ ```bash
146
+ mlsec inspect --allow-downloads
147
+ ```
148
+
149
+ ## Architecture
150
+
151
+ ```
152
+ mlsec/
153
+ ├── adversarial — FGSM, PGD, CW attacks with baseline regression tracking
154
+ ├── inspect — Weight anomaly detection + activation monitoring hooks
155
+ ├── poison — Gradient snapshotter + CUSUM change-point detection
156
+ ├── export-guard — ONNX lint + SHA-256 provenance chain + trtexec integration
157
+ ├── checkpoint — KL-divergence fingerprinting + safetensors conversion
158
+ └── triton — Heuristic config.pbtxt parser + security rule engine
159
+ ```
160
+
161
+ Each tool is **standalone** (no cross-dependencies) and works both as a CLI command and as a Python library:
162
+
163
+ ```python
164
+ from mlsec.tools.checkpoint_triage import inspect_state_dict, compute_fingerprint
165
+
166
+ anomalies = inspect_state_dict(model.state_dict(), threshold=100.0)
167
+ fingerprint = compute_fingerprint(model.state_dict())
168
+ ```
169
+
170
+ ## Installation
171
+
172
+ ```bash
173
+ # Minimal (PyTorch only)
174
+ pip install mlsec
175
+
176
+ # With specific extras
177
+ pip install mlsec[vision] # + torchvision
178
+ pip install mlsec[transformers] # + HuggingFace transformers
179
+ pip install mlsec[onnx] # + onnx + onnxruntime
180
+ pip install mlsec[safetensors] # + safetensors format support
181
+ pip install mlsec[all] # everything
182
+
183
+ # Development
184
+ pip install -e ".[dev,all]"
185
+ pytest tests/ -v
186
+ ```
187
+
188
+ ## CI/CD Integration
189
+
190
+ mlsec is designed to run in automated pipelines. Every tool returns structured exit codes:
191
+
192
+ | Exit Code | Meaning |
193
+ |-----------|---------|
194
+ | `0` | Clean — no anomalies detected |
195
+ | `1` | Error — invalid input or missing files |
196
+ | `2` | Alert — anomalies detected (investigate) |
197
+
198
+ Example GitHub Actions step:
199
+
200
+ ```yaml
201
+ - name: Adversarial regression gate
202
+ run: |
203
+ mlsec adversarial \
204
+ --model-script models.py \
205
+ --factory create_model \
206
+ --input-shape 1,3,224,224 \
207
+ --num-classes 1000 \
208
+ --attacks fgsm pgd \
209
+ --baseline-file baselines.json
210
+ ```
211
+
212
+ JSON output is available for checkpoint triage (`--json` flag) for machine-readable results.
213
+
214
+ ## Key Detection Capabilities
215
+
216
+ ### Adversarial Attacks
217
+ - **FGSM** — Fast Gradient Sign Method (single-step)
218
+ - **PGD** — Projected Gradient Descent (multi-step with random restarts)
219
+ - **CW** — Carlini-Wagner L-inf (optimization-based)
220
+ - Mixed-precision testing (float32, float16, bfloat16)
221
+ - Baseline tracking with configurable regression thresholds
222
+
223
+ ### Poisoning Detection
224
+ - Per-worker gradient L2/L-inf norm monitoring
225
+ - Cross-worker divergence ratio computation
226
+ - **CUSUM** change-point detection for slow-burn poisoning
227
+ - Live UDP broadcast aggregation for real-time alerts
228
+
229
+ ### Supply Chain Security
230
+ - SHA-256 provenance hashing at every pipeline stage
231
+ - ONNX graph linting (custom domains, control-flow ops, large constants, embedded subgraphs)
232
+ - KL-divergence fingerprinting for checkpoint drift detection
233
+ - Automatic safetensors conversion for pickle-free distribution
234
+
235
+ ### Deployment Hardening
236
+ - Triton config auditing without protobuf dependency
237
+ - Checks for: rate limiting, dynamic batching, auth controls, input bounds, logging redaction
238
+ - ONNX Runtime numerical drift comparison
239
+
240
+ ## Responsible Use
241
+
242
+ These tools are for **authorized security testing only**. See [SECURITY.md](SECURITY.md) for the full responsible use policy.
243
+
244
+ ## Contributing
245
+
246
+ ```bash
247
+ git clone https://github.com/scthornton/ml-security-tools.git
248
+ cd ml-security-tools
249
+ pip install -e ".[dev,all]"
250
+ pytest tests/ -v
251
+ ruff check .
252
+ ```
253
+
254
+ ## License
255
+
256
+ MIT — see [LICENSE](LICENSE).
257
+
258
+ ## Contact
259
+
260
+ - **Email:** scott@perfecxion.ai
261
+ - **Security issues:** See [SECURITY.md](SECURITY.md)
mlsec-2.0.0/README.md ADDED
@@ -0,0 +1,213 @@
1
+ # mlsec — ML Security Toolkit
2
+
3
+ [![CI](https://github.com/scthornton/ml-security-tools/actions/workflows/ci.yml/badge.svg)](https://github.com/scthornton/ml-security-tools/actions/workflows/ci.yml)
4
+ [![Python 3.10+](https://img.shields.io/badge/python-3.10%2B-blue.svg)](https://python.org)
5
+ [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE)
6
+
7
+ Security analysis toolkit for machine learning models and infrastructure. Covers the full ML lifecycle — from adversarial robustness testing through training-time poisoning detection to deployment hardening.
8
+
9
+ ```
10
+ pip install mlsec # core (torch only)
11
+ pip install mlsec[all] # all optional dependencies
12
+ ```
13
+
14
+ ## Why mlsec?
15
+
16
+ Most ML security tools focus on a single attack surface. **mlsec covers six critical areas in one toolkit**, designed to integrate into existing MLOps pipelines:
17
+
18
+ | Tool | What it catches | When to use it |
19
+ |------|----------------|----------------|
20
+ | `mlsec adversarial` | Robustness regressions from FGSM, PGD, CW attacks | Before every model release — CI gate |
21
+ | `mlsec inspect` | Suspicious weights, activation anomalies, FGSM vulnerability | Quick triage of new/third-party models |
22
+ | `mlsec poison` | Gradient divergence in distributed training (DDP) | During training — live or post-hoc |
23
+ | `mlsec export-guard` | ONNX graph tampering, supply-chain integrity | Before deploying optimized models |
24
+ | `mlsec checkpoint` | NaN/Inf injection, weight magnitude anomalies, backdoor fingerprints | Before loading any checkpoint |
25
+ | `mlsec triton` | Missing rate limits, auth gaps, DoS vectors | Before deploying inference servers |
26
+
27
+ ## Quick Start
28
+
29
+ ### Adversarial Robustness Testing
30
+
31
+ Track robustness drift across model versions with baseline regression:
32
+
33
+ ```bash
34
+ mlsec adversarial \
35
+ --model-script models.py \
36
+ --factory create_resnet \
37
+ --input-shape 1,3,224,224 \
38
+ --num-classes 1000 \
39
+ --attacks fgsm pgd cw \
40
+ --epsilon 0.01 \
41
+ --baseline-file baselines.json \
42
+ --update-baseline
43
+ ```
44
+
45
+ ### Checkpoint Security Triage
46
+
47
+ Scan checkpoints for anomalies, generate fingerprints, convert to safe formats:
48
+
49
+ ```bash
50
+ mlsec checkpoint /path/to/checkpoints/ \
51
+ --convert-safetensors \
52
+ --write-fingerprint fingerprints/ \
53
+ --reference-fingerprint reference.json \
54
+ --json
55
+ ```
56
+
57
+ ### Distributed Poisoning Detection
58
+
59
+ Monitor gradient divergence during distributed training:
60
+
61
+ ```bash
62
+ # Generate demo data
63
+ mlsec poison simulate --log-dir logs/ --workers 8 --steps 500
64
+
65
+ # Analyze for poisoning signals
66
+ mlsec poison monitor --log-dir logs/ --threshold 3.0
67
+
68
+ # Live monitoring via UDP broadcast
69
+ mlsec poison listen --port 5454 --expected-workers 8
70
+ ```
71
+
72
+ ### ONNX/TensorRT Export Validation
73
+
74
+ Validate the full export pipeline with provenance tracking:
75
+
76
+ ```bash
77
+ mlsec export-guard \
78
+ --model-script models.py \
79
+ --input-shape 1,3,224,224 \
80
+ --enable-onnxruntime \
81
+ --build-engine \
82
+ --hash-record attestation.json
83
+ ```
84
+
85
+ ### Triton Server Hardening
86
+
87
+ Audit inference server configs for security gaps:
88
+
89
+ ```bash
90
+ mlsec triton models/**/config.pbtxt --summary
91
+ ```
92
+
93
+ ### Model Inspection
94
+
95
+ Quick security triage of HuggingFace models:
96
+
97
+ ```bash
98
+ mlsec inspect --allow-downloads
99
+ ```
100
+
101
+ ## Architecture
102
+
103
+ ```
104
+ mlsec/
105
+ ├── adversarial — FGSM, PGD, CW attacks with baseline regression tracking
106
+ ├── inspect — Weight anomaly detection + activation monitoring hooks
107
+ ├── poison — Gradient snapshotter + CUSUM change-point detection
108
+ ├── export-guard — ONNX lint + SHA-256 provenance chain + trtexec integration
109
+ ├── checkpoint — KL-divergence fingerprinting + safetensors conversion
110
+ └── triton — Heuristic config.pbtxt parser + security rule engine
111
+ ```
112
+
113
+ Each tool is **standalone** (no cross-dependencies) and works both as a CLI command and as a Python library:
114
+
115
+ ```python
116
+ from mlsec.tools.checkpoint_triage import inspect_state_dict, compute_fingerprint
117
+
118
+ anomalies = inspect_state_dict(model.state_dict(), threshold=100.0)
119
+ fingerprint = compute_fingerprint(model.state_dict())
120
+ ```
121
+
122
+ ## Installation
123
+
124
+ ```bash
125
+ # Minimal (PyTorch only)
126
+ pip install mlsec
127
+
128
+ # With specific extras
129
+ pip install mlsec[vision] # + torchvision
130
+ pip install mlsec[transformers] # + HuggingFace transformers
131
+ pip install mlsec[onnx] # + onnx + onnxruntime
132
+ pip install mlsec[safetensors] # + safetensors format support
133
+ pip install mlsec[all] # everything
134
+
135
+ # Development
136
+ pip install -e ".[dev,all]"
137
+ pytest tests/ -v
138
+ ```
139
+
140
+ ## CI/CD Integration
141
+
142
+ mlsec is designed to run in automated pipelines. Every tool returns structured exit codes:
143
+
144
+ | Exit Code | Meaning |
145
+ |-----------|---------|
146
+ | `0` | Clean — no anomalies detected |
147
+ | `1` | Error — invalid input or missing files |
148
+ | `2` | Alert — anomalies detected (investigate) |
149
+
150
+ Example GitHub Actions step:
151
+
152
+ ```yaml
153
+ - name: Adversarial regression gate
154
+ run: |
155
+ mlsec adversarial \
156
+ --model-script models.py \
157
+ --factory create_model \
158
+ --input-shape 1,3,224,224 \
159
+ --num-classes 1000 \
160
+ --attacks fgsm pgd \
161
+ --baseline-file baselines.json
162
+ ```
163
+
164
+ JSON output is available for checkpoint triage (`--json` flag) for machine-readable results.
165
+
166
+ ## Key Detection Capabilities
167
+
168
+ ### Adversarial Attacks
169
+ - **FGSM** — Fast Gradient Sign Method (single-step)
170
+ - **PGD** — Projected Gradient Descent (multi-step with random restarts)
171
+ - **CW** — Carlini-Wagner L-inf (optimization-based)
172
+ - Mixed-precision testing (float32, float16, bfloat16)
173
+ - Baseline tracking with configurable regression thresholds
174
+
175
+ ### Poisoning Detection
176
+ - Per-worker gradient L2/L-inf norm monitoring
177
+ - Cross-worker divergence ratio computation
178
+ - **CUSUM** change-point detection for slow-burn poisoning
179
+ - Live UDP broadcast aggregation for real-time alerts
180
+
181
+ ### Supply Chain Security
182
+ - SHA-256 provenance hashing at every pipeline stage
183
+ - ONNX graph linting (custom domains, control-flow ops, large constants, embedded subgraphs)
184
+ - KL-divergence fingerprinting for checkpoint drift detection
185
+ - Automatic safetensors conversion for pickle-free distribution
186
+
187
+ ### Deployment Hardening
188
+ - Triton config auditing without protobuf dependency
189
+ - Checks for: rate limiting, dynamic batching, auth controls, input bounds, logging redaction
190
+ - ONNX Runtime numerical drift comparison
191
+
192
+ ## Responsible Use
193
+
194
+ These tools are for **authorized security testing only**. See [SECURITY.md](SECURITY.md) for the full responsible use policy.
195
+
196
+ ## Contributing
197
+
198
+ ```bash
199
+ git clone https://github.com/scthornton/ml-security-tools.git
200
+ cd ml-security-tools
201
+ pip install -e ".[dev,all]"
202
+ pytest tests/ -v
203
+ ruff check .
204
+ ```
205
+
206
+ ## License
207
+
208
+ MIT — see [LICENSE](LICENSE).
209
+
210
+ ## Contact
211
+
212
+ - **Email:** scott@perfecxion.ai
213
+ - **Security issues:** See [SECURITY.md](SECURITY.md)