File size: 37,456 Bytes
b9a2f1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# =============================================================================
# COPYRIGHT NOTICE
# -----------------------------------------------------------------------------
# This source code is the intellectual property of Aditya Pandey.
# Any unauthorized reproduction, distribution, or modification of this code
# is strictly prohibited.
# If you wish to use or modify this code for your project, please ensure
# to give full credit to Aditya Pandey.
#
# PROJECT DESCRIPTION
# -----------------------------------------------------------------------------
# This code is for a chatbot crafted with powerful prompts, designed to
# utilize the Gemini API. It is tailored to assist cybersecurity researchers.
#
# Author: Aditya Pandey
# =============================================================================

import os
import streamlit as st
from PIL import Image
import textwrap
from io import BytesIO
import io
import chardet
from constants import gemini_key
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.llms import OpenAI
from langchain import PromptTemplate
from langchain.chains import LLMChain
import google.generativeai as genai
from langchain.memory import ConversationBufferMemory
from google.generativeai.types import HarmCategory, HarmBlockThreshold, HarmProbability
from google.generativeai import GenerativeModel
from langchain.chains import SequentialChain
from datetime import datetime
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import requests
from pefile import PE, PEFormatError
import re
import hashlib

# VirusTotal API details
VIRUSTOTAL_API_KEY = 'ed48e6407e0b7975be7d19c797e1217f500183c9ae84d1119af8628ba4c98c3d'


# API configuration
os.environ["GOOGLE_API_KEY"] = gemini_key
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])

# Define correct username and password
CORRECT_USERNAME = "Oxsecure"
CORRECT_PASSWORD = "Oxsecure@123"

# Streamlit framework
st.set_page_config(
    page_title="OxSecure",
    page_icon="πŸ”’",
    layout="wide"
)

# Load custom CSS
def load_css(file_name):
    with open(file_name) as f:
        st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)

# Load the CSS file
load_css("ui/Style.css")

def render_login_page():
    st.title("Oxsecure 🧠 - Your Companion! πŸ”’")
    st.markdown("---")
    st.image('ui/Ox.jpg', width=200, use_column_width='always')
    st.write("Unlock the realm of cybersecurity expertise with OxSecure 🧠 πŸš€ Safeguarding your data. πŸ”’ Let's chat about security topics and empower your knowledge! Product of CyberBULL πŸ‘οΈ")
    st.markdown("---")
    st.write("Please log in to continue.")
    st.write("πŸ’³ Default Credentials  Username = Oxsecure , Password = Oxsecure@123 ")
    st.divider()
    st.markdown("""

    **Welcome to OxSecure Intelligence** πŸ” your ultimate destination for comprehensive and up-to-date information on cybersecurity. Whether you're a professional, student, or enthusiast, this app is designed to empower you with the knowledge and tools needed to navigate the complex world of cybersecurity.



    **Features**



    **πŸ“– In-Depth Information on Cybersecurity Topics:**

    

    Explore a wide range of topics in cybersecurity with detailed articles and guides. This app covers everything from basic concepts to advanced techniques, ensuring you have access to the information you need to stay informed and secure.



    **πŸ’» Secure Coding Principles:**

    

    Learn the best practices for secure coding to protect your software from vulnerabilities. These guides provide practical tips and examples to help you write code that is both functional and secure.



    **🚨 Major Cyberattacks:**

    

    Stay updated on major cyberattacks and learn from real-world cases. Understand the methods used by attackers, the impact of these attacks, and the measures you can take to protect yourself and your organization.



    **βš™οΈ Security Misconfiguration:**

    

    Identify common security misconfigurations and learn how to fix them. These resources help you ensure that your systems are configured correctly to prevent breaches and unauthorized access.



    **πŸ”Ž VirusTotal File Analysis:**

    

    Upload your files for in-depth malware scanning using the VirusTotal API. Instantly analyze your files and receive reports with threat intelligence on potential malware, ensuring your files are clean and secure.



    **πŸ” Comprehensive File Analysis:**

    

    Use this app to scan a variety of file types like PDFs, images, executables, and logs. From extracting metadata to analyzing file content, OxSecure Intelligence ensures thorough and real-time security analysis.



    **πŸ€– Powered by Gemini LLM:**

    

    This app leverages the powerful Gemini LLM to provide you with accurate and relevant information. Gemini LLM enhances the content with cutting-edge insights and helps you get the answers you need quickly and efficiently.



    **πŸ–ΌοΈ Image Analysis with Imagen:**

    

    Utilize the Imagen feature to extract detailed information from images. Simply upload an image, and our app will analyze it and provide responses tailored to your queries. Perfect for identifying vulnerabilities, assessing security measures, and more.



    **Why Choose OxSecure Intelligence?**



    - **🌐 Comprehensive Coverage:** From basic concepts to advanced practices, this app covers all aspects of cybersecurity.

    - **πŸ“š Expert Guidance:** Learn from detailed articles and guides written by cybersecurity experts.

    - **⚑ Advanced Tools:** Use powerful AI tools like Gemini LLM, Imagen, and VirusTotal to enhance your learning and problem-solving capabilities.

    - **πŸ”„ Stay Updated:** Keep up with the latest trends, threats, and best practices in the cybersecurity field.



    Join OxSecure Intelligence today and take your cybersecurity knowledge to the next level! πŸš€

    """)
    st.markdown("---")
    linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
    st.markdown("  Created with πŸ€—πŸ’– By Aditya Pandey  " f"[  LinkedIn πŸ”—]({linkedin_url})")

    username = st.sidebar.text_input("Username πŸ‘€")
    password = st.sidebar.text_input("Password πŸ”‘", type="password")
    login_button = st.sidebar.button("Login 🫒")

    if login_button:
        if username == CORRECT_USERNAME and password == CORRECT_PASSWORD:
            st.session_state.authenticated = True
            st.success("Login successful!")
            st.experimental_rerun()
            render_main_program()
        else:
            st.error("Invalid username or password. Please try again.")

