-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
1304 lines (1183 loc) · 63.2 KB
/
index.html
File metadata and controls
1304 lines (1183 loc) · 63.2 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
<!DOCTYPE html>
<html lang="en" data-bs-theme="auto">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YaraMan - YARA Rules Manager</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-LN+7fdVzj6u52u30Kp6M/trliBMCMKTyK833zpbD+pXdCLuTusPj697FH4R/5mcr" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="/assets/styles.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.13.1/font/bootstrap-icons.min.css">
<script src="/assets/color-modes.js"></script>
</head>
<body>
<!-- Theme Toggle -->
<div class="dropdown position-fixed bottom-0 end-0 mb-3 me-3 bd-mode-toggle">
<button class="btn btn-bd-primary py-2 dropdown-toggle d-flex align-items-center"
id="bd-theme"
type="button"
aria-expanded="false"
data-bs-toggle="dropdown"
aria-label="Toggle theme (auto)">
<svg class="bi my-1 theme-icon-active" width="1em" height="1em"><use href="#circle-half"></use></svg>
<span class="visually-hidden" id="bd-theme-text">Toggle theme</span>
</button>
<ul class="dropdown-menu dropdown-menu-end shadow" aria-labelledby="bd-theme-text">
<li>
<button type="button" class="dropdown-item d-flex align-items-center" data-bs-theme-value="light" aria-pressed="false">
<svg class="bi me-2 opacity-50" width="1em" height="1em"><use href="#sun-fill"></use></svg>
Light
<svg class="bi ms-auto d-none" width="1em" height="1em"><use href="#check2"></use></svg>
</button>
</li>
<li>
<button type="button" class="dropdown-item d-flex align-items-center" data-bs-theme-value="dark" aria-pressed="false">
<svg class="bi me-2 opacity-50" width="1em" height="1em"><use href="#moon-stars-fill"></use></svg>
Dark
<svg class="bi ms-auto d-none" width="1em" height="1em"><use href="#check2"></use></svg>
</button>
</li>
<li>
<button type="button" class="dropdown-item d-flex align-items-center active" data-bs-theme-value="auto" aria-pressed="true">
<svg class="bi me-2 opacity-50" width="1em" height="1em"><use href="#circle-half"></use></svg>
Auto
<svg class="bi ms-auto d-none" width="1em" height="1em"><use href="#check2"></use></svg>
</button>
</li>
</ul>
</div>
<!-- Navbar -->
<nav class="navbar navbar-expand-lg navbar-light bg-white shadow-sm sticky-top">
<div class="container-fluid">
<a class="navbar-brand fw-bold text-primary d-flex align-items-center" href="/">
<i class="fas fa-shield-virus me-2"></i>
YaraMan
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav me-auto">
<li class="nav-item">
<a class="nav-link active" href="/" id="nav-scanner">
<i class="fas fa-search me-1"></i>Scanner
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/rules" id="nav-rules">
<i class="fas fa-shield-alt me-1"></i>YARA Rules
</a>
</li>
</ul>
<ul class="navbar-nav">
<li class="nav-item" id="auth-section">
<button class="btn btn-outline-primary btn-sm" id="login-btn" onclick="showLoginModal()">
<i class="fas fa-sign-in-alt me-1"></i>Admin Login
</button>
<div class="dropdown" id="admin-dropdown" style="display: none;">
<button class="btn btn-success btn-sm dropdown-toggle" type="button" data-bs-toggle="dropdown">
<i class="fas fa-user-shield me-1"></i>Admin
</button>
<ul class="dropdown-menu dropdown-menu-end">
<li><button class="dropdown-item" onclick="logout()">
<i class="fas fa-sign-out-alt me-1"></i>Logout
</button></li>
</ul>
</div>
</li>
</ul>
</div>
</div>
</nav>
<div class="container mt-4 fade-in">
<!-- Header -->
<div class="text-center mb-5">
<h1 class="display-4 fw-bold mb-3">YaraMan</h1>
<p class="lead text-muted mb-4">YARA Rules Manager & File Scanner</p>
</div>
<!-- Main Content -->
<div id="main-content">
<!-- File Scanner Page -->
<div id="scanner-page" style="display: block;">
<div class="row">
<div class="col-lg-12">
<div class="text-center mb-4">
<h2 class="fw-bold mb-2">File Scanner</h2>
<p class="text-muted">Upload files to scan against your YARA rules</p>
</div>
<!-- Upload Form -->
<div class="row justify-content-center">
<div class="col-xl-8 col-lg-10">
<div class="card shadow-lg border-0">
<div class="card-header text-white">
<h5 class="card-title mb-0 d-flex align-items-center">
<i class="fas fa-search me-2"></i>
Upload Files for Scanning
</h5>
</div>
<div class="card-body">
<form id="scan-form" method="POST" enctype="multipart/form-data">
<div class="mb-4">
<label for="scan-files" class="form-label d-flex align-items-center">
<i class="fas fa-file me-2 text-primary"></i>
<strong>Select Files to Scan</strong>
</label>
<input type="file" class="form-control" id="scan-files" name="files" multiple required>
<div class="form-text">
Select one or more files to scan against your YARA rules.
</div>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary btn-lg">
<i class="fas fa-search me-2"></i>
Scan Files
</button>
</div>
</form>
<!-- Scan Progress -->
<div id="scan-progress" class="mt-4" style="display: none;">
<h6>Scanning Progress:</h6>
<div class="progress mb-2">
<div class="progress-bar progress-bar-striped progress-bar-animated"
role="progressbar" style="width: 0%" id="scan-progress-bar">
<span id="scan-progress-text">0%</span>
</div>
</div>
<small class="text-muted" id="scan-status">Preparing scan...</small>
</div>
</div>
</div>
</div>
</div>
<!-- Scan Results -->
<div id="scan-results" class="mt-4" style="display: none;">
<div class="card shadow-sm border-0">
<div class="card-header text-white">
<h5 class="card-title mb-0">
<i class="fas fa-clipboard-list me-2"></i>
Scan Results
</h5>
</div>
<div class="card-body">
<div id="results-container"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- YARA Rules Management Page -->
<div id="rules-page" style="display: none;">
<div class="row">
<div class="col-lg-12">
<div class="d-flex justify-content-between align-items-center mb-4">
<div>
<h2 class="fw-bold mb-2">YARA Rules Management</h2>
<p class="text-muted">Manage and upload YARA rules for malware detection and analysis</p>
</div>
<div class="btn-group" role="group">
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#uploadModal" id="upload-rules-btn">
<i class="fas fa-upload me-2"></i>Upload Rules
</button>
<button class="btn btn-outline-warning" onclick="removeAllNonExistentRules()" id="remove-non-existent-btn" style="display: none;">
<i class="fas fa-broom me-2"></i>Cleanup Orphaned Rules
</button>
</div>
</div>
<!-- Statistics Cards -->
<div class="row mb-4">
<div class="col-md-3">
<div class="card bg-primary bg-opacity-10 border-0">
<div class="card-body text-center">
<i class="fas fa-file-code fa-2x text-primary mb-2"></i>
<h4 class="fw-bold" id="total-rules">-</h4>
<small class="text-muted">Total Rule Files</small>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card bg-success bg-opacity-10 border-0">
<div class="card-body text-center">
<i class="fas fa-shield-check fa-2x text-success mb-2"></i>
<h4 class="fw-bold" id="total-rule-count">-</h4>
<small class="text-muted">Individual Rules</small>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card bg-warning bg-opacity-10 border-0">
<div class="card-body text-center">
<i class="fas fa-clock fa-2x text-warning mb-2"></i>
<h4 class="fw-bold" id="recent-uploads">-</h4>
<small class="text-muted">Recent Uploads</small>
</div>
</div>
</div>
<div class="col-md-3">
<div class="card bg-info bg-opacity-10 border-0">
<div class="card-body text-center">
<i class="fas fa-hdd fa-2x text-info mb-2"></i>
<h4 class="fw-bold" id="total-size">-</h4>
<small class="text-muted">Total Size</small>
</div>
</div>
</div>
</div>
<!-- Rules Table -->
<div class="card shadow-sm border-0">
<div class="card-header text-white">
<h5 class="card-title mb-0 d-flex align-items-center justify-content-between">
<span>
<i class="fas fa-list me-2"></i>
Available YARA Rules
</span>
<button class="btn btn-light btn-sm" onclick="refreshRules()">
<i class="fas fa-sync-alt me-1"></i>Refresh
</button>
</h5>
</div>
<div class="card-body p-0">
<div class="table-responsive">
<table class="table table-hover mb-0">
<thead class="table-light">
<tr>
<th><i class="fas fa-file me-1"></i>Filename</th>
<th><i class="fas fa-shield-alt me-1"></i>Rules</th>
<th><i class="fas fa-hdd me-1"></i>Size</th>
<th><i class="fas fa-clock me-1"></i>Modified</th>
<th><i class="fas fa-cogs me-1"></i>Actions</th>
</tr>
</thead>
<tbody id="rules-table">
<tr>
<td colspan="5" class="text-center py-4">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<p class="mt-2 text-muted">Loading YARA rules...</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Admin Login Modal -->
<div class="modal fade" id="loginModal" tabindex="-1" aria-labelledby="loginModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="loginModalLabel">
<i class="fas fa-sign-in-alt text-primary me-2"></i>
Admin Login
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form id="login-form">
<div class="mb-3">
<label for="admin-username" class="form-label">
<i class="fas fa-user me-1"></i>Username
</label>
<input type="text" class="form-control" id="admin-username" name="username" required>
</div>
<div class="mb-3">
<label for="admin-password" class="form-label">
<i class="fas fa-lock me-1"></i>Password
</label>
<input type="password" class="form-control" id="admin-password" name="password" required>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary">
<i class="fas fa-sign-in-alt me-2"></i>Login
</button>
</div>
</form>
<!-- Login Error -->
<div id="login-error" class="mt-3" style="display: none;">
<div class="alert alert-danger">
<i class="fas fa-exclamation-triangle me-2"></i>
<span id="login-error-message">Invalid credentials</span>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Upload Modal -->
<div class="modal fade" id="uploadModal" tabindex="-1" aria-labelledby="uploadModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="uploadModalLabel">
<i class="fas fa-upload text-primary me-2"></i>
Upload YARA Rules
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<p class="text-muted">
<strong>Supported formats:</strong>
</p>
<ul class="text-muted">
<li><code>.yar</code> and <code>.yara</code> files - Individual YARA rule files</li>
<li><code>.zip</code> files - Archive containing multiple YARA rules (will be extracted)</li>
</ul>
</div>
<form id="upload-form" enctype="multipart/form-data">
<div class="mb-3">
<label for="file-input" class="form-label">
<i class="fas fa-file-upload me-1"></i>
Select YARA Rule File(s)
</label>
<input type="file" class="form-control" id="file-input" name="file"
accept=".yar,.yara,.zip" required>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary">
<i class="fas fa-upload me-2"></i>Upload Rules
</button>
</div>
</form>
<!-- Upload Progress -->
<div id="upload-progress" class="mt-3" style="display: none;">
<div class="progress">
<div class="progress-bar progress-bar-striped progress-bar-animated"
role="progressbar" style="width: 0%" id="progress-bar">
<span id="progress-text">0%</span>
</div>
</div>
</div>
<!-- Upload Results -->
<div id="upload-results" class="mt-3" style="display: none;">
<h6>Upload Results:</h6>
<div id="upload-results-container"></div>
</div>
</div>
</div>
</div>
</div>
<!-- Rule Details Modal -->
<div class="modal fade" id="ruleDetailsModal" tabindex="-1" aria-labelledby="ruleDetailsModalLabel" aria-hidden="true">
<div class="modal-dialog modal-xl">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="ruleDetailsModalLabel">
<i class="fas fa-code text-primary me-2"></i>
YARA Rule Details
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div id="rule-content">
<div class="text-center py-4">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<p class="mt-2 text-muted">Loading rule content...</p>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- All Rules Modal -->
<div class="modal fade" id="allRulesModal" tabindex="-1" aria-labelledby="allRulesModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="allRulesModalLabel">
<i class="fas fa-shield-alt text-primary me-2"></i>
All Rules
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div id="all-rules-content">
<div class="text-center py-4">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<p class="mt-2 text-muted">Loading rules...</p>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- SVG Icons for theme switching -->
<svg xmlns="http://www.w3.org/2000/svg" class="d-none">
<symbol id="check2" viewBox="0 0 16 16">
<path d="M13.854 3.646a.5.5 0 0 1 0 .708l-7 7a.5.5 0 0 1-.708 0l-3.5-3.5a.5.5 0 1 1 .708-.708L6.5 10.293l6.646-6.647a.5.5 0 0 1 .708 0z"/>
</symbol>
<symbol id="circle-half" viewBox="0 0 16 16">
<path d="M8 15A7 7 0 1 0 8 1v14zm0 1A8 8 0 1 1 8 0a8 8 0 0 1 0 16z"/>
</symbol>
<symbol id="moon-stars-fill" viewBox="0 0 16 16">
<path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278z"/>
<path d="M10.794 3.148a.217.217 0 0 1 .412 0l.387 1.162c.173.518.579.924 1.097 1.097l1.162.387a.217.217 0 0 1 0 .412l-1.162.387a1.734 1.734 0 0 0-1.097 1.097l-.387 1.162a.217.217 0 0 1-.412 0l-.387-1.162A1.734 1.734 0 0 0 9.31 6.593l-1.162-.387a.217.217 0 0 1 0-.412l1.162-.387a1.734 1.734 0 0 0 1.097-1.097l.387-1.162zM13.863.099a.145.145 0 0 1 .274 0l.258.774c.115.346.386.617.732.732l.774.258a.145.145 0 0 1 0 .274l-.774.258a1.156 1.156 0 0 0-.732.732l-.258.774a.145.145 0 0 1-.274 0l-.258-.774a1.156 1.156 0 0 0-.732-.732l-.774-.258a.145.145 0 0 1 0-.274l.774-.258c.346-.115.617-.386.732-.732L13.863.1z"/>
</symbol>
<symbol id="sun-fill" viewBox="0 0 16 16">
<path d="M8 12a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM8 0a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 0zm0 13a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 8 13zm8-5a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2a.5.5 0 0 1 .5.5zM3 8a.5.5 0 0 1-.5.5h-2a.5.5 0 0 1 0-1h2A.5.5 0 0 1 3 8zm10.657-5.657a.5.5 0 0 1 0 .707l-1.414 1.415a.5.5 0 1 1-.707-.708l1.414-1.414a.5.5 0 0 1 .707 0zm-9.193 9.193a.5.5 0 0 1 0 .707L3.05 13.657a.5.5 0 0 1-.707-.707l1.414-1.414a.5.5 0 0 1 .707 0zm9.193 2.121a.5.5 0 0 1-.707 0l-1.414-1.414a.5.5 0 0 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .707zM4.464 4.465a.5.5 0 0 1-.707 0L2.343 3.05a.5.5 0 1 1 .707-.707l1.414 1.414a.5.5 0 0 1 0 .708z"/>
</symbol>
</svg>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js" integrity="sha384-ndDqU0Gzau9qJ1lfW4pNLlhNTkCfHzAVBReH9diLvGRem5+R9g2FzA8ZGN954O5Q" crossorigin="anonymous"></script>
<script src="/assets/main.js"></script>
<script src="/assets/ui.js"></script>
<script>
// Navigation handling
let currentPage = 'scanner';
function showPage(page) {
// Check admin access for rules page server-side
if (page === 'rules') {
fetch('/api/ui/admin-features')
.then(response => response.json())
.then(data => {
if (!data.can_access_rules_page) {
showLoginModal();
return;
}
// Show rules page if authorized
switchToPage(page);
})
.catch(error => {
console.error('Error checking access:', error);
showLoginModal();
});
} else {
switchToPage(page);
}
}
function switchToPage(page) {
// Hide all pages
document.getElementById('scanner-page').style.display = 'none';
document.getElementById('rules-page').style.display = 'none';
// Show selected page
document.getElementById(page + '-page').style.display = 'block';
// Update navbar
document.querySelectorAll('.nav-link').forEach(link => {
link.classList.remove('active');
});
document.getElementById('nav-' + page).classList.add('active');
currentPage = page;
// Load rules when switching to rules page
if (page === 'rules') {
loadRules();
}
}
// Handle navigation clicks
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('nav-scanner').addEventListener('click', function(e) {
e.preventDefault();
updateURL('scanner');
showPage('scanner');
});
document.getElementById('nav-rules').addEventListener('click', function(e) {
e.preventDefault();
updateURL('rules');
showPage('rules');
});
// Initial page load
updateUIBasedOnAuth();
handleRouting();
});
// Handle URL routing
function handleRouting() {
const path = window.location.pathname;
if (path === '/rules') {
showPage('rules');
} else {
showPage('scanner');
}
}
// Update URL without page reload
function updateURL(page) {
const url = page === 'scanner' ? '/' : '/' + page;
window.history.pushState({ page: page }, '', url);
}
// Handle browser back/forward
window.addEventListener('popstate', function(e) {
if (e.state && e.state.page) {
showPage(e.state.page);
} else {
handleRouting();
}
});
// Authentication Functions
function updateUIBasedOnAuth() {
fetch('/api/ui/admin-features')
.then(response => response.json())
.then(data => {
updateAuthUI(data);
})
.catch(error => {
console.error('Error checking auth status:', error);
updateAuthUI({
show_upload_button: false,
show_delete_buttons: false,
show_admin_dropdown: false,
show_login_button: true,
can_access_rules_page: false
});
});
}
function updateAuthUI(features) {
const loginBtn = document.getElementById('login-btn');
const adminDropdown = document.getElementById('admin-dropdown');
const uploadBtn = document.getElementById('upload-rules-btn');
if (features.show_login_button) {
loginBtn.style.display = 'block';
} else {
loginBtn.style.display = 'none';
}
if (features.show_admin_dropdown) {
adminDropdown.style.display = 'block';
} else {
adminDropdown.style.display = 'none';
}
if (uploadBtn) {
if (features.show_upload_button) {
uploadBtn.style.display = 'block';
} else {
uploadBtn.style.display = 'none';
}
}
// Store features for use in displayRules
window.adminFeatures = features;
}
function showLoginModal() {
const modal = new bootstrap.Modal(document.getElementById('loginModal'));
document.getElementById('login-error').style.display = 'none';
document.getElementById('login-form').reset();
modal.show();
}
function login(username, password) {
return fetch('/api/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password })
})
.then(response => response.json())
.then(data => {
if (data.error) {
throw new Error(data.error);
}
// Update UI based on server response
updateUIBasedOnAuth();
return data;
});
}
function logout() {
fetch('/api/auth/logout', { method: 'POST' })
.then(response => response.json())
.then(data => {
// Update UI based on server response
updateUIBasedOnAuth();
// Redirect to scanner if on rules page
if (currentPage === 'rules') {
updateURL('scanner');
switchToPage('scanner');
}
})
.catch(error => {
console.error('Error logging out:', error);
});
}
// YARA Rules Management Functions
function loadRules() {
fetch('/api/yara/rules')
.then(response => response.json())
.then(data => {
if (data.error) {
showError('Failed to load rules: ' + data.error);
return;
}
displayRules(data.rules);
updateStatistics(data.rules);
})
.catch(error => {
console.error('Error loading rules:', error);
showError('Failed to load rules');
});
}
function displayRules(rules) {
const tableBody = document.getElementById('rules-table');
const removeNonExistentBtn = document.getElementById('remove-non-existent-btn');
// Check if there are any rules with errors (non-existent files)
const hasNonExistentRules = rules.some(rule => rule.error);
// Show/hide the bulk remove button based on admin status and presence of non-existent rules
if (removeNonExistentBtn && window.adminFeatures && window.adminFeatures.show_delete_buttons && hasNonExistentRules) {
removeNonExistentBtn.style.display = 'block';
} else if (removeNonExistentBtn) {
removeNonExistentBtn.style.display = 'none';
}
if (rules.length === 0) {
tableBody.innerHTML = `
<tr>
<td colspan="5" class="text-center py-4">
<i class="fas fa-file-alt fa-3x text-muted mb-3"></i>
<p class="text-muted">No YARA rules found. Upload some rules to get started!</p>
</td>
</tr>
`;
return;
}
tableBody.innerHTML = rules.map(rule => `
<tr>
<td>
<div class="d-flex align-items-center">
<i class="fas fa-file-code text-primary me-2"></i>
<span class="fw-semibold">${escapeHtml(rule.display_name || rule.filename)}</span>
${rule.error ? '<i class="fas fa-exclamation-triangle text-warning ms-2" title="Error reading file"></i>' : ''}
</div>
</td>
<td>
<span class="badge bg-success">${rule.rule_count} rule${rule.rule_count !== 1 ? 's' : ''}</span>
${rule.rule_names.length > 0 ? `
<div class="small text-muted mt-1">
${rule.rule_names.slice(0, 3).map(name => `<code>${escapeHtml(name)}</code>`).join(', ')}
${rule.rule_names.length > 3 ? `<button class="btn btn-link btn-sm p-0 ms-1 text-muted" onclick="showAllRulesPopup(${rule.id}, '${escapeHtml(rule.display_name || rule.filename)}', ${JSON.stringify(rule.rule_names).replace(/"/g, '"')})">+${rule.rule_names.length - 3} more</button>` : ''}
</div>
` : ''}
</td>
<td>${formatFileSize(rule.size)}</td>
<td>${formatDate(rule.modified)}</td>
<td>
<div class="btn-group btn-group-sm" role="group">
${!rule.error ? `
<button type="button" class="btn btn-outline-primary" onclick="viewRule(${rule.id})" title="View Rule">
<i class="fas fa-eye"></i>
</button>
` : ''}
${(window.adminFeatures && window.adminFeatures.show_delete_buttons) ? `
${!rule.error ? `
<button type="button" class="btn btn-outline-danger" onclick="deleteRule(${rule.id}, '${escapeHtml(rule.display_name)}')" title="Delete Rule">
<i class="fas fa-trash"></i>
</button>
` : `
<button type="button" class="btn btn-outline-warning" onclick="removeNonExistentRule(${rule.id}, '${escapeHtml(rule.display_name)}')" title="Remove non-existent rule from database">
<i class="fas fa-unlink"></i> Remove
</button>
`}
` : ''}
</div>
</td>
</tr>
`).join('');
}
function updateStatistics(rules) {
const totalRules = rules.length;
const totalRuleCount = rules.reduce((sum, rule) => sum + rule.rule_count, 0);
const totalSize = rules.reduce((sum, rule) => sum + rule.size, 0);
const recentUploads = rules.filter(rule => {
const modifiedDate = new Date(rule.modified);
const weekAgo = new Date();
weekAgo.setDate(weekAgo.getDate() - 7);
return modifiedDate > weekAgo;
}).length;
document.getElementById('total-rules').textContent = totalRules;
document.getElementById('total-rule-count').textContent = totalRuleCount;
document.getElementById('total-size').textContent = formatFileSize(totalSize);
document.getElementById('recent-uploads').textContent = recentUploads;
}
function refreshRules() {
loadRules();
}
function viewRule(ruleId) {
const modal = new bootstrap.Modal(document.getElementById('ruleDetailsModal'));
document.getElementById('ruleDetailsModalLabel').innerHTML = `
<i class="fas fa-code text-primary me-2"></i>
YARA Rule Details
`;
document.getElementById('rule-content').innerHTML = `
<div class="text-center py-4">
<div class="spinner-border text-primary" role="status">
<span class="visually-hidden">Loading...</span>
</div>
<p class="mt-2 text-muted">Loading rule content...</p>
</div>
`;
modal.show();
fetch(`/api/yara/rules/${ruleId}`)
.then(response => response.json())
.then(data => {
if (data.error) {
document.getElementById('rule-content').innerHTML = `
<div class="alert alert-danger">
<i class="fas fa-exclamation-triangle me-2"></i>
Error loading rule: ${escapeHtml(data.error)}
</div>
`;
return;
}
// Update modal title with display name
document.getElementById('ruleDetailsModalLabel').innerHTML = `
<i class="fas fa-code text-primary me-2"></i>
YARA Rule Details - ${escapeHtml(data.display_name)}
`;
document.getElementById('rule-content').innerHTML = `
<pre><code class="language-yara">${escapeHtml(data.content)}</code></pre>
`;
})
.catch(error => {
console.error('Error loading rule:', error);
document.getElementById('rule-content').innerHTML = `
<div class="alert alert-danger">
<i class="fas fa-exclamation-triangle me-2"></i>
Failed to load rule content
</div>
`;
});
}
function deleteRule(ruleId, displayName) {
if (!confirm(`Are you sure you want to delete "${displayName}"? This action cannot be undone.`)) {
return;
}
fetch(`/api/yara/rules/${ruleId}`, {
method: 'DELETE'
})
.then(response => {
if (response.status === 401) {
updateUIBasedOnAuth();
showLoginModal();
return Promise.reject(new Error('Authentication required'));
}
return response.json();
})
.then(data => {
if (data.error) {
showError('Failed to delete rule: ' + data.error);
return;
}
showSuccess('Rule deleted successfully');
loadRules();
})
.catch(error => {
if (error.message === 'Authentication required') {
return;
}
console.error('Error deleting rule:', error);
showError('Failed to delete rule');
});
}
function removeNonExistentRule(ruleId, displayName) {
if (!confirm(`Remove "${displayName}" from database? The file no longer exists on the filesystem.`)) {
return;
}
fetch(`/api/yara/rules/${ruleId}`, {
method: 'DELETE'
})
.then(response => {
if (response.status === 401) {
updateUIBasedOnAuth();
showLoginModal();
return Promise.reject(new Error('Authentication required'));
}
return response.json();
})
.then(data => {
if (data.error) {
showError('Failed to remove rule: ' + data.error);
return;
}
showSuccess('Rule removed from database successfully');
loadRules();
})
.catch(error => {
if (error.message === 'Authentication required') {
return;
}
console.error('Error removing rule:', error);
showError('Failed to remove rule');
});
}
function removeAllNonExistentRules() {
if (!confirm('Remove all non-existent rules from database? This will clean up all entries where the files no longer exist on the filesystem.')) {
return;
}
// Show progress indicator
const button = document.getElementById('remove-non-existent-btn');
const originalText = button.innerHTML;
button.innerHTML = '<i class="fas fa-spinner fa-spin me-2"></i>Cleaning...';
button.disabled = true;
fetch('/api/yara/cleanup-orphaned', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
.then(response => {
if (response.status === 401) {
updateUIBasedOnAuth();
showLoginModal();
return Promise.reject(new Error('Authentication required'));
}
return response.json();
})
.then(data => {
if (data.error) {
showError('Failed to clean up rules: ' + data.error);
return;
}
if (data.removed_count > 0) {
showSuccess(`Successfully removed ${data.removed_count} non-existent rule(s) from database`);
} else {
showSuccess('No non-existent rules found to remove');
}
loadRules();
})
.catch(error => {
if (error.message === 'Authentication required') {
return;
}
console.error('Error cleaning up rules:', error);
showError('Failed to clean up rules');
})
.finally(() => {
// Restore button
button.innerHTML = originalText;
button.disabled = false;
});
}
// Login form handling
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('login-form').addEventListener('submit', function(e) {
e.preventDefault();
const username = document.getElementById('admin-username').value;
const password = document.getElementById('admin-password').value;
login(username, password)
.then(data => {
bootstrap.Modal.getInstance(document.getElementById('loginModal')).hide();
showSuccess('Login successful');
// If user was trying to access rules page, redirect there
if (currentPage !== 'rules') {
updateURL('rules');
switchToPage('rules');
}
})
.catch(error => {
document.getElementById('login-error-message').textContent = error.message;
document.getElementById('login-error').style.display = 'block';
});
});
});
// Upload form handling
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('upload-form').addEventListener('submit', function(e) {
e.preventDefault();
const fileInput = document.getElementById('file-input');
const file = fileInput.files[0];
if (!file) {
showError('Please select a file to upload');
return;
}
const formData = new FormData();
formData.append('file', file);
document.getElementById('upload-progress').style.display = 'block';
document.getElementById('upload-results').style.display = 'none';
updateProgress(0);
fetch('/api/yara/rules', {
method: 'POST',
body: formData
})
.then(response => {
if (response.status === 401) {
updateUIBasedOnAuth();
showLoginModal();
bootstrap.Modal.getInstance(document.getElementById('uploadModal')).hide();
return Promise.reject(new Error('Authentication required'));
}
return response.json();
})
.then(data => {
updateProgress(100);
if (data.error) {
showUploadError(data.error);
return;
}
showUploadResults(data.results);
fileInput.value = '';
setTimeout(() => {
loadRules();
bootstrap.Modal.getInstance(document.getElementById('uploadModal')).hide();
}, 2000);
})
.catch(error => {
if (error.message === 'Authentication required') {