-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·1252 lines (1048 loc) · 46 KB
/
app.py
File metadata and controls
executable file
·1252 lines (1048 loc) · 46 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
YaraMan - YARA Rules Manager & File Scanner
A standalone Flask application for managing YARA rules and scanning files.
"""
from flask import Flask, render_template, request, jsonify, send_from_directory, make_response
import os
import json
import zipfile
import yara
import hashlib
import sqlite3
from datetime import datetime
from werkzeug.utils import secure_filename
import re
from werkzeug.security import generate_password_hash, check_password_hash
from functools import wraps
import configparser
import secrets
from datetime import timedelta
def load_config(config_file='app.conf'):
"""Load configuration from app.conf file with environment variable override"""
config = configparser.ConfigParser()
# Set default values
defaults = {
'application': {
'debug': 'false',
'host': '0.0.0.0',
'port': '5002',
'secret_key': 'yaraman-default-secret-key-change-in-production'
},
'directories': {
'yara_rules_folder': 'yara_rules',
'upload_folder': 'uploads'
},
'limits': {
'max_content_length': '104857600',
'max_string_instances': '5',
'max_strings_per_match': '10',
'max_match_data_length': '100'
},
'authentication': {
'admin_username': 'admin',
'admin_password': 'admin123'
},
'security': {
'enable_cors': 'false',
'allowed_origins': '',
'file_extensions_yara': '.yar,.yara',
'file_extensions_upload': ''
},
'logging': {
'log_level': 'INFO',
'log_file': '',
'console_logging': 'true'
},
'scanning': {
'compile_timeout': '30',
'scan_timeout': '60',
'chunk_size': '4096'
}
}
# Load defaults first
config.read_dict(defaults)
# Load from config file if it exists
if os.path.exists(config_file):
config.read(config_file)
# Environment variable overrides
env_mappings = {
'SECRET_KEY': ('application', 'secret_key'),
'ADMIN_USERNAME': ('authentication', 'admin_username'),
'ADMIN_PASSWORD': ('authentication', 'admin_password'),
'YARA_RULES_FOLDER': ('directories', 'yara_rules_folder'),
'UPLOAD_FOLDER': ('directories', 'upload_folder'),
'DEBUG': ('application', 'debug'),
'HOST': ('application', 'host'),
'PORT': ('application', 'port')
}
for env_var, (section, key) in env_mappings.items():
value = os.environ.get(env_var)
if value:
config.set(section, key, value)
return config
def secure_path(path):
"""Secure a file path while preserving directory structure"""
if not path:
return ""
# Normalize path separators to forward slashes
path = path.replace('\\', '/')
# Remove any leading/trailing slashes
path = path.strip('/')
# Split into components and secure each part
parts = path.split('/')
secured_parts = []
for part in parts:
# Skip empty parts and current/parent directory references
if not part or part in ('.', '..'):
continue
# Secure each filename component individually
secured_part = secure_filename(part)
if secured_part: # Only add non-empty parts
secured_parts.append(secured_part)
# Rejoin with forward slashes
return '/'.join(secured_parts)
# Load configuration
config = load_config()
app = Flask(__name__)
app.config['YARA_RULES_FOLDER'] = config.get('directories', 'yara_rules_folder')
app.config['UPLOAD_FOLDER'] = config.get('directories', 'upload_folder')
app.config['MAX_CONTENT_LENGTH'] = config.getint('limits', 'max_content_length')
app.config['SECRET_KEY'] = config.get('application', 'secret_key')
app.config['ADMIN_USERNAME'] = config.get('authentication', 'admin_username')
app.config['ADMIN_PASSWORD'] = config.get('authentication', 'admin_password')
app.config['DEBUG'] = config.getboolean('application', 'debug')
app.config['HOST'] = config.get('application', 'host')
app.config['PORT'] = config.getint('application', 'port')
# Additional config for scanning limits
app.config['MAX_STRING_INSTANCES'] = config.getint('limits', 'max_string_instances')
app.config['MAX_STRINGS_PER_MATCH'] = config.getint('limits', 'max_strings_per_match')
app.config['MAX_MATCH_DATA_LENGTH'] = config.getint('limits', 'max_match_data_length')
app.config['CHUNK_SIZE'] = config.getint('scanning', 'chunk_size')
def init_database():
"""Initialize SQLite database for user management"""
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'yaraman.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Create users table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'admin',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_login TIMESTAMP
)
''')
# Create sessions table for complete server-side session management
cursor.execute('''
CREATE TABLE IF NOT EXISTS user_sessions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
session_id TEXT UNIQUE NOT NULL,
user_id INTEGER NOT NULL,
username TEXT NOT NULL,
role TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL,
last_accessed TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
is_valid BOOLEAN DEFAULT 1,
ip_address TEXT,
user_agent TEXT,
FOREIGN KEY (user_id) REFERENCES users (id)
)
''')
# Create yara_rules table
cursor.execute('''
CREATE TABLE IF NOT EXISTS yara_rules (
id INTEGER PRIMARY KEY AUTOINCREMENT,
filename TEXT NOT NULL,
full_path TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Add full_path column if it doesn't exist (for existing databases)
cursor.execute('''
PRAGMA table_info(yara_rules)
''')
columns = [column[1] for column in cursor.fetchall()]
if 'full_path' not in columns:
cursor.execute('''
ALTER TABLE yara_rules ADD COLUMN full_path TEXT DEFAULT ''
''')
# Create default admin user if none exists
cursor.execute('SELECT COUNT(*) FROM users WHERE role = "admin"')
admin_count = cursor.fetchone()[0]
if admin_count == 0:
admin_username = app.config['ADMIN_USERNAME']
admin_password = app.config['ADMIN_PASSWORD']
password_hash = generate_password_hash(admin_password)
cursor.execute('''
INSERT INTO users (username, password_hash, role)
VALUES (?, ?, ?)
''', (admin_username, password_hash, 'admin'))
print(f"Created default admin user: {admin_username}")
conn.commit()
conn.close()
def add_yara_rule_to_db(filename, full_path):
"""Add a YARA rule to the database"""
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'yaraman.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO yara_rules (filename, full_path)
VALUES (?, ?)
''', (filename, full_path))
rule_id = cursor.lastrowid
conn.commit()
conn.close()
return rule_id
def get_yara_rule_from_db(rule_id):
"""Get a YARA rule by ID from database"""
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'yaraman.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute('''
SELECT id, filename, full_path, created_at
FROM yara_rules
WHERE id = ?
''', (rule_id,))
result = cursor.fetchone()
conn.close()
if result:
return {
'id': result[0],
'filename': result[1],
'full_path': result[2],
'created_at': result[3]
}
return None
def delete_yara_rule_from_db(rule_id):
"""Delete a YARA rule from database"""
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'yaraman.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute('''
DELETE FROM yara_rules
WHERE id = ?
''', (rule_id,))
deleted = cursor.rowcount > 0
conn.commit()
conn.close()
return deleted
def get_all_yara_rules_from_db():
"""Get all YARA rules from database"""
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'yaraman.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute('''
SELECT id, filename, full_path, created_at
FROM yara_rules
ORDER BY filename
''')
results = cursor.fetchall()
conn.close()
return [{
'id': row[0],
'filename': row[1],
'full_path': row[2],
'created_at': row[3]
} for row in results]
def authenticate_user(username, password):
"""Authenticate user against database"""
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'yaraman.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute('''
SELECT id, username, password_hash, role
FROM users
WHERE username = ? AND role = 'admin'
''', (username,))
user = cursor.fetchone()
conn.close()
if user and check_password_hash(user[2], password):
return {
'id': user[0],
'username': user[1],
'role': user[3]
}
return None
def create_session(user_info, ip_address=None, user_agent=None):
"""Create a new server-side session"""
session_id = secrets.token_urlsafe(32)
expires_at = datetime.now() + timedelta(hours=24) # 24 hour session
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'yaraman.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Invalidate all existing sessions for this user
cursor.execute('''
UPDATE user_sessions
SET is_valid = 0
WHERE user_id = ? AND is_valid = 1
''', (user_info['id'],))
# Create new session with all user data stored server-side
cursor.execute('''
INSERT INTO user_sessions (
session_id, user_id, username, role, expires_at,
ip_address, user_agent
) VALUES (?, ?, ?, ?, ?, ?, ?)
''', (session_id, user_info['id'], user_info['username'],
user_info['role'], expires_at, ip_address, user_agent))
# Update last login timestamp
cursor.execute('''
UPDATE users
SET last_login = CURRENT_TIMESTAMP
WHERE id = ?
''', (user_info['id'],))
conn.commit()
conn.close()
return session_id
def get_session_data(session_id):
"""Get session data from server-side storage"""
if not session_id:
return None
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'yaraman.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute('''
SELECT user_id, username, role, expires_at, ip_address, user_agent
FROM user_sessions
WHERE session_id = ?
AND is_valid = 1
AND expires_at > CURRENT_TIMESTAMP
''', (session_id,))
result = cursor.fetchone()
if result:
# Update last accessed time
cursor.execute('''
UPDATE user_sessions
SET last_accessed = CURRENT_TIMESTAMP
WHERE session_id = ?
''', (session_id,))
conn.commit()
conn.close()
return {
'user_id': result[0],
'username': result[1],
'role': result[2],
'expires_at': result[3],
'ip_address': result[4],
'user_agent': result[5]
}
conn.close()
return None
def invalidate_session(session_id):
"""Invalidate a specific session"""
if not session_id:
return
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'yaraman.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute('''
UPDATE user_sessions
SET is_valid = 0
WHERE session_id = ?
''', (session_id,))
conn.commit()
conn.close()
def invalidate_user_sessions(user_id):
"""Invalidate all sessions for a user"""
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'yaraman.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute('''
UPDATE user_sessions
SET is_valid = 0
WHERE user_id = ? AND is_valid = 1
''', (user_id,))
conn.commit()
conn.close()
def cleanup_expired_sessions():
"""Clean up expired sessions"""
db_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'yaraman.db')
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
cursor.execute('''
DELETE FROM user_sessions
WHERE expires_at < CURRENT_TIMESTAMP OR is_valid = 0
''')
conn.commit()
conn.close()
def get_session_id():
"""Get session ID from request cookie"""
return request.cookies.get('yaraman_session_id')
def set_session_cookie(response, session_id):
"""Set secure session cookie"""
response.set_cookie(
'yaraman_session_id',
session_id,
max_age=24*60*60, # 24 hours
httponly=True, # Prevent JavaScript access
secure=False, # Set to True in production with HTTPS
samesite='Lax' # CSRF protection
)
return response
def clear_session_cookie(response):
"""Clear session cookie"""
response.set_cookie(
'yaraman_session_id',
'',
expires=0,
httponly=True,
secure=False,
samesite='Lax'
)
return response
def admin_required(f):
"""Decorator to require admin authentication for YARA management endpoints"""
@wraps(f)
def decorated_function(*args, **kwargs):
session_id = get_session_id()
if not session_id:
return jsonify({'error': 'Admin authentication required'}), 401
# Get session data from server-side storage
session_data = get_session_data(session_id)
if not session_data or session_data['role'] != 'admin':
return jsonify({'error': 'Admin authentication required'}), 401
# Clean up expired sessions periodically
cleanup_expired_sessions()
return f(*args, **kwargs)
return decorated_function
def validate_yara_rule(rule_content):
"""Validate YARA rule syntax"""
try:
yara.compile(source=rule_content)
return True, None
except yara.SyntaxError as e:
return False, str(e)
except Exception as e:
return False, f"Validation error: {str(e)}"
def get_yara_rules():
"""Get list of available YARA rules from database"""
rules = []
db_rules = get_all_yara_rules_from_db()
yara_folder = app.config['YARA_RULES_FOLDER']
for db_rule in db_rules:
# Use full_path for file operations
filepath = os.path.join(yara_folder, db_rule['full_path']) if db_rule['full_path'] else os.path.join(yara_folder, db_rule['filename'])
try:
if os.path.exists(filepath):
stat = os.stat(filepath)
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
# Extract rule names from content
rule_names = []
lines = content.split('\n')
for i, line in enumerate(lines):
line = line.strip()
if line.startswith('rule '):
# Handle both "rule name {" and "rule name\n{"
if '{' in line:
rule_name = line.split('rule ')[1].split()[0].rstrip('{').rstrip(':')
else:
rule_name = line.split('rule ')[1].split()[0].rstrip(':')
rule_names.append(rule_name)
rules.append({
'id': db_rule['id'],
'filename': db_rule['filename'],
'full_path': db_rule['full_path'],
'display_name': db_rule['filename'], # Use filename as display name (just the filename, no path)
'size': stat.st_size,
'modified': datetime.fromtimestamp(stat.st_mtime).isoformat(),
'rule_names': rule_names,
'rule_count': len(rule_names)
})
else:
# File exists in DB but not on filesystem
rules.append({
'id': db_rule['id'],
'filename': db_rule['filename'],
'full_path': db_rule['full_path'],
'display_name': db_rule['filename'],
'size': 0,
'modified': '',
'rule_names': [],
'rule_count': 0,
'error': 'File not found on filesystem'
})
except Exception as e:
rules.append({
'id': db_rule['id'],
'filename': db_rule['filename'],
'full_path': db_rule['full_path'],
'display_name': db_rule['filename'],
'size': 0,
'modified': '',
'rule_names': [],
'rule_count': 0,
'error': str(e)
})
return sorted(rules, key=lambda x: x['display_name'])
def compile_yara_rules():
"""Compile all YARA rules for scanning"""
yara_folder = app.config['YARA_RULES_FOLDER']
if not os.path.exists(yara_folder):
return None
compilation_errors = []
db_rules = get_all_yara_rules_from_db()
# Build a dictionary of filepaths with proper namespace handling
all_filepaths = {}
old_cwd = os.getcwd()
try:
# Don't change working directory yet - first validate paths with absolute paths
print(f"Processing {len(db_rules)} rules from database...")
for i, db_rule in enumerate(db_rules):
# Use full_path for file operations
if db_rule['full_path']:
relative_filepath = db_rule['full_path']
else:
relative_filepath = db_rule['filename']
# Full absolute path to the file
full_filepath = os.path.join(yara_folder, relative_filepath)
if os.path.exists(full_filepath):
try:
# Test compile individual rule first to validate
# Change to the specific directory where the rule is located
rule_dir = os.path.dirname(full_filepath)
if rule_dir and rule_dir != yara_folder:
test_cwd = os.getcwd()
os.chdir(rule_dir)
# Use just the filename for compilation from its directory
yara.compile(filepath=os.path.basename(full_filepath))
os.chdir(test_cwd)
print(f"Successfully validated rule: {db_rule['filename']}")
else:
# If rule is in root yara folder, compile with relative path
yara.compile(filepath=relative_filepath)
print(f"Successfully validated rule: {db_rule['filename']}")
# Add to compilation list using relative path from yara_folder
namespace = f"rules_{i}"
all_filepaths[namespace] = relative_filepath
except Exception as rule_error:
print(f"Error validating YARA rule {db_rule['filename']}: {rule_error}")
compilation_errors.append(f"{db_rule['filename']}: {str(rule_error)}")
else:
print(f"YARA rule file not found: {full_filepath}")
compilation_errors.append(f"{db_rule['filename']}: File not found")
if not all_filepaths:
print("No valid YARA rules found for compilation")
if compilation_errors:
print("Compilation errors:")
for error in compilation_errors:
print(f" - {error}")
return None
# Final compilation of all valid rules
# Change to yara rules folder for final compilation to resolve includes
os.chdir(yara_folder)
compiled_rules = yara.compile(filepaths=all_filepaths)
print(f"Successfully compiled {len(all_filepaths)} YARA rule file(s)")
if compilation_errors:
print("Some rules were skipped:")
for error in compilation_errors:
print(f" - {error}")
return compiled_rules
except Exception as e:
print(f"Error compiling YARA rules: {e}")
return None
finally:
# Restore working directory
os.chdir(old_cwd)
def scan_file_with_yara(file_path):
"""Scan a file with compiled YARA rules"""
compiled_rules = compile_yara_rules()
if not compiled_rules:
return {
'filename': os.path.basename(file_path),
'size': os.path.getsize(file_path) if os.path.exists(file_path) else 0,
'matches': [],
'error': 'No YARA rules available for scanning'
}
try:
# Get file info
file_size = os.path.getsize(file_path)
filename = os.path.basename(file_path)
# Scan file
matches = compiled_rules.match(filepath=file_path)
# Filter out generic rules with excessive matches
filtered_matches = []
filtered_count = 0
for match in matches:
if is_generic_rule_match(match):
print(f"Filtering out generic rule match: {match.rule} (too many null bytes/wildcards)")
filtered_count += 1
continue
filtered_matches.append(match)
if filtered_count > 0:
print(f"Filtered out {filtered_count} generic rule matches, showing {len(filtered_matches)} quality matches")
# Format matches
formatted_matches = []
for match in filtered_matches:
formatted_match = {
'rule': match.rule,
'tags': list(match.tags),
'namespace': match.namespace,
'meta': {key: value for key, value in match.meta.items()},
'strings': []
}
for s in match.strings[:app.config['MAX_STRINGS_PER_MATCH']]:
string_info = {
'identifier': s.identifier,
'instances': [],
'total_instances': len(s.instances)
}
for instance in s.instances[:app.config['MAX_STRING_INSTANCES']]:
raw_data = instance.matched_data
hex_data = raw_data.hex().upper()
# Try to show printable ASCII, fallback to hex if mostly binary
try:
ascii_data = raw_data.decode('ascii', errors='ignore')
printable_chars = sum(1 for c in ascii_data if c.isprintable())
if len(ascii_data) > 0 and printable_chars / len(ascii_data) > 0.7:
display_data = ascii_data[:app.config['MAX_MATCH_DATA_LENGTH']]
else:
# Show hex data in readable format (space every 2 chars)
display_data = ' '.join(hex_data[i:i+2] for i in range(0, min(len(hex_data), app.config['MAX_MATCH_DATA_LENGTH']*2), 2))
except:
# Fallback to hex display
display_data = ' '.join(hex_data[i:i+2] for i in range(0, min(len(hex_data), app.config['MAX_MATCH_DATA_LENGTH']*2), 2))
string_info['instances'].append({
'offset': instance.offset,
'matched_length': len(raw_data),
'match_data': display_data,
'hex_data': ' '.join(hex_data[i:i+2] for i in range(0, min(len(hex_data), 200), 2)) # First 100 bytes as hex
})
formatted_match['strings'].append(string_info)
formatted_matches.append(formatted_match)
return {
'filename': filename,
'size': file_size,
'matches': formatted_matches,
'md5': get_file_hash(file_path, 'md5'),
'sha256': get_file_hash(file_path, 'sha256')
}
except Exception as e:
return {
'filename': os.path.basename(file_path),
'size': os.path.getsize(file_path) if os.path.exists(file_path) else 0,
'matches': [],
'error': f"Scan error: {str(e)}"
}
def is_generic_rule_match(match):
"""
Analyze a YARA match to determine if it comes from a generic/low-quality rule.
This filters out rules that produce too many matches due to generic patterns.
Args:
match: YARA match object
Returns:
bool: True if match should be filtered out (generic rule), False otherwise
"""
try:
# Criteria for generic rules:
# 1. Excessive number of matches (1000+ matches usually means overly generic pattern)
# 2. Matches that are too short (less than 8 bytes)
# 3. Matches consisting mostly of null bytes
total_string_matches = len(match.strings)
if total_string_matches == 0:
return False # No string data to analyze
# Count total instances across all strings
total_instances = 0
null_heavy_strings = 0
for string_match in match.strings:
instances = getattr(string_match, 'instances', [])
total_instances += len(instances)
# Analyze the actual matched data for null byte content
for instance in instances[:5]: # Check first few instances
matched_data = getattr(instance, 'matched_data', b'')
if len(matched_data) > 0:
null_count = matched_data.count(b'\x00')
null_ratio = null_count / len(matched_data)
# Flag as generic if >70% null bytes and >50 bytes long
if null_ratio > 0.7 and len(matched_data) > 50:
null_heavy_strings += 1
break # One null-heavy instance is enough
# Filter criteria:
# 1. More than 500 total instances (extremely generic)
if total_instances > 500:
print(f"Rule {match.rule} flagged: {total_instances} instances (too many)")
return True
# 2. Multiple strings with high null byte content
if null_heavy_strings > 0 and total_instances > 100:
print(f"Rule {match.rule} flagged: null-heavy pattern with {total_instances} instances")
return True
# 3. Special case: Rules with patterns like Microsoft_Visual_Cpp that match at wrong locations
# If rule name suggests it should match at entry point but has many scattered matches
rule_name = match.rule.lower()
if any(keyword in rule_name for keyword in ['microsoft', 'visual', 'cpp', 'compiler']):
if total_instances > 50:
print(f"Rule {match.rule} flagged: compiler signature with too many matches ({total_instances})")
return True
return False
except Exception as e:
print(f"Error analyzing match quality for {match.rule}: {e}")
return False # If we can't analyze, don't filter it out
def get_file_hash(file_path, hash_type='sha256'):
"""Calculate file hash"""
hash_func = hashlib.new(hash_type)
try:
with open(file_path, 'rb') as f:
chunk_size = app.config['CHUNK_SIZE']
for chunk in iter(lambda: f.read(chunk_size), b""):
hash_func.update(chunk)
return hash_func.hexdigest()
except Exception:
return None
@app.route('/')
@app.route('/<path:path>')
def index(path=''):
"""Main page - catch all routes for SPA"""
# Serve API routes normally (they have their own handlers)
if path.startswith('api/') or path.startswith('assets/'):
# Let Flask handle 404 for missing API/asset routes
from flask import abort
abort(404)
# Serve index.html for all other routes (SPA routing)
return send_from_directory('.', 'index.html')
@app.route('/api/ui/admin-features')
def get_admin_features():
"""API endpoint to get admin UI features based on authentication"""
session_id = get_session_id()
admin_logged_in = False
if session_id:
session_data = get_session_data(session_id)
if session_data and session_data['role'] == 'admin':
admin_logged_in = True
return jsonify({
'show_upload_button': admin_logged_in,
'show_delete_buttons': admin_logged_in,
'show_admin_dropdown': admin_logged_in,
'show_login_button': not admin_logged_in,
'can_access_rules_page': admin_logged_in
})
@app.route('/assets/<path:filename>')
def assets(filename):
"""Serve static assets"""
return send_from_directory('assets', filename)
@app.route('/api/auth/login', methods=['POST'])
def admin_login():
"""Admin login endpoint"""
try:
data = request.get_json()
if not data:
return jsonify({'error': 'No data provided'}), 400
username = data.get('username')
password = data.get('password')
if not username or not password:
return jsonify({'error': 'Username and password required'}), 400
user = authenticate_user(username, password)
if user:
# Get client info for session tracking
ip_address = request.environ.get('HTTP_X_FORWARDED_FOR', request.environ.get('REMOTE_ADDR'))
user_agent = request.headers.get('User-Agent')
# Create new server-side session
session_id = create_session(user, ip_address, user_agent)
# Create response with session cookie
response = make_response(jsonify({'message': 'Login successful', 'admin': True}))
response = set_session_cookie(response, session_id)
return response
else:
return jsonify({'error': 'Invalid credentials'}), 401
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/auth/logout', methods=['POST'])
def admin_logout():
"""Admin logout endpoint"""
session_id = get_session_id()
# Invalidate the session in database
if session_id:
invalidate_session(session_id)
# Create response and clear session cookie
response = make_response(jsonify({'message': 'Logout successful'}))
response = clear_session_cookie(response)
return response
@app.route('/api/auth/status')
def auth_status():
"""Check current authentication status"""
session_id = get_session_id()
admin_logged_in = False
username = None
if session_id:
session_data = get_session_data(session_id)
if session_data and session_data['role'] == 'admin':
admin_logged_in = True
username = session_data['username']
return jsonify({
'admin_logged_in': admin_logged_in,
'username': username
})
@app.route('/api/yara/rules')
def list_yara_rules():
"""API endpoint to list available YARA rules"""
try:
rules = get_yara_rules()
return jsonify({'rules': rules})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/yara/rules/<int:rule_id>')
def get_yara_rule(rule_id):
"""API endpoint to get a specific YARA rule content by ID"""
try:
# Get rule from database
db_rule = get_yara_rule_from_db(rule_id)
if not db_rule:
return jsonify({'error': 'Rule not found'}), 404
# Use full_path for file operations
filepath = os.path.join(app.config['YARA_RULES_FOLDER'], db_rule['full_path']) if db_rule['full_path'] else os.path.join(app.config['YARA_RULES_FOLDER'], db_rule['filename'])
if not os.path.exists(filepath):
return jsonify({'error': 'Rule file not found on filesystem'}), 404
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
return jsonify({
'id': db_rule['id'],
'filename': db_rule['filename'],
'full_path': db_rule['full_path'],
'display_name': db_rule['filename'],
'content': content
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/yara/rules', methods=['POST'])
@admin_required
def upload_yara_rule():
"""API endpoint to upload YARA rule(s)"""
try:
# Handle file upload
if 'file' not in request.files:
return jsonify({'error': 'No file provided'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
yara_folder = app.config['YARA_RULES_FOLDER']
if not os.path.exists(yara_folder):
os.makedirs(yara_folder)
results = []
# Handle ZIP files
if file.filename.lower().endswith('.zip'):
try:
with zipfile.ZipFile(file) as zip_file:
allowed_extensions = tuple(ext.strip() for ext in config.get('security', 'file_extensions_yara').split(','))
# First pass: Extract all files to temporary locations
extracted_files = []
for zip_info in zip_file.infolist():
if zip_info.filename.endswith(allowed_extensions) and not zip_info.is_dir():
rule_content = zip_file.read(zip_info).decode('utf-8')
original_path = zip_info.filename
rule_filename = os.path.basename(original_path)
rule_full_path = secure_path(original_path)
rule_path = os.path.join(yara_folder, rule_full_path)
# Create subdirectories if they don't exist