def features():
    st.write("***πŸ”‘ Key Features of OxSecure Intelligence***")
    
    with st.expander("πŸ“– In-Depth Information on Cybersecurity Topics"):
        st.write("""

            **Expand Your Cybersecurity Knowledge**  

            Stay informed with detailed articles and guides covering a wide range of cybersecurity topics. Whether you're 

            learning basic concepts or exploring advanced techniques, this resource ensures you're well-equipped to handle 

            the latest cybersecurity challenges.

        """)
        
    with st.expander("πŸ’» Secure Coding Principles"):
        st.write("""

            **Write Code that Stands the Test of Time**  

            Learn essential best practices for writing secure, reliable code. Our secure coding guides offer practical tips 

            and real-world examples to help you minimize vulnerabilities in your software.

        """)
        
    with st.expander("🚨 Major Cyberattacks"):
        st.write("""

            **Stay Informed on Critical Threats**  

            Keep up-to-date on the most significant cyberattacks around the world. Analyze real-world incidents, learn the 

            attack vectors used, and discover defensive strategies to protect against similar threats.

        """)
        
    with st.expander("βš™οΈ Security Misconfiguration"):
        st.write("""

            **Configure with Confidence**  

            Learn how to avoid common misconfigurations that leave systems exposed. This section provides a comprehensive 

            guide to correctly configuring security settings, protecting your organization from unnecessary risks.

        """)
        
    with st.expander("πŸ”Ž VirusTotal File Analysis"):
        st.write("""

            **Instant Malware Scanning**  

            Upload your files to run advanced malware scans via VirusTotal API. Get real-time reports with detailed threat 

            intelligence and analysis, ensuring your files are secure before you use or share them.

        """)
        
    with st.expander("πŸ” Comprehensive File Analysis"):
        st.write("""

            **Analyze Multiple File Types**  

            OxSecure Intelligence allows you to scan PDFs, images, executables, and logs with ease. From extracting metadata 

            to conducting thorough file content analysis, you'll have all the tools you need to secure your files.

        """)
        
    with st.expander("πŸ€– Powered by Gemini LLM"):
        st.write("""

            **AI-Powered Insights**  

            Harness the cutting-edge power of Gemini LLM to get instant, accurate answers to your cybersecurity queries. 

            With AI-driven insights, you can navigate complex data and extract valuable knowledge faster than ever before.

        """)
        
    with st.expander("πŸ–ΌοΈ Image Analysis with Imagen"):
        st.write("""

            **Visual Intelligence at Your Fingertips**  

            Upload images for detailed analysis using the Imagen feature. Whether you're assessing a security measure or 

            scanning for vulnerabilities, this tool ensures you get the most out of every image.

        """)

