Text Generation
PEFT
English
security
vulnerability-detection
code-repair
zero-day
exploit-scanner
cybersecurity
sft
qlora
Instructions to use jacobmahon/zero-day-exploit-scanner-fixer with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use jacobmahon/zero-day-exploit-scanner-fixer with PEFT:
Task type is invalid.
- Notebooks
- Google Colab
- Kaggle
Add inference script for vulnerability scanning
Browse files- inference.py +188 -0
inference.py
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Zero-Day Exploit Scanner & Fixer - Inference Script
|
| 3 |
+
====================================================
|
| 4 |
+
Scans code for vulnerabilities and generates fixes.
|
| 5 |
+
|
| 6 |
+
Usage:
|
| 7 |
+
python inference.py --code "int main() { char buf[10]; gets(buf); }"
|
| 8 |
+
python inference.py --file vulnerable_code.c
|
| 9 |
+
python inference.py --interactive
|
| 10 |
+
|
| 11 |
+
Requirements:
|
| 12 |
+
pip install transformers peft torch bitsandbytes accelerate
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import argparse
|
| 16 |
+
import torch
|
| 17 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 18 |
+
from peft import PeftModel
|
| 19 |
+
|
| 20 |
+
MODEL_ID = "jacobmahon/zero-day-exploit-scanner-fixer"
|
| 21 |
+
BASE_MODEL = "Qwen/Qwen2.5-Coder-7B-Instruct"
|
| 22 |
+
|
| 23 |
+
SYSTEM_PROMPT = """You are a world-class security expert specializing in zero-day vulnerability detection and remediation. When given code, you will:
|
| 24 |
+
1. SCAN: Determine if the code contains a security vulnerability
|
| 25 |
+
2. IDENTIFY: If vulnerable, identify the CWE type and CVE ID if known
|
| 26 |
+
3. EXPLAIN: Provide a clear explanation of the vulnerability mechanism, attack vector, and potential impact
|
| 27 |
+
4. FIX: Provide the corrected code that patches the vulnerability
|
| 28 |
+
|
| 29 |
+
Always respond in the following structured format:
|
| 30 |
+
## SCAN RESULT
|
| 31 |
+
[VULNERABLE / SAFE]
|
| 32 |
+
|
| 33 |
+
## VULNERABILITY DETAILS
|
| 34 |
+
- **CWE**: [CWE ID and name]
|
| 35 |
+
- **CVE**: [CVE ID if known, otherwise "N/A"]
|
| 36 |
+
- **Severity**: [CRITICAL / HIGH / MEDIUM / LOW]
|
| 37 |
+
|
| 38 |
+
## EXPLANATION
|
| 39 |
+
[Detailed explanation of the vulnerability]
|
| 40 |
+
|
| 41 |
+
## VULNERABLE LINES
|
| 42 |
+
[Specific lines or patterns that are vulnerable]
|
| 43 |
+
|
| 44 |
+
## FIXED CODE
|
| 45 |
+
```
|
| 46 |
+
[Corrected code]
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
## FIX EXPLANATION
|
| 50 |
+
[What was changed and why]"""
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def load_model(model_id=MODEL_ID, base_model=BASE_MODEL, device="auto"):
|
| 54 |
+
"""Load the fine-tuned model with QLoRA adapter."""
|
| 55 |
+
print(f"Loading base model: {base_model}")
|
| 56 |
+
|
| 57 |
+
bnb_config = BitsAndBytesConfig(
|
| 58 |
+
load_in_4bit=True,
|
| 59 |
+
bnb_4bit_use_double_quant=True,
|
| 60 |
+
bnb_4bit_quant_type="nf4",
|
| 61 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 65 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 66 |
+
base_model,
|
| 67 |
+
quantization_config=bnb_config,
|
| 68 |
+
device_map=device,
|
| 69 |
+
torch_dtype=torch.bfloat16,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
print(f"Loading LoRA adapter: {model_id}")
|
| 73 |
+
model = PeftModel.from_pretrained(model, model_id)
|
| 74 |
+
model.eval()
|
| 75 |
+
|
| 76 |
+
return model, tokenizer
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def scan_code(code: str, model, tokenizer, language: str = "auto", max_new_tokens: int = 2048):
|
| 80 |
+
"""Scan code for vulnerabilities and generate fixes."""
|
| 81 |
+
|
| 82 |
+
if language == "auto":
|
| 83 |
+
# Simple language detection heuristics
|
| 84 |
+
if "#include" in code or "malloc" in code or "void " in code:
|
| 85 |
+
language = "C"
|
| 86 |
+
elif "def " in code or "import " in code:
|
| 87 |
+
language = "Python"
|
| 88 |
+
elif "function " in code or "const " in code or "=>" in code:
|
| 89 |
+
language = "JavaScript"
|
| 90 |
+
elif "public class" in code or "System.out" in code:
|
| 91 |
+
language = "Java"
|
| 92 |
+
else:
|
| 93 |
+
language = "code"
|
| 94 |
+
|
| 95 |
+
messages = [
|
| 96 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 97 |
+
{"role": "user", "content": f"Analyze the following {language} code for security vulnerabilities and provide a fix if needed:\n\n```{language.lower()}\n{code}\n```"},
|
| 98 |
+
]
|
| 99 |
+
|
| 100 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 101 |
+
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 102 |
+
|
| 103 |
+
with torch.no_grad():
|
| 104 |
+
outputs = model.generate(
|
| 105 |
+
**inputs,
|
| 106 |
+
max_new_tokens=max_new_tokens,
|
| 107 |
+
temperature=0.3,
|
| 108 |
+
top_p=0.9,
|
| 109 |
+
do_sample=True,
|
| 110 |
+
repetition_penalty=1.1,
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 114 |
+
return response
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
def main():
|
| 118 |
+
parser = argparse.ArgumentParser(description="Zero-Day Exploit Scanner & Fixer")
|
| 119 |
+
parser.add_argument("--code", type=str, help="Code string to analyze")
|
| 120 |
+
parser.add_argument("--file", type=str, help="Path to file to analyze")
|
| 121 |
+
parser.add_argument("--interactive", action="store_true", help="Interactive mode")
|
| 122 |
+
parser.add_argument("--language", type=str, default="auto", help="Programming language")
|
| 123 |
+
parser.add_argument("--model", type=str, default=MODEL_ID, help="Model ID")
|
| 124 |
+
parser.add_argument("--base-model", type=str, default=BASE_MODEL, help="Base model ID")
|
| 125 |
+
args = parser.parse_args()
|
| 126 |
+
|
| 127 |
+
model, tokenizer = load_model(args.model, args.base_model)
|
| 128 |
+
|
| 129 |
+
if args.code:
|
| 130 |
+
result = scan_code(args.code, model, tokenizer, args.language)
|
| 131 |
+
print(result)
|
| 132 |
+
|
| 133 |
+
elif args.file:
|
| 134 |
+
with open(args.file, "r") as f:
|
| 135 |
+
code = f.read()
|
| 136 |
+
print(f"\nScanning: {args.file}")
|
| 137 |
+
print("=" * 60)
|
| 138 |
+
result = scan_code(code, model, tokenizer, args.language)
|
| 139 |
+
print(result)
|
| 140 |
+
|
| 141 |
+
elif args.interactive:
|
| 142 |
+
print("Zero-Day Exploit Scanner & Fixer")
|
| 143 |
+
print("Enter code to analyze (type 'END' on a new line to submit, 'quit' to exit)")
|
| 144 |
+
print("=" * 60)
|
| 145 |
+
|
| 146 |
+
while True:
|
| 147 |
+
print("\nEnter code:")
|
| 148 |
+
lines = []
|
| 149 |
+
while True:
|
| 150 |
+
line = input()
|
| 151 |
+
if line.strip() == "END":
|
| 152 |
+
break
|
| 153 |
+
if line.strip() == "quit":
|
| 154 |
+
return
|
| 155 |
+
lines.append(line)
|
| 156 |
+
|
| 157 |
+
code = "\n".join(lines)
|
| 158 |
+
if not code.strip():
|
| 159 |
+
continue
|
| 160 |
+
|
| 161 |
+
print("\nAnalyzing...")
|
| 162 |
+
result = scan_code(code, model, tokenizer, args.language)
|
| 163 |
+
print("\n" + result)
|
| 164 |
+
|
| 165 |
+
else:
|
| 166 |
+
# Demo with example vulnerable code
|
| 167 |
+
demo_code = '''
|
| 168 |
+
void process_input(char *user_input) {
|
| 169 |
+
char buffer[64];
|
| 170 |
+
strcpy(buffer, user_input); // No bounds checking
|
| 171 |
+
printf("Processed: %s\\n", buffer);
|
| 172 |
+
}
|
| 173 |
+
|
| 174 |
+
int main() {
|
| 175 |
+
char input[1024];
|
| 176 |
+
gets(input); // Unsafe input
|
| 177 |
+
process_input(input);
|
| 178 |
+
return 0;
|
| 179 |
+
}
|
| 180 |
+
'''
|
| 181 |
+
print("Demo: Scanning example vulnerable C code")
|
| 182 |
+
print("=" * 60)
|
| 183 |
+
result = scan_code(demo_code, model, tokenizer, "C")
|
| 184 |
+
print(result)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
if __name__ == "__main__":
|
| 188 |
+
main()
|