chaos4455 commited on
Commit
e5c4cbf
·
verified ·
1 Parent(s): f7afb2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -138
app.py CHANGED
@@ -11,13 +11,13 @@ import torch # Necessário para SentenceTransformer, mesmo que não explícito
11
  from sentence_transformers import SentenceTransformer
12
 
13
  # --- Configuração Inicial ---
14
- DB_NAME = "training_data_large.db" # Usaremos o DB pré-gerado
 
15
  TABLE_NAME = "events"
16
 
17
  MODEL_NAME = "markusbayer/CySecBERT"
18
 
19
- # Parâmetros de Treinamento (mantidos para consistência, mas não usados para treinamento em tempo real)
20
- RANDOM_SEED = 42
21
  RISK_THRESHOLD = 50.0
22
 
23
  # --- Configuração de Seed Global (para reprodutibilidade da inferência, se houver aleatoriedade) ---
@@ -28,12 +28,13 @@ if torch.cuda.is_available():
28
  torch.cuda.manual_seed_all(RANDOM_SEED)
29
 
30
  # --- Globais para Modelos e Ferramentas (serão carregadas UMA VEZ) ---
31
- # Usamos st.cache_resource para carregar estes modelos apenas uma vez na inicialização do app
32
  model_base = None
33
  mlp_regressor, scaler = None, None
34
  tfidf_vectorizer, tfidf_regressor = None, None
35
 
36
  # --- Vocabulário de Palavras-Chave para a Cabeça 3 (Regra Baseada) ---
 