def use_app():
    st.write("***πŸ“‹ How to Use OxSecure Intelligence***")
    
    st.write("""

     πŸš€ **OxSecure Intelligence: Use Cases**



OxSecure Intelligence is a comprehensive cybersecurity tool designed to provide in-depth information on various security topics, analyze images, and perform detailed file analysis. The app consists of three powerful tools:



πŸ›‘οΈ **1. OxSecure Chat**



 ***Use Case:***

**OxSecure Chat** allows users to gain a deep understanding of cybersecurity topics by generating detailed outputs based on the entered topics. This tool is ideal for:



- **Learning and Research:** Enter any cybersecurity topic to receive a thorough explanation, including secure coding principles and major attack vectors.

- **Training and Development:** Use the detailed outputs to educate teams or individuals about specific security concepts and practices.

- **Consultation and Advisory:** Provide clients or stakeholders with well-researched and comprehensive information on cybersecurity issues.



**How It Works:**

1. Enter a security topic related to cybersecurity.

2. Receive a detailed response including:

   - **Secure Coding Principles:** Best practices and guidelines.

   - **Major Attacks:** Common threats and attack methods.



πŸ–ΌοΈ **2. OxSecure ImaGen**



 ***Use Case:***

**OxSecure ImaGen** offers advanced image analysis by allowing users to input prompts and retrieve detailed information about the image. This tool is perfect for:



- **Image Verification:** Analyze images to extract metadata and ensure they are authentic and unaltered.

- **Content Analysis:** Understand the content and context of images through custom prompts.

- **Forensic Analysis:** Utilize the tool in digital forensics to scrutinize image details for investigative purposes.



 **How It Works:**

1. Upload an image.

2. Enter prompts to specify the desired output.

3. Receive detailed information and insights based on the image content.



 πŸ“‚ **3. File Analysis**



***Use Case:***

**File Analysis** is designed for thorough examination of files, providing essential metadata, hash information, and integrating with VirusTotal for comprehensive security analysis. This tool is valuable for:



- **File Verification:** Extract metadata and hash information to verify file integrity and authenticity.

- **Threat Detection:** Communicate with VirusTotal API to assess the file’s security status and identify potential threats.

- **Visual Analytics:** Obtain graphical representations of file analysis results to visualize threat levels and security metrics.



**How It Works:**

1. Upload a file.

2. Extract metadata and hash information.

3. Integrate with VirusTotal API for detailed security analysis.

4. View graphical reports and insights about the file.



---



**OxSecure Intelligence** empowers you with detailed insights and robust analysis tools to enhance your cybersecurity practices and ensure data integrity. Explore these tools to stay ahead of potential threats and make informed decisions!

""")

## Function to load Gemini vision model and get response
def get_gemini_response(input_prompt, image):
    Model = genai.GenerativeModel('gemini-1.5-pro')
    if input_prompt != "":
        response = Model.generate_content([input_prompt, image])
    else:
        response = Model.generate_content(image)
    return response.text


def render_main_program():
    st.markdown("# πŸ”’ Unlock the Future of Cybersecurity with OxSecure")
    st.divider()
    st.markdown("**Where Knowledge Meets Innovation! πŸš€ Dive into Cyber Brilliance with OxSecure** πŸ€– 🌟")
    st.markdown("----")

    # Sidebar for navigation
    app_choice = st.sidebar.radio("Choose App", 
         ("Features πŸ€ΉπŸ»β€β™€οΈ", 
          "OxSecure Chat πŸ€–", 
          "OxSecure ImaGen 🎨", 
          "File Analysis πŸ“", 
          "Help & Uses πŸ’πŸ»"))
    
    #Main content selector
    # app_choice = st.selectbox(
    #     "Choose App",
    #     ["Features πŸ€ΉπŸ»β€β™€οΈ", "OxSecure Chat πŸ€–", "OxSecure ImaGen 🎨", "File Analysis πŸ“", "Help & Uses πŸ’πŸ»"]
    # )

    # Render the selected app based on user's choice
    if app_choice == "OxSecure Chat πŸ€–":
        render_gemini_api_app()
    elif app_choice == "OxSecure ImaGen 🎨":
        render_gemini_vision_app()
    elif app_choice == "File Analysis πŸ“":
        render_file_analysis_app()
    elif app_choice == "Features πŸ€ΉπŸ»β€β™€οΈ":
        features()
    elif app_choice == "Help & Uses πŸ’πŸ»":
        use_app()

