Update README.md
Browse files
README.md
CHANGED
|
@@ -35,24 +35,32 @@ inference:
|
|
| 35 |
## Quick Start
|
| 36 |
|
| 37 |
```python
|
|
|
|
|
|
|
| 38 |
import torch
|
| 39 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 40 |
from peft import PeftModel
|
| 41 |
|
| 42 |
BASE_MODEL = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 43 |
ADAPTER = "sohomn/attack-chain-simulator-mistral7b-lora"
|
| 44 |
|
| 45 |
-
|
| 46 |
-
bnb_4bit_compute_dtype=torch.float16, bnb_4bit_quant_type="nf4")
|
| 47 |
|
| 48 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 49 |
tokenizer.pad_token = tokenizer.eos_token
|
| 50 |
|
|
|
|
| 51 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 52 |
-
BASE_MODEL,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
model = PeftModel.from_pretrained(base_model, ADAPTER)
|
| 55 |
model.eval()
|
|
|
|
|
|
|
| 56 |
|
| 57 |
def generate_attack_chain(log_text):
|
| 58 |
messages = [{"role": "user", "content": (
|
|
@@ -64,16 +72,37 @@ def generate_attack_chain(log_text):
|
|
| 64 |
"Privilege Escalation:\nLateral Movement:\n"
|
| 65 |
"Command & Control:\nData Exfiltration:"
|
| 66 |
)}]
|
| 67 |
-
prompt = tokenizer.apply_chat_template(
|
|
|
|
|
|
|
| 68 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 69 |
input_len = inputs["input_ids"].shape[-1]
|
|
|
|
| 70 |
with torch.no_grad():
|
| 71 |
-
out = model.generate(
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
return tokenizer.decode(out[0][input_len:], skip_special_tokens=True).strip()
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
```
|
| 78 |
|
| 79 |
## Training Data
|
|
|
|
| 35 |
## Quick Start
|
| 36 |
|
| 37 |
```python
|
| 38 |
+
!pip install -q hf_xet
|
| 39 |
+
|
| 40 |
import torch
|
| 41 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 42 |
from peft import PeftModel
|
| 43 |
|
| 44 |
BASE_MODEL = "mistralai/Mistral-7B-Instruct-v0.2"
|
| 45 |
ADAPTER = "sohomn/attack-chain-simulator-mistral7b-lora"
|
| 46 |
|
| 47 |
+
torch.cuda.empty_cache()
|
|
|
|
| 48 |
|
| 49 |
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 50 |
tokenizer.pad_token = tokenizer.eos_token
|
| 51 |
|
| 52 |
+
# No quantization — same as training environment
|
| 53 |
base_model = AutoModelForCausalLM.from_pretrained(
|
| 54 |
+
BASE_MODEL,
|
| 55 |
+
torch_dtype=torch.float16,
|
| 56 |
+
device_map="auto",
|
| 57 |
+
low_cpu_mem_usage=True,
|
| 58 |
+
)
|
| 59 |
|
| 60 |
model = PeftModel.from_pretrained(base_model, ADAPTER)
|
| 61 |
model.eval()
|
| 62 |
+
print("Model ready.")
|
| 63 |
+
print(f"VRAM: {torch.cuda.memory_allocated()/1e9:.1f} GB")
|
| 64 |
|
| 65 |
def generate_attack_chain(log_text):
|
| 66 |
messages = [{"role": "user", "content": (
|
|
|
|
| 72 |
"Privilege Escalation:\nLateral Movement:\n"
|
| 73 |
"Command & Control:\nData Exfiltration:"
|
| 74 |
)}]
|
| 75 |
+
prompt = tokenizer.apply_chat_template(
|
| 76 |
+
messages, tokenize=False, add_generation_prompt=True
|
| 77 |
+
)
|
| 78 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 79 |
input_len = inputs["input_ids"].shape[-1]
|
| 80 |
+
|
| 81 |
with torch.no_grad():
|
| 82 |
+
out = model.generate(
|
| 83 |
+
**inputs,
|
| 84 |
+
max_new_tokens=300,
|
| 85 |
+
do_sample=True,
|
| 86 |
+
temperature=0.7,
|
| 87 |
+
top_p=0.9,
|
| 88 |
+
top_k=50,
|
| 89 |
+
repetition_penalty=1.3,
|
| 90 |
+
pad_token_id=tokenizer.eos_token_id
|
| 91 |
+
)
|
| 92 |
return tokenizer.decode(out[0][input_len:], skip_special_tokens=True).strip()
|
| 93 |
|
| 94 |
+
# Tests
|
| 95 |
+
tests = [
|
| 96 |
+
"53 | 196 | 2 | 2 | 84 | 116 | 42 | 42 | 42.0 | 0.0",
|
| 97 |
+
"Suspicious PowerShell from WINHOST-04, outbound HTTPS to 185.234.x.x:443",
|
| 98 |
+
"Spear phishing email opened, macro executed, LSASS dump, PsExec lateral move",
|
| 99 |
+
]
|
| 100 |
+
|
| 101 |
+
for i, t in enumerate(tests, 1):
|
| 102 |
+
print(f"\n{'='*60}")
|
| 103 |
+
print(f"TEST {i}")
|
| 104 |
+
print(f"{'='*60}")
|
| 105 |
+
print(generate_attack_chain(t))
|
| 106 |
```
|
| 107 |
|
| 108 |
## Training Data
|