37
  HIGH_RISK_KEYWORDS = {
38
  'failed': 15, 'unauthorized': 20, 'invalid': 15, 'blocked': 25, 'mfa_failed': 30, 'brute_force': 40, 'attack': 40,
39
  'threat': 30, 'compromise': 30, 'malicious': 35, 'lockout': 25, 'critical': 20, 'urgent': 20, 'severe': 25,
@@ -50,12 +51,131 @@ LOW_RISK_KEYWORDS = {
50
  'backup completed': -20, 'schema migration successful': -15, 'network policy updated': -10
51
  }
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  # --- Funções para Inferência ---
54
 
55
  # Decorator para cachear o carregamento de recursos pesados, como modelos.
56
  # Isso garante que o modelo seja carregado apenas uma vez, mesmo após interações na UI.
57
  @st.cache_resource
58
- def load_all_models():
59
  """
60
  Carrega todos os modelos pré-treinados e o SentenceTransformer.
61
  Esta função é cacheada pelo Streamlit para ser executada apenas uma vez.
@@ -137,25 +257,30 @@ st.sidebar.info("Desenvolvido para demonstração no Hugging Face Spaces.")
137
  # Este bloco de `st.empty()` e `st.spinner` permite que as mensagens de carregamento sejam temporárias
138
  loading_message_placeholder = st.empty()
139
  with loading_message_placeholder.container():
140
- with st.spinner("Carregando modelos... isso pode levar um momento na primeira vez."):
141
- model_base, mlp_regressor, scaler, tfidf_vectorizer, tfidf_regressor = load_all_models()
142
  loading_message_placeholder.empty() # Remove a mensagem de carregamento após a conclusão
143
 
144
  st.subheader("Insira o Evento de Segurança para Análise:")
 
 
 
 
145
  event_text = st.text_area(
146
  "Descrição do Evento",
147
  height=200,
148
- value="Audit log: Unsandboxed process attempted lateral movement via a code repository. Status: the action was obfuscated."
 
149
  )
150
 
151
- # Adicione um botão para "Gerar Evento Aleatório" para facilitar a demonstração
152
  if st.button("Gerar Evento Aleatório (Risco)", help="Gera um exemplo de evento de alto risco para demonstração."):
153
- actor = random.choice([a for a in ADVERSARIAL_RISK_ACTORS]) # Usar listas do script de treino
154
- action = random.choice([a for a in ADVERSARIAL_RISK_ACTIONS])
155
- target = random.choice([t for t in ADVERSARIAL_RISK_TARGETS])
156
- outcome = random.choice([o for o in ADVERSARIAL_RISK_OUTCOMES])
157
- event_text = f"Audit log: {actor} {action} {target}. Status: {outcome}."
158
- st.session_state.event_text = event_text # Atualiza o estado da sessão para manter o texto
 
159
 
160
  if st.button("Analisar Risco", type="primary"):
161
  if not event_text.strip():
@@ -209,126 +334,4 @@ if st.button("Analisar Risco", type="primary"):
209
  plt.close(fig) # Fecha a figura para liberar memória
210
 
211
  st.markdown("---")
212
- st.markdown("Para uma nova análise, modifique o texto do evento acima ou gere um aleatório e clique em 'Analisar Risco' novamente.")
213
-
214
- # --- As listas de vocabulário extensas foram movidas para `train_and_save_models.py` para reduzir o tamanho do `app.py` ---
215
- # Se você quiser usar a funcionalidade de "Gerar Evento Aleatório", você precisará incluir essas listas aqui também.
216
- # Para evitar repetição e manter o app.py mais leve, o "Gerar Evento Aleatório" do app.py irá usar variáveis fixas ou uma versão reduzida.
217
- # Para manter a funcionalidade do "Gerar Evento Aleatório" no app.py, as listas completas precisam estar presentes.
218
- # Vamos adicioná-las aqui para que o botão de "Gerar Evento Aleatório" funcione no app.py também.
219
- ADVERSARIAL_RISK_ACTORS = [
220
- "Unsandboxed process", "Leaked API key", "Misconfigured service account", "Shadow IT application",
221
- "Dormant user account", "Ransomware payload", "Phishing attempt", "Insider threat",
222
- "Zero-day exploit", "Malicious actor", "Compromised credential", "Vulnerable third-party library",
223
- "Compromised Kubernetes pod", "Malicious Docker container", "AWS IAM role escalation",
224
- "Azure AD privilege escalation", "GCP service account abuse", "Container escape attempt",
225
- "Serverless function injection", "Cloud storage bucket enumeration", "API gateway bypass",
226
- "Microservice lateral movement", "Container registry poisoning", "Cloud metadata exploitation",
227
- "CI/CD pipeline compromise", "Git repository poisoning", "Build artifact tampering",
228
- "Deployment script injection", "Infrastructure as Code attack", "Secret scanning bypass",
229
- "Dependency confusion attack", "Supply chain compromise", "Code signing certificate theft",
230
- "Pipeline privilege escalation", "Artifact repository poisoning", "Build environment escape",
231
- "Compromised IoT device", "Edge computing exploit", "Industrial control system breach",
232
- "SCADA system compromise", "Smart city infrastructure attack", "Medical device exploitation",
233
- "Automotive system breach", "Home automation compromise", "Sensor data manipulation",
234
- "Edge gateway exploitation", "Industrial protocol abuse", "IoT botnet recruitment",
235
- "Mobile app sandbox escape", "iOS jailbreak exploitation", "Android rootkit installation",
236
- "Mobile banking trojan", "Enterprise device compromise", "BYOD policy violation",
237
- "Mobile device management bypass", "App store poisoning", "Mobile certificate pinning bypass",
238
- "Endpoint detection evasion", "Mobile phishing campaign", "Device fingerprinting abuse",
239
- "Network segmentation bypass", "Firewall rule manipulation", "VPN tunnel exploitation",
240
- "DNS hijacking attempt", "BGP route hijacking", "Network protocol abuse",
241
- "Wireless network compromise", "Bluetooth attack vector", "NFC exploitation",
242
- "Network monitoring evasion", "Traffic analysis bypass", "Protocol fuzzing attack"
243
- ]
244
- ADVERSARIAL_RISK_ACTIONS = [
245
- "attempted lateral movement via", "initiated a DNS tunneling request to",
246
- "executed a living-off-the-land binary on", "was flagged for unusual API call patterns against",
247
- "triggered a data access anomaly in", "exfiltrated data from", "modified critical system files in",
248
- "gained unauthorized access to", "deployed malicious code on", "brute-forced login for",
249
- "injected SQL into", "exploited a vulnerability in",
250
- "attempted container escape from", "escalated privileges in Kubernetes cluster",
251
- "abused IAM role permissions for", "enumerated cloud storage buckets through",
252
- "bypassed API gateway authentication to", "injected malicious code into serverless function",
253
- "compromised container registry access for", "exploited cloud metadata service to",
254
- "performed lateral movement across microservices in", "poisoned container image in",
255
- "abused cloud resource tagging for", "exploited cloud logging service to",
256
- "compromised CI/CD pipeline to", "injected malicious code into build process for",
257
- "poisoned dependency repository to", "tampered with build artifacts in",
258
- "escalated privileges in deployment pipeline for", "bypassed security scanning in",
259
- "abused infrastructure automation to", "compromised secret management system for",
260
- "injected malicious code into deployment scripts for", "exploited build environment to",
261
- "abused artifact signing process for", "compromised code repository access to",
262
- "compromised IoT device firmware to", "exploited edge computing vulnerability in",
263
- "breached industrial control system through", "manipulated sensor data from",
264
- "exploited SCADA system vulnerability to", "compromised smart city infrastructure via",
265
- "abused industrial protocol to", "exploited edge gateway vulnerability in",
266
- "recruited device into botnet through", "compromised medical device firmware to",
267
- "exploited automotive system vulnerability in", "breached home automation system via",
268
- "escaped mobile app sandbox to", "exploited iOS jailbreak vulnerability in",
269
- "installed rootkit on Android device to", "compromised enterprise mobile device through",
270
- "bypassed mobile device management to", "poisoned mobile app store listing for",
271
- "exploited mobile certificate pinning in", "compromised mobile banking app through",
272
- "abused device fingerprinting to", "exploited mobile phishing vulnerability in",
273
- "breached BYOD policy through", "compromised mobile endpoint security via",
274
- "bypassed network segmentation to", "manipulated firewall rules for",
275
- "exploited VPN tunnel vulnerability in", "hijacked DNS resolution for",
276
- "abused BGP routing protocol to", "compromised wireless network through",
277
- "exploited Bluetooth vulnerability in", "abused NFC communication to",
278
- "evaded network monitoring through", "bypassed traffic analysis via",
279
- "exploited network protocol vulnerability in", "compromised network infrastructure through"
280
- ]
281
- ADVERSARIAL_RISK_TARGETS = [
282
- "a code repository", "the CI/CD pipeline", "a cloud storage bucket", "the internal DNS server",
283
- "the virtual machine hypervisor", "sensitive customer data", "financial databases",
284
- "intellectual property servers", "critical infrastructure controls", "user authentication service",
285
- "production web server", "database backup storage",
286
- "Kubernetes cluster control plane", "Docker container registry", "AWS S3 bucket with sensitive data",
287
- "Azure Active Directory tenant", "GCP Cloud Storage bucket", "container orchestration system",
288
- "serverless function environment", "cloud API gateway", "microservice mesh network",
289
- "container security scanning service", "cloud logging and monitoring system", "infrastructure as code repository",
290
- "Git repository with production secrets", "Jenkins build pipeline", "Docker image registry",
291
- "artifact repository with signed packages", "infrastructure provisioning system", "secret management vault",
292
- "code signing certificate store", "dependency management system", "deployment automation platform",
293
- "build environment with elevated privileges", "CI/CD security scanning tools", "infrastructure monitoring system",
294
- "industrial control system network", "SCADA system database", "IoT device management platform",
295
- "edge computing gateway", "smart city infrastructure", "medical device network",
296
- "automotive system bus", "home automation hub", "sensor data collection system",
297
- "industrial protocol gateway", "edge security monitoring system", "IoT device firmware repository",
298
- "enterprise mobile device fleet", "mobile app store backend", "mobile device management system",
299
- "mobile banking infrastructure", "mobile certificate authority", "mobile security scanning service",
300
- "BYOD policy enforcement system", "mobile endpoint detection system", "mobile app security testing platform",
301
- "mobile device fingerprinting database", "mobile phishing detection system", "mobile app code signing service",
302
- "network segmentation firewall", "VPN concentrator", "DNS authoritative server",
303
- "BGP route reflector", "wireless access point controller", "network monitoring system",
304
- "traffic analysis platform", "network security scanning tool", "protocol analysis system",
305
- "network infrastructure management", "security information system", "network forensics platform"
306
- ]
307
- ADVERSARIAL_RISK_OUTCOMES = [
308
- "the action was obfuscated", "a low-and-slow data transfer was detected",
309
- "the process terminated abnormally after execution", "security controls were temporarily disabled",
310
- "alert thresholds were bypassed", "data integrity was compromised", "system uptime was impacted",
311
- "a backdoor was established", "a privilege escalation was achieved", "system resources were depleted",
312
- "data encryption initiated",
313
- "container escape was successful", "Kubernetes RBAC was bypassed", "cloud IAM policies were circumvented",
314
- "container registry was compromised", "serverless function was weaponized", "cloud logging was manipulated",
315
- "microservice communication was intercepted", "container security scanning was evaded",
316
- "cloud resource tagging was abused", "container orchestration was compromised",
317
- "cloud metadata service was exploited", "container networking was hijacked",
318
- "build pipeline was compromised", "dependency repository was poisoned", "artifact signing was bypassed",
319
- "infrastructure automation was weaponized", "secret management was breached", "code repository was compromised",
320
- "deployment process was hijacked", "build environment was escaped", "CI/CD security was bypassed",
321
- "infrastructure monitoring was disabled", "artifact integrity was compromised", "deployment approval was bypassed",
322
- "IoT device was recruited into botnet", "industrial control system was compromised", "edge gateway was breached",
323
- "sensor data was manipulated", "SCADA system was taken offline", "smart city infrastructure was disrupted",
324
- "medical device was compromised", "automotive system was hijacked", "home automation was breached",
325
- "industrial protocol was abused", "edge security was bypassed", "IoT device firmware was modified",
326
- "mobile device was rooted/jailbroken", "enterprise mobile security was bypassed", "mobile app was compromised",
327
- "mobile device management was evaded", "mobile banking was breached", "mobile certificate pinning was bypassed",
328
- "BYOD policy was violated", "mobile endpoint detection was evaded", "mobile app store was poisoned",
329
- "mobile device fingerprinting was spoofed", "mobile phishing was successful", "mobile security scanning was bypassed",
330
- "network segmentation was bypassed", "firewall rules were manipulated", "VPN tunnel was compromised",
331
- "DNS resolution was hijacked", "BGP routing was manipulated", "wireless network was compromised",
332
- "Bluetooth security was bypassed", "NFC communication was intercepted", "network monitoring was evaded",
333
- "traffic analysis was bypassed", "network protocol was abused", "network infrastructure was compromised"
334
- ]
 
11
  from sentence_transformers import SentenceTransformer
12
 
13
  # --- Configuração Inicial ---
14
+ # DB_NAME e TABLE_NAME são necessários para saber onde o DB pré-gerado está.
15
+ DB_NAME = "training_data_large.db"
16
  TABLE_NAME = "events"
17
 
18
  MODEL_NAME = "markusbayer/CySecBERT"
19
 
20
+ RANDOM_SEED = 42
 
21
  RISK_THRESHOLD = 50.0
22
 
23
  # --- Configuração de Seed Global (para reprodutibilidade da inferência, se houver aleatoriedade) ---
 
28
  torch.cuda.manual_seed_all(RANDOM_SEED)
29
 
30
  # --- Globais para Modelos e Ferramentas (serão carregadas UMA VEZ) ---
31
+ # Declaradas globalmente para serem acessíveis pelas funções de inferência.
32
  model_base = None
33
  mlp_regressor, scaler = None, None
34
  tfidf_vectorizer, tfidf_regressor = None, None
35
 
36
  # --- Vocabulário de Palavras-Chave para a Cabeça 3 (Regra Baseada) ---
37
+ # Essas listas são necessárias para a lógica de "Gerar Evento Aleatório" e a análise de palavras-chave.
38
  HIGH_RISK_KEYWORDS = {
39
  'failed': 15, 'unauthorized': 20, 'invalid': 15, 'blocked': 25, 'mfa_failed': 30, 'brute_force': 40, 'attack': 40,
40
  'threat': 30, 'compromise': 30, 'malicious': 35, 'lockout': 25, 'critical': 20, 'urgent': 20, 'severe': 25,
 
51
  'backup completed': -20, 'schema migration successful': -15, 'network policy updated': -10
52
  }
53
 
54
+ # Listas de vocabulário para o botão "Gerar Evento Aleatório" no app.py
55
+ ADVERSARIAL_RISK_ACTORS = [
56
+ "Unsandboxed process", "Leaked API key", "Misconfigured service account", "Shadow IT application",
57
+ "Dormant user account", "Ransomware payload", "Phishing attempt", "Insider threat",
58
+ "Zero-day exploit", "Malicious actor", "Compromised credential", "Vulnerable third-party library",
59
+ "Compromised Kubernetes pod", "Malicious Docker container", "AWS IAM role escalation",
60
+ "Azure AD privilege escalation", "GCP service account abuse", "Container escape attempt",
61
+ "Serverless function injection", "Cloud storage bucket enumeration", "API gateway bypass",
62
+ "Microservice lateral movement", "Container registry poisoning", "Cloud metadata exploitation",
63
+ "CI/CD pipeline compromise", "Git repository poisoning", "Build artifact tampering",
64
+ "Deployment script injection", "Infrastructure as Code attack", "Secret scanning bypass",
65
+ "Dependency confusion attack", "Supply chain compromise", "Code signing certificate theft",
66
+ "Pipeline privilege escalation", "Artifact repository poisoning", "Build environment escape",
67
+ "Compromised IoT device", "Edge computing exploit", "Industrial control system breach",
68
+ "SCADA system compromise", "Smart city infrastructure attack", "Medical device exploitation",
69
+ "Automotive system breach", "Home automation compromise", "Sensor data manipulation",
70
+ "Edge gateway exploitation", "Industrial protocol abuse", "IoT botnet recruitment",
71
+ "Mobile app sandbox escape", "iOS jailbreak exploitation", "Android rootkit installation",
72
+ "Mobile banking trojan", "Enterprise device compromise", "BYOD policy violation",
73
+ "Mobile device management bypass", "App store poisoning", "Mobile certificate pinning bypass",
74
+ "Endpoint detection evasion", "Mobile phishing campaign", "Device fingerprinting abuse",
75
+ "Network segmentation bypass", "Firewall rule manipulation", "VPN tunnel exploitation",
76
+ "DNS hijacking attempt", "BGP route hijacking", "Network protocol abuse",
77
+ "Wireless network compromise", "Bluetooth attack vector", "NFC exploitation",
78
+ "Network monitoring evasion", "Traffic analysis bypass", "Protocol fuzzing attack"
79
+ ]
80
+ ADVERSARIAL_RISK_ACTIONS = [
81
+ "attempted lateral movement via", "initiated a DNS tunneling request to",
82
+ "executed a living-off-the-land binary on", "was flagged for unusual API call patterns against",
83
+ "triggered a data access anomaly in", "exfiltrated data from", "modified critical system files in",
84
+ "gained unauthorized access to", "deployed malicious code on", "brute-forced login for",
85
+ "injected SQL into", "exploited a vulnerability in",
86
+ "attempted container escape from", "escalated privileges in Kubernetes cluster",
87
+ "abused IAM role permissions for", "enumerated cloud storage buckets through",
88
+ "bypassed API gateway authentication to", "injected malicious code into serverless function",
89
+ "compromised container registry access for", "exploited cloud metadata service to",
90
+ "performed lateral movement across microservices in", "poisoned container image in",
91
+ "abused cloud resource tagging for", "exploited cloud logging service to",
92
+ "compromised CI/CD pipeline to", "injected malicious code into build process for",
93
+ "poisoned dependency repository to", "tampered with build artifacts in",
94
+ "escalated privileges in deployment pipeline for", "bypassed security scanning in",
95
+ "abused infrastructure automation to", "compromised secret management system for",
96
+ "injected malicious code into deployment scripts for", "exploited build environment to",
97
+ "abused artifact signing process for", "compromised code repository access to",
98
+ "compromised IoT device firmware to", "exploited edge computing vulnerability in",
99
+ "breached industrial control system through", "manipulated sensor data from",
100
+ "exploited SCADA system vulnerability to", "compromised smart city infrastructure via",
101
+ "abused industrial protocol to", "exploited edge gateway vulnerability in",
102
+ "recruited device into botnet through", "compromised medical device firmware to",
103
+ "exploited automotive system vulnerability in", "breached home automation system via",
104
+ "escaped mobile app sandbox to", "exploited iOS jailbreak vulnerability in",
105
+ "installed rootkit on Android device to", "compromised enterprise mobile device through",
106
+ "bypassed mobile device management to", "poisoned mobile app store listing for",
107
+ "exploited mobile certificate pinning in", "compromised mobile banking app through",
108
+ "abused device fingerprinting to", "exploited mobile phishing vulnerability in",
109
+ "breached BYOD policy through", "compromised mobile endpoint security via",
110
+ "bypassed network segmentation to", "manipulated firewall rules for",
111
+ "exploited VPN tunnel vulnerability in", "hijacked DNS resolution for",
112
+ "abused BGP routing protocol to", "compromised wireless network through",
113
+ "exploited Bluetooth vulnerability in", "abused NFC communication to",
114
+ "evaded network monitoring through", "bypassed traffic analysis via",
115
+ "exploited network protocol vulnerability in", "compromised network infrastructure through"
116
+ ]
117
+ ADVERSARIAL_RISK_TARGETS = [
118
+ "a code repository", "the CI/CD pipeline", "a cloud storage bucket", "the internal DNS server",
119
+ "the virtual machine hypervisor", "sensitive customer data", "financial databases",
120
+ "intellectual property servers", "critical infrastructure controls", "user authentication service",
121
+ "production web server", "database backup storage",
122
+ "Kubernetes cluster control plane", "Docker container registry", "AWS S3 bucket with sensitive data",
123
+ "Azure Active Directory tenant", "GCP Cloud Storage bucket", "container orchestration system",
124
+ "serverless function environment", "cloud API gateway", "microservice mesh network",
125
+ "container security scanning service", "cloud logging and monitoring system", "infrastructure as code repository",
126
+ "Git repository with production secrets", "Jenkins build pipeline", "Docker image registry",
127
+ "artifact repository with signed packages", "infrastructure provisioning system", "secret management vault",
128
+ "code signing certificate store", "dependency management system", "deployment automation platform",
129
+ "build environment with elevated privileges", "CI/CD security scanning tools", "infrastructure monitoring system",
130
+ "industrial control system network", "SCADA system database", "IoT device management platform",
131
+ "edge computing gateway", "smart city infrastructure", "medical device network",
132
+ "automotive system bus", "home automation hub", "sensor data collection system",
133
+ "industrial protocol gateway", "edge security monitoring system", "IoT device firmware repository",
134
+ "enterprise mobile device fleet", "mobile app store backend", "mobile device management system",
135
+ "mobile banking infrastructure", "mobile certificate authority", "mobile security scanning service",
136
+ "BYOD policy enforcement system", "mobile endpoint detection system", "mobile app security testing platform",
137
+ "mobile device fingerprinting database", "mobile phishing detection system", "mobile app code signing service",
138
+ "network segmentation firewall", "VPN concentrator", "DNS authoritative server",
139
+ "BGP route reflector", "wireless access point controller", "network monitoring system",
140
+ "traffic analysis platform", "network security scanning tool", "protocol analysis system",
141
+ "network infrastructure management", "security information system", "network forensics platform"
142
+ ]
143
+ ADVERSARIAL_RISK_OUTCOMES = [
144
+ "the action was obfuscated", "a low-and-slow data transfer was detected",
145
+ "the process terminated abnormally after execution", "security controls were temporarily disabled",
146
+ "alert thresholds were bypassed", "data integrity was compromised", "system uptime was impacted",
147
+ "a backdoor was established", "a privilege escalation was achieved", "system resources were depleted",
148
+ "data encryption initiated",
149
+ "container escape was successful", "Kubernetes RBAC was bypassed", "cloud IAM policies were circumvented",
150
+ "container registry was compromised", "serverless function was weaponized", "cloud logging was manipulated",
151
+ "microservice communication was intercepted", "container security scanning was evaded",
152
+ "cloud resource tagging was abused", "container orchestration was compromised",
153
+ "cloud metadata service was exploited", "container networking was hijacked",
154
+ "build pipeline was compromised", "dependency repository was poisoned", "artifact signing was bypassed",
155
+ "infrastructure automation was weaponized", "secret management was breached", "code repository was compromised",
156
+ "deployment process was hijacked", "build environment was escaped", "CI/CD security was bypassed",
157
+ "infrastructure monitoring was disabled", "artifact integrity was compromised", "deployment approval was bypassed",
158
+ "IoT device was recruited into botnet", "industrial control system was compromised", "edge gateway was breached",
159
+ "sensor data was manipulated", "SCADA system was taken offline", "smart city infrastructure was disrupted",
160
+ "medical device was compromised", "automotive system was hijacked", "home automation was breached",
161
+ "industrial protocol was abused", "edge security was bypassed", "IoT device firmware was modified",
162
+ "mobile device was rooted/jailbroken", "enterprise mobile security was bypassed", "mobile app was compromised",
163
+ "mobile device management was evaded", "mobile banking was breached", "mobile certificate pinning was bypassed",
164
+ "BYOD policy was violated", "mobile endpoint detection was evaded", "mobile app store was poisoned",
165
+ "mobile device fingerprinting was spoofed", "mobile phishing was successful", "mobile security scanning was bypassed",
166
+ "network segmentation was bypassed", "firewall rules were manipulated", "VPN tunnel was compromised",
167
+ "DNS resolution was hijacked", "BGP routing was manipulated", "wireless network was compromised",
168
+ "Bluetooth security was bypassed", "NFC communication was intercepted", "network monitoring was evaded",
169
+ "traffic analysis was bypassed", "network protocol was abused", "network infrastructure was compromised"
170
+ ]
171
+
172
+
173
  # --- Funções para Inferência ---
174
 
175
  # Decorator para cachear o carregamento de recursos pesados, como modelos.
176
  # Isso garante que o modelo seja carregado apenas uma vez, mesmo após interações na UI.
177
  @st.cache_resource
178
+ def load_all_models_and_tokenizer():
179
  """
180
  Carrega todos os modelos pré-treinados e o SentenceTransformer.
181
  Esta função é cacheada pelo Streamlit para ser executada apenas uma vez.
 
257
  # Este bloco de `st.empty()` e `st.spinner` permite que as mensagens de carregamento sejam temporárias
258
  loading_message_placeholder = st.empty()
259
  with loading_message_placeholder.container():
260
+ model_base, mlp_regressor, scaler, tfidf_vectorizer, tfidf_regressor = load_all_models_and_tokenizer()
 
261
  loading_message_placeholder.empty() # Remove a mensagem de carregamento após a conclusão
262
 
263
  st.subheader("Insira o Evento de Segurança para Análise:")
264
+ # Use st.session_state para manter o texto da área de texto após a geração aleatória
265
+ if 'event_text_input' not in st.session_state:
266
+ st.session_state.event_text_input = "Audit log: Unsandboxed process attempted lateral movement via a code repository. Status: the action was obfuscated."
267
+
268
  event_text = st.text_area(
269
  "Descrição do Evento",
270
  height=200,
271
+ value=st.session_state.event_text_input,
272
+ key="event_text_input" # Atribui uma chave para o widget
273
  )
274
 
275
+ # Botão para "Gerar Evento Aleatório (Risco)"
276
  if st.button("Gerar Evento Aleatório (Risco)", help="Gera um exemplo de evento de alto risco para demonstração."):
277
+ actor = random.choice(ADVERSARIAL_RISK_ACTORS)
278
+ action = random.choice(ADVERSARIAL_RISK_ACTIONS)
279
+ target = random.choice(ADVERSARIAL_RISK_TARGETS)
280
+ outcome = random.choice(ADVERSARIAL_RISK_OUTCOMES)
281
+ random_event_text = f"Audit log: {actor} {action} {target}. Status: {outcome}."
282
+ st.session_state.event_text_input = random_event_text # Atualiza o texto na sessão
283
+ st.experimental_rerun() # Força o Streamlit a re-executar para atualizar a text_area
284
 
285
  if st.button("Analisar Risco", type="primary"):
286
  if not event_text.strip():
 
334
  plt.close(fig) # Fecha a figura para liberar memória
335
 
336
  st.markdown("---")
337
+ st.markdown("Para uma nova análise, modifique o texto do evento acima ou gere um aleatório e clique em 'Analisar Risco' novamente.")