def render_gemini_api_app():
    st.caption("πŸš€ Empower Tomorrow, πŸ›‘οΈ Secure Today: Unleash the Power of Cybersecurity Brilliance! πŸ’»βœ¨ πŸ›‘οΈπŸ’¬  ")
    st.markdown("---")

    st.title("OxSecure Intelligence 🧠")
    st.markdown("-----")
    input_text = st.text_input("Search your Security Related Topic πŸ”")

    # Prompt Templates
    first_input_prompt = PromptTemplate(
        input_variables=['Topic'],
        template=textwrap.dedent("""

            As an experienced cybersecurity researcher, provide a comprehensive and detailed explanation about {Topic}. Cover the following aspects:

            1. Introduction and Importance in well informative

            2. Key Concepts and Terminologies

            3. Historical Background and Evolution

            4. Its Architecture and Types

            5. Current Trends and Best Practices

            6. Major Threats and Vulnerabilities

            7. Case Studies and Real-world Examples

            8. Future Outlook and Predictions

            

            Ensure the information is professional, well-structured, key conceptual  and suitable for someone with an advanced understanding and Beginner of cybersecurity.

        """)
    )

    # Select the model
    model = genai.GenerativeModel('gemini-1.5-pro')
    safety_settings = {
        HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_DANGEROUS: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_SEXUAL: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_TOXICITY: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmProbability.HIGH
    }

    # Memory
    Topic_memory = ConversationBufferMemory(input_key='Topic', memory_key='chat_history')
    Policy_memory = ConversationBufferMemory(input_key='secure coding', memory_key='chat_history')
    Practice_memory = ConversationBufferMemory(input_key='Practice', memory_key='description_history')

    ## GEMINI LLMS
    llm = ChatGoogleGenerativeAI(model="gemini-1.5-pro")
    chain = LLMChain(
        llm=llm, prompt=first_input_prompt, verbose=True, output_key='secure coding', memory=Topic_memory)
    safety_settings = {
        HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_DANGEROUS: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_SEXUAL: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_TOXICITY: HarmBlockThreshold.BLOCK_NONE,
        HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmProbability.HIGH
    }
    # Prompt Templates
    second_input_prompt = PromptTemplate(
        input_variables=['secure coding'],
        template="write best {secure coding} and perfect code snippet for implementing secure coding to this {Topic} in well detailed and descriptive way use code snippets for each point and describe code."
    )

    chain2 = LLMChain(
        llm=llm, prompt=second_input_prompt, verbose=True, output_key='Practice', memory=Policy_memory)
    # Prompt Templates
    third_input_prompt = PromptTemplate(
        input_variables=['Practice'],
        template="Implement major best Cybersecurity {Practice} for this {Topic} that helps better security postures into any business. illustrate Major cyberattack which is done by misconfiguration of {Topic} and give the informative info about the malware which caused this"
    )
    chain3 = LLMChain(llm=llm, prompt=third_input_prompt, verbose=True, output_key='description', memory=Practice_memory)
    parent_chain = SequentialChain(
        chains=[chain, chain2, chain3], input_variables=['Topic'], output_variables=['secure coding', 'Practice',
                                                                                     'description'], verbose=True)

    if input_text:
        with st.spinner('Processing.... ⏳'):
            st.text(parent_chain({'Topic': input_text}))

        with st.expander('Your Topic'):
            st.info(Topic_memory.buffer)

        with st.expander('Major Practices'):
            st.info(Practice_memory.buffer)
    st.markdown("---")
    linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
    st.markdown("  Created with πŸ€—πŸ’– By Aditya Pandey   "  f"[  LinkedIn πŸ”—]({linkedin_url})")


