-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
521 lines (439 loc) · 21 KB
/
Copy pathmain.py
File metadata and controls
521 lines (439 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
import os
import json
import time
import re
import pytz
from datetime import datetime, timedelta
from jinja2 import Template
import feedparser
import google.generativeai as genai
# --- 1. CONFIGURATION ---
GITHUB_REPO = "https://github.com/merv3guler/cyber-news-feed"
DATA_FILE = "data/articles.json"
MAX_HISTORY = 600
# KAYNAK YAPILANDIRMASI (İsim: Link)
# Buradaki isimler butonlarda görünecek
FEED_CONFIG = {
"The Hacker News": "https://feeds.feedburner.com/TheHackersNews",
"BleepingComputer": "https://www.bleepingcomputer.com/feed/",
"CISA Alerts": "https://www.cisa.gov/uscert/ncas/alerts.xml",
"Google Security": "https://feeds.feedburner.com/GoogleOnlineSecurityBlog",
"The Register": "https://www.theregister.com/security/headlines.atom",
"SANS ISC": "https://isc.sans.edu/rssfeed_full.xml",
"Krebs on Security": "https://krebsonsecurity.com/feed/",
"Schneier on Security": "https://www.schneier.com/feed/atom/",
"Palo Alto Unit 42": "https://unit42.paloaltonetworks.com/feed/",
"Trend Micro": "https://blog.trendmicro.com/category/trendlabs-security-intelligence/feed/",
"Dark Reading": "https://www.darkreading.com/rss.xml",
"Threatpost": "https://threatpost.com/feed/",
"Exploit-DB": "https://www.exploit-db.com/rss.xml"
}
# --- 2. HTML TEMPLATE (Frontend) ---
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cyber Threat Intel | Merve Guler</title>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;700&family=Inter:wght@400;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<style>
:root {
/* DARK MODE VARIABLES */
--bg-color: #0d1117;
--card-bg: #161b22;
--text-main: #c9d1d9;
--text-muted: #8b949e;
--border: #30363d;
--accent: #00ff41;
--accent-glow: rgba(0, 255, 65, 0.2);
--danger: #ff003c;
--sun-color: #f39c12;
--source-badge-bg: #1f6feb;
}
body.light-mode {
--bg-color: #f6f8fa;
--card-bg: #ffffff;
--text-main: #24292f;
--text-muted: #57606a;
--border: #d0d7de;
--accent: #1f883d;
--accent-glow: rgba(31, 136, 61, 0.1);
--source-badge-bg: #0969da;
}
body {
background-color: var(--bg-color);
color: var(--text-main);
font-family: 'Inter', sans-serif;
margin: 0;
padding: 0;
transition: background-color 0.3s, color 0.3s;
display: flex; flex-direction: column; min-height: 100vh;
}
.container { max-width: 1000px; margin: 0 auto; padding: 20px; flex: 1; width: 100%; }
/* HEADER */
header {
display: flex; justify-content: space-between; align-items: center;
padding-bottom: 20px; border-bottom: 1px solid var(--border); margin-bottom: 20px;
}
h1 {
font-family: 'JetBrains Mono', monospace; color: var(--accent); margin: 0;
font-size: 1.8rem; text-shadow: 0 0 5px var(--accent-glow);
}
.header-controls { display: flex; gap: 15px; align-items: center; }
.icon-btn {
background: none; border: 1px solid var(--border); color: var(--text-main);
padding: 8px 12px; border-radius: 6px; cursor: pointer; font-size: 1.1rem; transition: all 0.2s;
text-decoration: none; display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); }
.fa-sun { color: var(--sun-color); filter: drop-shadow(0 0 3px var(--sun-color)); }
/* FILTERS */
.controls-bar { display: flex; flex-direction: column; gap: 15px; margin-bottom: 30px; }
.filter-container {
display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
}
.filter-label { font-size: 0.8rem; color: var(--text-muted); font-family: 'JetBrains Mono', monospace; font-weight: bold; margin-right: 5px; }
.filter-btn {
background-color: var(--card-bg); border: 1px solid var(--border); color: var(--text-muted);
padding: 6px 12px; border-radius: 4px; cursor: pointer; font-size: 0.85rem; font-weight: 600; transition: 0.2s;
}
.filter-btn:hover, .filter-btn.active { background-color: var(--accent); color: #000; border-color: var(--accent); }
.filter-btn.zeroday-btn { border-color: var(--danger); color: var(--danger); }
.filter-btn.zeroday-btn:hover, .filter-btn.zeroday-btn.active { background-color: var(--danger); color: #fff; }
/* LIST LAYOUT */
.grid { display: flex; flex-direction: column; gap: 20px; }
.card {
background-color: var(--card-bg); border: 1px solid var(--border); border-radius: 6px;
padding: 20px; display: flex; flex-direction: column; position: relative;
transition: transform 0.2s; overflow: hidden; border-left: 4px solid var(--accent);
}
.card:hover { transform: translateX(5px); box-shadow: 0 5px 15px rgba(0,0,0,0.2); border-color: var(--accent); }
.zeroday-banner {
background-color: var(--danger); color: #fff; font-size: 0.7rem; font-weight: bold;
padding: 4px 8px; position: absolute; top: 0; right: 0;
border-bottom-left-radius: 6px; animation: pulse 1.5s infinite;
}
@keyframes pulse { 0% { opacity: 1; } 50% { opacity: 0.7; } 100% { opacity: 1; } }
.meta { display: flex; gap: 15px; margin-bottom: 8px; font-size: 0.75rem; font-family: 'JetBrains Mono'; align-items: center;}
.source-badge {
font-weight: bold; text-transform: uppercase; letter-spacing: 0.5px;
color: var(--source-badge-bg);
}
.date-badge { color: var(--text-muted); }
h2 { font-size: 1.2rem; margin: 0 0 10px 0; line-height: 1.3; }
h2 a { color: var(--text-main); text-decoration: none; }
h2 a:hover { color: var(--accent); text-decoration: underline; }
.summary { font-size: 0.95rem; line-height: 1.6; color: var(--text-muted); margin-bottom: 15px; }
.card-footer {
border-top: 1px solid var(--border); padding-top: 12px;
display: flex; justify-content: flex-end; align-items: center;
}
.share-links { display: flex; gap: 15px; }
.share-icon { color: var(--text-muted); font-size: 1.1rem; transition: 0.2s; }
.share-icon:hover { color: var(--accent); transform: scale(1.1); }
.share-icon:hover .fa-x-twitter { color: var(--text-main); }
/* NO CONTENT MESSAGE */
.no-content {
display: none; /* Hidden by default */
text-align: center;
padding: 40px;
background: var(--card-bg);
border: 1px dashed var(--border);
border-radius: 6px;
color: var(--text-muted);
font-family: 'JetBrains Mono';
}
/* PAGINATION */
.pagination { display: flex; justify-content: center; gap: 10px; margin-top: 40px; }
.page-btn {
background: var(--card-bg); border: 1px solid var(--border); color: var(--text-main);
padding: 8px 16px; border-radius: 4px; cursor: pointer; font-weight: bold;
}
.page-btn:hover { border-color: var(--accent); color: var(--accent); }
.page-btn:disabled { opacity: 0.5; cursor: not-allowed; border-color: var(--border); color: var(--text-muted); }
.page-info { align-self: center; font-family: 'JetBrains Mono'; font-size: 0.9rem; }
footer { margin-top: 50px; text-align: center; color: var(--text-muted); font-size: 0.85rem; padding-bottom: 20px; }
.heart { color: #ff003c; animation: beat 1s infinite alternate; display: inline-block; }
@keyframes beat { to { transform: scale(1.1); } }
.export-btn {
background-color: var(--card-bg); border: 1px solid var(--accent); color: var(--accent);
padding: 6px 12px; border-radius: 4px; cursor: pointer; font-weight: bold;
}
.export-btn:hover { background-color: var(--accent); color: #000; }
</style>
</head>
<body>
<div class="container">
<header>
<div>
<h1>/// CYBER_INTEL_FEED</h1>
<small style="font-family: 'JetBrains Mono'; color: var(--text-muted);">
Status: <span style="color:var(--accent)">ONLINE</span> | {{ last_updated }}
</small>
</div>
<div class="header-controls">
<button class="icon-btn" id="themeToggle"><i class="fas fa-sun"></i></button>
<a href="{{ github_repo }}" target="_blank" class="icon-btn" title="Go to Repo"><i class="fab fa-github"></i></a>
</div>
</header>
<div class="controls-bar">
<div class="filter-container">
<span class="filter-label">SOURCES:</span>
<button class="filter-btn active" onclick="applyFilter('all')">ALL</button>
<button class="filter-btn zeroday-btn" onclick="applyFilter('zeroday')">ZERO DAY</button>
{% for source_name in all_sources %}
<button class="filter-btn" onclick="applyFilter('{{ source_name }}')">{{ source_name }}</button>
{% endfor %}
</div>
<div style="display:flex; justify-content:flex-end; margin-top:10px;">
<button class="export-btn" onclick="exportToExcel()"><i class="fas fa-file-excel"></i> Export CSV</button>
</div>
</div>
<div class="grid" id="newsGrid">
{% for item in items %}
<article class="card"
data-source="{{ item.source }}"
data-zeroday="{{ 'true' if item.is_zeroday else 'false' }}">
{% if item.is_zeroday %}<div class="zeroday-banner">⚠️ ZERO-DAY DETECTED</div>{% endif %}
<div class="meta">
<span class="source-badge">{{ item.source }}</span>
<span class="date-badge"> | {{ item.date }}</span>
</div>
<h2><a href="{{ item.link }}" target="_blank">{{ item.title }}</a></h2>
<div class="summary">{{ item.summary | safe }}</div>
<div class="card-footer">
<div class="share-links">
<a href="https://wa.me/?text={{ item.link }}" target="_blank" class="share-icon"><i class="fab fa-whatsapp"></i></a>
<a href="https://www.linkedin.com/sharing/share-offsite/?url={{ item.link }}" target="_blank" class="share-icon"><i class="fab fa-linkedin"></i></a>
<a href="https://twitter.com/intent/tweet?text={{ item.title }}&url={{ item.link }}" target="_blank" class="share-icon"><i class="fa-brands fa-x-twitter"></i></a>
<a href="mailto:?subject=Cyber Intel&body={{ item.title }} - {{ item.link }}" class="share-icon"><i class="fas fa-envelope"></i></a>
</div>
</div>
</article>
{% endfor %}
</div>
<div id="noContentMsg" class="no-content">
<i class="fas fa-inbox" style="font-size: 2rem; margin-bottom: 10px; display:block;"></i>
No current content available for this source.
</div>
<div class="pagination" id="paginationControls">
<button class="page-btn" onclick="changePage(-1)" id="prevBtn">< Previous</button>
<span class="page-info" id="pageInfo">Page 1</span>
<button class="page-btn" onclick="changePage(1)" id="nextBtn">Next ></button>
</div>
<footer>
Developed by <a href="{{ github_repo }}" style="color:var(--text-main);text-decoration:none;font-weight:bold;">Merve Guler</a>
<span class="heart">🩷</span>
</footer>
</div>
<script>
// --- VARIABLES ---
const cards = Array.from(document.querySelectorAll('.card'));
const itemsPerPage = 9;
let currentPage = 1;
let filteredCards = cards;
// --- 1. THEME TOGGLE ---
const toggleBtn = document.getElementById('themeToggle');
const icon = toggleBtn.querySelector('i');
const body = document.body;
if (localStorage.getItem('theme') === 'light') {
body.classList.add('light-mode');
icon.classList.replace('fa-sun', 'fa-moon');
icon.style.color = "";
}
toggleBtn.addEventListener('click', () => {
body.classList.toggle('light-mode');
if (body.classList.contains('light-mode')) {
icon.classList.replace('fa-sun', 'fa-moon');
icon.style.color = "";
localStorage.setItem('theme', 'light');
} else {
icon.classList.replace('fa-moon', 'fa-sun');
icon.style.color = "#f39c12";
localStorage.setItem('theme', 'dark');
}
});
// --- 2. FILTER LOGIC ---
function applyFilter(criteria) {
document.querySelectorAll('.filter-btn').forEach(btn => btn.classList.remove('active'));
event.target.classList.add('active');
filteredCards = cards.filter(card => {
const source = card.getAttribute('data-source');
const isZeroDay = card.getAttribute('data-zeroday') === 'true';
if (criteria === 'all') return true;
if (criteria === 'zeroday') return isZeroDay;
if (source === criteria) return true;
return false;
});
// Show/Hide No Content Message
const noContentMsg = document.getElementById('noContentMsg');
const grid = document.getElementById('newsGrid');
if (filteredCards.length === 0) {
noContentMsg.style.display = 'block';
grid.style.display = 'none';
} else {
noContentMsg.style.display = 'none';
grid.style.display = 'flex';
}
currentPage = 1;
renderPagination();
}
// --- 3. PAGINATION LOGIC ---
function renderPagination() {
const totalPages = Math.ceil(filteredCards.length / itemsPerPage);
cards.forEach(card => card.style.display = 'none');
const start = (currentPage - 1) * itemsPerPage;
const end = start + itemsPerPage;
const pageItems = filteredCards.slice(start, end);
pageItems.forEach(card => card.style.display = 'flex');
document.getElementById('pageInfo').innerText = `Page ${currentPage} of ${totalPages || 1}`;
document.getElementById('prevBtn').disabled = currentPage === 1;
document.getElementById('nextBtn').disabled = currentPage >= totalPages;
// Hide pagination if no items or fit in one page
document.getElementById('paginationControls').style.display = (filteredCards.length > 0 && totalPages > 1) ? 'flex' : 'none';
}
function changePage(direction) {
currentPage += direction;
renderPagination();
window.scrollTo(0, 0);
}
// --- 4. EXCEL EXPORT ---
function exportToExcel() {
let csv = "Source,Date,Title,Link,Summary\\n";
filteredCards.forEach(card => {
const src = card.getAttribute('data-source');
const date = card.querySelector('.meta span:last-child').innerText.replace('| ', '');
const title = card.querySelector('h2 a').innerText.replace(/,/g, '');
const link = card.querySelector('h2 a').href;
const sum = card.querySelector('.summary').innerText.replace(/\\n/g, ' ').replace(/,/g, '');
csv += `${src},${date},${title},${link},"${sum}"\\n`;
});
const link = document.createElement("a");
link.href = encodeURI("data:text/csv;charset=utf-8," + csv);
link.download = "cyber_intel_report.csv";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
// Initial render
renderPagination();
</script>
</body>
</html>
"""
# --- 3. BACKEND LOGIC ---
def clean_html(raw_html):
"""Remove HTML tags and cut cleanly."""
cleanr = re.compile('<.*?>')
cleantext = re.sub(cleanr, '', raw_html)
if len(cleantext) > 400:
return cleantext[:400].rsplit(' ', 1)[0] + "..."
return cleantext.strip()
def load_history():
"""Load existing articles from JSON."""
if os.path.exists(DATA_FILE):
try:
with open(DATA_FILE, 'r', encoding='utf-8') as f:
return json.load(f)
except:
return []
return []
def save_history(articles):
"""Save articles to JSON."""
os.makedirs(os.path.dirname(DATA_FILE), exist_ok=True)
with open(DATA_FILE, 'w', encoding='utf-8') as f:
json.dump(articles, f, ensure_ascii=False, indent=4)
def fetch_rss_feeds():
"""Fetch and parse RSS feeds."""
articles = []
utc_now = datetime.now(pytz.utc)
time_limit = utc_now - timedelta(hours=24)
print("[-] Scanning RSS Feeds...")
zeroday_keywords = ['0-day', 'zero-day', 'zero day', 'exploit', 'cve-', 'critical', 'vulnerability']
for name, url in FEED_CONFIG.items():
try:
feed = feedparser.parse(url)
# Use OUR name from config, not the feed's name
source_name = name
for entry in feed.entries:
if hasattr(entry, 'published_parsed'):
pub_date = datetime(*entry.published_parsed[:6], tzinfo=pytz.utc)
else:
continue
if pub_date > time_limit:
title = entry.title
raw_summary = entry.get('summary', entry.get('description', ''))
clean_summary = clean_html(raw_summary)
is_zeroday = any(k in title.lower() or k in clean_summary.lower() for k in zeroday_keywords)
articles.append({
"source": source_name,
"title": title,
"link": entry.link,
"raw_summary": clean_summary,
"summary": "",
"date": pub_date.strftime("%Y-%m-%d %H:%M"),
"timestamp": pub_date.isoformat(),
"is_zeroday": is_zeroday,
"processed": False
})
except Exception as e:
print(f"[!] Error processing {name}: {e}")
return articles
def process_ai_summaries(new_articles):
"""Summarize new articles using Gemini."""
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
print("[!] No GEMINI_API_KEY found. Skipping AI.")
return new_articles
genai.configure(api_key=api_key)
model = genai.GenerativeModel('gemini-1.5-flash')
count = 0
total = len(new_articles)
print(f"[-] Processing {total} new articles with AI...")
for article in new_articles:
if not article.get('processed'):
count += 1
try:
print(f" [{count}/{total}] Summarizing: {article['title'][:30]}...")
context = "CRITICAL VULNERABILITY! " if article['is_zeroday'] else ""
prompt = (f"{context}Summarize this cybersecurity news in English. "
f"Provide exactly one complete, technical paragraph. Do not cut off sentences. "
f"Max 50 words. News: {article['title']} - {article['raw_summary']}")
response = model.generate_content(prompt)
article['summary'] = response.text
article['processed'] = True
time.sleep(20)
except Exception as e:
print(f" [!] AI Error: {e}")
article['summary'] = article['raw_summary']
return new_articles
def main():
history = load_history()
history_links = {item['link'] for item in history}
fresh_news = fetch_rss_feeds()
new_unique_news = [item for item in fresh_news if item['link'] not in history_links]
if new_unique_news:
print(f"[*] Found {len(new_unique_news)} new unique articles.")
processed_news = process_ai_summaries(new_unique_news)
full_list = processed_news + history
full_list.sort(key=lambda x: x['timestamp'], reverse=True)
full_list = full_list[:MAX_HISTORY]
save_history(full_list)
print("[*] Database updated.")
else:
print("[*] No new articles found. Generating page from history.")
full_list = history
template = Template(HTML_TEMPLATE)
output_html = template.render(
items=full_list,
last_updated=datetime.now().strftime("%Y-%m-%d %H:%M UTC"),
github_repo=GITHUB_REPO,
all_sources=list(FEED_CONFIG.keys()) # Pass source names to template
)
with open("index.html", "w", encoding="utf-8") as f:
f.write(output_html)
print("[+] index.html generated successfully.")
if __name__ == "__main__":
main()