def render_gemini_vision_app():
    st.title('OxSecure ImaGen 🎨')
    st.markdown("----")
    input_prompt = st.text_input("Input Prompt: ", key="input")
    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
    image = ""
    submit = False  # Initialize submit variable

    if uploaded_file is not None:
        image = Image.open(uploaded_file)
        st.image(image, caption="Uploaded Image.", use_column_width=True)
        submit = st.button("Tell me about the image")

    if submit:
        response = get_gemini_response(input_prompt, image)
        st.subheader("The Response is")
        st.write(response)

    st.markdown("---")
    linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
    st.markdown("  Created with πŸ€—πŸ’– By Aditya Pandey  " f"[  LinkedIn πŸ”—]({linkedin_url})")


def get_file_hash(file):
    file.seek(0)  # Reset file pointer to the beginning
    file_hash = hashlib.sha256(file.read()).hexdigest()
    file.seek(0)  # Reset file pointer to the beginning
    return file_hash

# Function to analyze the file using VirusTotal
def virustotal_analysis(file_hash):
    url = f"https://www.virustotal.com/api/v3/files/{file_hash}"
    headers = {"x-apikey": VIRUSTOTAL_API_KEY}
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        st.error("Error with VirusTotal API request. Please check your API key or the file hash.")
        return None

# Function to extract metadata from PE files
def extract_metadata(file):
    try:
        pe = PE(data=file.read())
        metadata = {
            "Number of Sections": pe.FILE_HEADER.NumberOfSections,
            "Time Date Stamp": pe.FILE_HEADER.TimeDateStamp,
            "Characteristics": pe.FILE_HEADER.Characteristics,
        }
        return metadata
    except PEFormatError:
        st.error("Uploaded file is not a valid PE format.")
        return None


def analyze_log_file(log_content):
    # Data storage structures for IPs, Domains, Headers, Sessions
    ip_data = []
    domain_data = []
    header_data = []
    id_data = []

    # Regular expressions for matching
    ip_regex = re.compile(r'\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b')
    domain_regex = re.compile(r'\b[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b')
    header_regex = re.compile(r'(User-Agent|Content-Type|Authorization):\s*(.*)', re.IGNORECASE)
    id_regex = re.compile(r'\b(?:SessionID|UserID|ID|id|sessionid|userid)\s*[:=\s]\s*([a-zA-Z0-9-]+)', re.IGNORECASE)

    log_entries = []

    for line in log_content.splitlines():
        # Match IPs
        ips = ip_regex.findall(line)
        if ips:
            ip_data.extend(ips)

        # Match Domains
        domains = domain_regex.findall(line)
        if domains:
            domain_data.extend(domains)

        # Match Headers
        headers = header_regex.findall(line)
        if headers:
            header_data.extend(headers)

        # Match IDs (Session IDs, User IDs, etc.)
        ids = id_regex.findall(line)
        if ids:
            id_data.extend(ids)

        log_entries.append(line)

    # Convert to DataFrame
    log_df = pd.DataFrame(log_entries, columns=["Log Entries"])

    # Additional DataFrames for captured data
    ip_df = pd.DataFrame(ip_data, columns=["IP Addresses"])
    domain_df = pd.DataFrame(domain_data, columns=["Domains"])
    header_df = pd.DataFrame(header_data, columns=["Header Name", "Header Value"])
    id_df = pd.DataFrame(id_data, columns=["IDs"])

    # Summary of findings
    summary = {
        "log_dataframe": log_df,
        "ip_dataframe": ip_df,
        "domain_dataframe": domain_df,
        "header_dataframe": header_df,
        "id_dataframe": id_df
    }

    return summary

# Function to create charts from VirusTotal results with theme selection
def create_virus_total_charts(virus_total_results, theme="light"):
    if not virus_total_results:
        return None
    
    # Extract the data for the charts
    stats = virus_total_results['data']['attributes']['last_analysis_stats']
    labels = list(stats.keys())
    values = list(stats.values())
    
    # Convert data to DataFrame for better handling
    df = pd.DataFrame({'Analysis Types': labels, 'Count': values})
    
    # Set the background color theme based on user input
    if theme == "dark":
        plt.style.use("dark_background")
        text_color = 'white'
    else:
        plt.style.use("default")
        text_color = 'black'
    
    # Create a container (figure) with 3 rows and 2 columns of charts
    fig, axs = plt.subplots(3, 2, figsize=(18, 18))  # 3 rows and 2 columns of charts
    
    # --- Bar Chart ---
    sns.barplot(x='Analysis Types', y='Count', data=df, palette="coolwarm", ax=axs[0, 0])
    axs[0, 0].set_title("VirusTotal Analysis Results (Bar Chart)", fontsize=14, fontweight='bold', color=text_color)
    axs[0, 0].tick_params(axis='x', rotation=45, labelsize=10, labelcolor=text_color)  # Rotate x-axis labels
    
    # Add value labels on the bar chart
    for p in axs[0, 0].patches:
        axs[0, 0].annotate(f'{int(p.get_height())}', (p.get_x() + p.get_width() / 2., p.get_height()), 
                           ha='center', va='baseline', fontsize=10, color=text_color, xytext=(0, 3), 
                           textcoords='offset points')
    
    # --- Horizontal Bar Chart ---
    sns.barplot(y='Analysis Types', x='Count', data=df, palette="magma", ax=axs[0, 1], orient='h')
    axs[0, 1].set_title("VirusTotal Analysis Results (Horizontal Bar)", fontsize=14, fontweight='bold', color=text_color)
    axs[0, 1].tick_params(axis='y', labelsize=10, labelcolor=text_color)
    
    # Add value labels on horizontal bar chart
    for p in axs[0, 1].patches:
        axs[0, 1].annotate(f'{int(p.get_width())}', (p.get_width(), p.get_y() + p.get_height() / 2), 
                           ha='center', va='center_baseline', fontsize=10, color=text_color, xytext=(5, 0),
                           textcoords='offset points')
    
    # --- Pie Chart ---
    wedges, texts, autotexts = axs[1, 0].pie(values, labels=labels, autopct='%1.1f%%', startangle=90,
                                             colors=sns.color_palette("coolwarm", len(labels)),
                                             wedgeprops=dict(edgecolor=text_color))
    
    # Format the text and labels
    for text in texts:
        text.set_fontsize(10)
        text.set_color(text_color)
    
    for autotext in autotexts:
        autotext.set_color(text_color)
    
    axs[1, 0].set_title("VirusTotal Analysis Results (Pie Chart)", fontsize=14, fontweight='bold', color=text_color)
    axs[1, 0].axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.

    # --- Donut Chart ---
    wedges, texts, autotexts = axs[1, 1].pie(values, labels=labels, autopct='%1.1f%%', startangle=90,
                                             pctdistance=0.85, colors=sns.color_palette("Set2", len(labels)),
                                             wedgeprops=dict(width=0.4, edgecolor=text_color))  # Donut chart
    
    # Format the text and labels for Donut Chart
    for text in texts:
        text.set_fontsize(10)
        text.set_color(text_color)
    
    for autotext in autotexts:
        autotext.set_color(text_color)
    
    axs[1, 1].set_title("VirusTotal Analysis Results (Donut Chart)", fontsize=14, fontweight='bold', color=text_color)
    axs[1, 1].axis('equal')  # Equal aspect ratio for donut shape
    
    # --- Heatmap (Random Example) ---
    random_data = np.random.rand(len(labels), len(labels))  # Create a dummy heatmap based on the stats
    sns.heatmap(random_data, annot=True, cmap="Blues", ax=axs[2, 0], cbar_kws={'label': 'Intensity'})
    axs[2, 0].set_title("Random Heatmap (Dummy)", fontsize=14, fontweight='bold', color=text_color)
    axs[2, 0].set_xticklabels(labels, rotation=45, color=text_color)
    axs[2, 0].set_yticklabels(labels, rotation=0, color=text_color)

    # --- Scatter Plot ---
    sns.scatterplot(x=labels, y=values, hue=values, palette="deep", s=100, ax=axs[2, 1], legend=False)
    axs[2, 1].set_title("VirusTotal Analysis Results (Scatter Plot)", fontsize=14, fontweight='bold', color=text_color)
    axs[2, 1].set_xlabel("Analysis Types", fontsize=12, color=text_color)
    axs[2, 1].set_ylabel("Count", fontsize=12, color=text_color)
    axs[2, 1].tick_params(axis='x', rotation=45, labelcolor=text_color)
    axs[2, 1].tick_params(axis='y', labelcolor=text_color)
    
    # Adjust layout for better spacing and clarity
    fig.tight_layout(pad=4.0)
    
    # Set background based on theme
    fig.patch.set_facecolor('black' if theme == "dark" else 'white')
    
    return fig

# Function to create detailed tables from JSON data
def create_detailed_table(data, title):
    st.write(f"{title}")
    
    # Normalize JSON data into a DataFrame
    df = pd.json_normalize(data)
    
    # Debug: Show raw data and DataFrame
    st.write("Raw Data:", data)

    if df.empty:
        st.write("No data available.")
    else:
        # Apply minimal styling for debugging
        styled_df = df.style.background_gradient(cmap='viridis') \
                          .format(na_rep='N/A', precision=2)
        
        # Display the styled DataFrame
        st.dataframe(styled_df)

# Function to display the analysis results on the dashboard
def display_analysis_results(metadata, virus_total_results, log_analysis=None):
    st.write("## Analysis Results")

    # Metadata
    if metadata:
        st.write("### πŸ“‚ PE File Metadata")
        create_detailed_table(metadata, "PE File Metadata")

    # VirusTotal Results
    if virus_total_results:
        st.write("### 🦠 VirusTotal Results")
        create_detailed_table(virus_total_results['data'], "VirusTotal Results")
        st.write("#### πŸ“Š VirusTotal Analysis Stats")
        st.markdown("------")
        fig = create_virus_total_charts(virus_total_results)
        if fig:
            st.pyplot(fig)

    # Log Analysis
    if log_analysis is not None:
        st.write("### πŸ“ Log Analysis")
        st.markdown("------")
        col1, col2 = st.columns(2)
        
        with col1:
            st.write("**IP Addresses:**")
            st.dataframe(log_analysis.get("ip_dataframe"))
            
        with col2:
            st.write("**Domains:**")
            st.dataframe(log_analysis.get("domain_dataframe"))
            
        col3, col4, col5 = st.columns([2, 1, 1])
        st.markdown("----------")
        
        with col3:
            st.write("**Log Entries:**")
            st.dataframe(log_analysis.get("log_dataframe"))
            
        with col4:
            st.write("**IDs (Session/User/Generic):**")
            if not log_analysis.get("id_dataframe").empty:
                st.dataframe(log_analysis.get("id_dataframe"))
            else:
                st.write("No IDs found.")
                
                
        with col5:
            st.write("**Headers:**")
            if not log_analysis.get("header_dataframe").empty:
                st.dataframe(log_analysis.get("header_dataframe"))
            else:
                st.write("No headers found.")

def read_file_with_fallback(byte_data):
    try:
        # Attempt to read the file with UTF-8 encoding
        return byte_data.decode("utf-8")
    except UnicodeDecodeError:
        # If UTF-8 decoding fails, try to detect encoding
        byte_stream = io.BytesIO(byte_data)
        detected_encoding = chardet.detect(byte_data)['encoding']
        byte_stream.seek(0)  # Reset stream pointer
        return byte_stream.read().decode(detected_encoding, errors='replace')


def render_file_analysis_app():
    st.title("πŸ” File Analysis Dashboard")
    st.markdown("---")
    st.image('ui/antivirus.png', width=80, use_column_width='none')

    uploaded_file = st.file_uploader("Upload any file for analysis", type=["exe", "dll", "log", "pdf", "png", "jpg", "jpeg", "gif", "txt", "zip", "rar", "apk"])

    if uploaded_file:
        file_hash = get_file_hash(uploaded_file)
        st.write(f"SHA-256 Hash: {file_hash}")

        file_extension = uploaded_file.name.split('.')[-1].lower()

        # Handle different file types
        if file_extension in ['png', 'jpg', 'jpeg', 'gif']:
            st.write("### πŸ“„ Image Preview")
            image = Image.open(uploaded_file)
            image.thumbnail((512, 512))  # Resize for preview
            st.image(image, width=240, caption='Uploaded Image', use_column_width=True)
            # Save uploaded file temporarily
            file_path = f"./temp/{uploaded_file.name}"
            os.makedirs(os.path.dirname(file_path), exist_ok=True)
            with open(file_path, "wb") as f:
                f.write(uploaded_file.getbuffer())

            try:
                with open(file_path, "rb") as file:
                    file_hash = get_file_hash(file)
                    metadata = extract_metadata(file)
                    virus_total_results = virustotal_analysis(file_hash)

            finally:
                # Clean up
                os.remove(file_path)
            
            log_analysis = None


        elif file_extension == 'pdf':
            st.write("### πŸ“„ PDF File")
            st.write("PDF preview is not supported. Please use other tools to view.")
            st.download_button(label="Download PDF", data=uploaded_file, file_name=uploaded_file.name)
            # Save uploaded file temporarily
            file_path = f"./temp/{uploaded_file.name}"
            os.makedirs(os.path.dirname(file_path), exist_ok=True)
            with open(file_path, "wb") as f:
                f.write(uploaded_file.getbuffer())

            try:
                with open(file_path, "rb") as file:
                    file_hash = get_file_hash(file)
                    metadata = extract_metadata(file)
                    virus_total_results = virustotal_analysis(file_hash)

            finally:
                # Clean up
                os.remove(file_path)
            
            log_analysis = None

        elif file_extension in ['txt', 'log']:
            st.write("### πŸ“ Log File Content")
            log_content = read_file_with_fallback(uploaded_file.getvalue())
            log_analysis = analyze_log_file(log_content)
            # Save uploaded file temporarily
            file_path = f"./temp/{uploaded_file.name}"
            os.makedirs(os.path.dirname(file_path), exist_ok=True)
            with open(file_path, "wb") as f:
                f.write(uploaded_file.getbuffer())

            try:
                with open(file_path, "rb") as file:
                    file_hash = get_file_hash(file)
                    metadata = extract_metadata(file)
                    virus_total_results = virustotal_analysis(file_hash)

            finally:
                # Clean up
                os.remove(file_path)
            
            log_analysis = analyze_log_file(log_content)

        elif file_extension in ['zip', 'rar']:
            st.write("### πŸ“¦ Compressed File")
            st.write("Compressed files require further extraction and analysis.")
            # Save uploaded file temporarily
            file_path = f"./temp/{uploaded_file.name}"
            os.makedirs(os.path.dirname(file_path), exist_ok=True)
            with open(file_path, "wb") as f:
                f.write(uploaded_file.getbuffer())

            try:
                with open(file_path, "rb") as file:
                    file_hash = get_file_hash(file)
                    metadata = extract_metadata(file)
                    virus_total_results = virustotal_analysis(file_hash)

            finally:
                # Clean up
                os.remove(file_path)
            
            log_analysis = None

        elif file_extension in ['apk', 'exe', 'dll']:
            # Save uploaded file temporarily
            file_path = f"./temp/{uploaded_file.name}"
            os.makedirs(os.path.dirname(file_path), exist_ok=True)
            with open(file_path, "wb") as f:
                f.write(uploaded_file.getbuffer())

            try:
                with open(file_path, "rb") as file:
                    file_hash = get_file_hash(file)
                    metadata = extract_metadata(file)
                    virus_total_results = virustotal_analysis(file_hash)

            finally:
                # Clean up
                os.remove(file_path)
            
            log_analysis = None

        else:
            st.error("Unsupported file type.")
            metadata = None
            virus_total_results = None
            log_analysis = None

        display_analysis_results(metadata, virus_total_results, log_analysis)
                

    st.markdown("---")
    linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
    st.markdown("  Created with πŸ€—πŸ’– By Aditya Pandey  " f"[  LinkedIn πŸ”—]({linkedin_url})")


def main():
    if 'authenticated' not in st.session_state:
        st.session_state.authenticated = False

    if st.session_state.authenticated:
        render_main_program()
    else:
        render_login_page()

if __name__ == "__main__":
    main()