Dc0dE commited on
Commit
b9a2f1d
Β·
verified Β·
1 Parent(s): 7172149

Upload 9 files

Browse files
Files changed (9) hide show
  1. AdRAG.py +453 -0
  2. ImaGen.py +73 -0
  3. app.py +276 -0
  4. constants.py +2 -0
  5. mod2.py +143 -0
  6. packages.txt +1 -0
  7. requirements.txt +95 -0
  8. test.py +842 -0
  9. vt.py +317 -0
AdRAG.py ADDED
@@ -0,0 +1,453 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =============================================================================
2
+ # COPYRIGHT NOTICE
3
+ # -----------------------------------------------------------------------------
4
+ # This source code is the intellectual property of Aditya Pandey.
5
+ # Any unauthorized reproduction, distribution, or modification of this code
6
+ # is strictly prohibited.
7
+ # If you wish to use or modify this code for your project, please ensure
8
+ # to give full credit to Aditya Pandey.
9
+ #
10
+ # PROJECT DESCRIPTION
11
+ # -----------------------------------------------------------------------------
12
+ # This code is for a chatbot crafted with powerful prompts, designed to
13
+ # utilize the Gemini API. It is tailored to assist cybersecurity researchers.
14
+ #
15
+ # Author: Aditya Pandey
16
+ # =============================================================================
17
+
18
+ # Import library
19
+ import os
20
+ import faiss
21
+ import numpy as np
22
+ import pandas as pd
23
+ import requests
24
+ from PIL import Image
25
+ from PyPDF2 import PdfReader
26
+ import streamlit as st
27
+ from gtts import gTTS
28
+ from io import BytesIO
29
+ import google.generativeai as genai
30
+ from constants import gemini_key
31
+ from bs4 import BeautifulSoup
32
+ import urllib.request
33
+ import re
34
+ import json
35
+ from google.api_core.exceptions import GoogleAPIError
36
+ import speech_recognition as sr
37
+ from collections import defaultdict
38
+
39
+ # Streamlit configuration
40
+ st.set_page_config(
41
+ page_title="OxSecure RAG",
42
+ page_icon="🀿",
43
+ layout="wide"
44
+ )
45
+
46
+ def load_css(file_name):
47
+ with open(file_name) as f:
48
+ st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
49
+
50
+ # Load the CSS file
51
+ load_css("ui/Style.css")
52
+
53
+ # API configuration
54
+ os.environ["GOOGLE_API_KEY"] = gemini_key
55
+ genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
56
+
57
+ # Function to query Gemini model
58
+ def query_gemini(context, prompt, image=None):
59
+ try:
60
+ if image:
61
+ model = genai.GenerativeModel('gemini-1.5-pro-latest')
62
+ response = model.generate_content([context + prompt, image])
63
+ else:
64
+ model = genai.GenerativeModel('gemini-1.5-pro-latest')
65
+ response = model.generate_content(context + prompt)
66
+
67
+ if hasattr(response, 'candidates') and response.candidates:
68
+ return ' '.join(part.text for part in response.candidates[0].content.parts)
69
+ else:
70
+ st.error("Unexpected response format from Gemini API.")
71
+ return None
72
+ except GoogleAPIError as e:
73
+ st.error(f"An error occurred while querying the Gemini API: {e}")
74
+ return None
75
+
76
+ # Function to extract text from PDF
77
+ def extract_text_from_pdf(file):
78
+ try:
79
+ pdf_reader = PdfReader(file)
80
+ text = ""
81
+ for page in pdf_reader.pages:
82
+ text += page.extract_text()
83
+ return text
84
+ except Exception as e:
85
+ st.error(f"An error occurred while extracting text from PDF: {e}")
86
+ return ""
87
+
88
+ # Function to extract text from URL
89
+ def extract_text_from_url(url):
90
+ try:
91
+ headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
92
+ request = urllib.request.Request(url, headers=headers)
93
+ response = urllib.request.urlopen(request)
94
+ html = response.read()
95
+ soup = BeautifulSoup(html, 'html.parser')
96
+ paragraphs = soup.find_all('p')
97
+ text = ' '.join([para.get_text() for para in paragraphs])
98
+ return text
99
+ except Exception as e:
100
+ st.error(f"An error occurred while extracting text from URL: {e}")
101
+ return ""
102
+
103
+ # Function to extract text from CSV
104
+ def extract_text_from_csv(file):
105
+ try:
106
+ df = pd.read_csv(file)
107
+ return df.to_string(index=False)
108
+ except Exception as e:
109
+ st.error(f"An error occurred while extracting text from CSV: {e}")
110
+ return ""
111
+
112
+ # Function to extract text from Excel
113
+ def extract_text_from_excel(file):
114
+ try:
115
+ df = pd.read_excel(file)
116
+ return df.to_string(index=False)
117
+ except Exception as e:
118
+ st.error(f"An error occurred while extracting text from Excel: {e}")
119
+ return ""
120
+
121
+ # Function to extract text from JSON
122
+ def extract_text_from_json(file):
123
+ try:
124
+ json_data = json.load(file)
125
+ formatted_text = json.dumps(json_data, indent=4)
126
+ return formatted_text
127
+ except Exception as e:
128
+ st.error(f"An error occurred while extracting text from JSON: {e}")
129
+ return ""
130
+
131
+ # Remove special characters and improve formatting
132
+ def clean_text(text):
133
+ # Retain only alphabetic characters, numbers, punctuation, and spaces
134
+ clean_text = re.sub(r'[^a-zA-Z0-9.,!?;:()\'\" \n]', '', text)
135
+ return re.sub(r'\s+', ' ', clean_text).strip()
136
+
137
+ # Placeholder function to create embeddings
138
+ def embed_text(text):
139
+ # This should be replaced with the actual embedding generation logic
140
+ # For demonstration, return a dummy vector
141
+ return np.random.rand(512).astype('float32')
142
+
143
+ # Function to create embeddings and store in FAISS
144
+ def store_embeddings(text):
145
+ chunks = [text[i:i+512] for i in range(0, len(text), 512)]
146
+ vectors = [embed_text(chunk) for chunk in chunks]
147
+ dimension = vectors[0].shape[0]
148
+ index = faiss.IndexFlatL2(dimension)
149
+ index.add(np.array(vectors))
150
+ return index, chunks
151
+
152
+ # Function to search embeddings and retrieve relevant text
153
+ def search_embeddings(index, query, top_k):
154
+ query_vector = embed_text(query) # Replace with actual embedding generation
155
+ D, I = index.search(np.array([query_vector]), k=top_k)
156
+ return I[0]
157
+
158
+ # Function to handle Q&A
159
+ def handle_qa(query, faiss_index, document_chunks, top_k):
160
+ if faiss_index:
161
+ retrieved_indices = search_embeddings(faiss_index, query, top_k)
162
+ context = " ".join([document_chunks[i] for i in retrieved_indices])
163
+ response = query_gemini(context, query)
164
+ else:
165
+ response = query_gemini(st.session_state.context, query)
166
+ return response
167
+
168
+ # Function for speech recognition
169
+ def recognize_speech():
170
+ r = sr.Recognizer()
171
+ try:
172
+ with sr.Microphone() as source:
173
+ st.info("Listening...")
174
+ audio = r.listen(source)
175
+ text = r.recognize_google(audio)
176
+ st.success(f"You said: {text}")
177
+ return text
178
+ except sr.UnknownValueError:
179
+ st.error("Could not understand audio")
180
+ return None
181
+ except sr.RequestError as e:
182
+ st.error(f"Could not request results from Google Speech Recognition service; {e}")
183
+ return None
184
+ except Exception as e:
185
+ st.error(f"An error occurred: {e}")
186
+ return None
187
+
188
+ # Function to analyze log file
189
+ def analyze_log_file(file):
190
+ log_summary = {
191
+ 'total_lines': 0,
192
+ 'error_count': 0,
193
+ 'warning_count': 0,
194
+ 'info_count': 0,
195
+ 'error_details': defaultdict(int),
196
+ 'warning_details': defaultdict(int),
197
+ 'info_details': defaultdict(int),
198
+ }
199
+
200
+ error_pattern = re.compile(r'\bERROR\b')
201
+ warning_pattern = re.compile(r'\bWARNING\b')
202
+ info_pattern = re.compile(r'\bINFO\b')
203
+
204
+ with open(file, 'r') as file:
205
+ for line in file:
206
+ log_summary['total_lines'] += 1
207
+
208
+ if error_pattern.search(line):
209
+ log_summary['error_count'] += 1
210
+ log_summary['error_details'][line.strip()] += 1
211
+ elif warning_pattern.search(line):
212
+ log_summary['warning_count'] += 1
213
+ log_summary['warning_details'][line.strip()] += 1
214
+ elif info_pattern.search(line):
215
+ log_summary['info_count'] += 1
216
+ log_summary['info_details'][line.strip()] += 1
217
+
218
+ return log_summary
219
+
220
+ # Main App Function
221
+ def render_main_app():
222
+ st.title('OxSecure RAG ♨️')
223
+ st.divider()
224
+ st.markdown('**By :- Aditya Pandey πŸ§‘πŸ»β€πŸ’»**')
225
+
226
+ input_prompt = st.text_input("Input Prompt: ", key="input")
227
+
228
+ uploaded_file = st.file_uploader("Choose a file (image, PDF, CSV, Excel, JSON, or LOG)...", type=["jpg", "jpeg", "png", "pdf", "csv", "xlsx", "json", "log"])
229
+ uploaded_url = st.text_input("Or enter an article URL:")
230
+
231
+ image = None
232
+ file_text = ""
233
+
234
+ if uploaded_file is not None:
235
+ if uploaded_file.type in ["image/jpeg", "image/png", "image/jpg", "image/webp"]:
236
+ image = Image.open(uploaded_file)
237
+ st.image(image, caption="Uploaded Image.", use_column_width=True)
238
+ elif uploaded_file.type == "application/pdf":
239
+ file_text = extract_text_from_pdf(uploaded_file)
240
+ st.text_area("Extracted Text from PDF:", file_text, height=300)
241
+ elif uploaded_file.type == "text/csv":
242
+ df = pd.read_csv(uploaded_file)
243
+ st.dataframe(df)
244
+ file_text = df.to_string(index=False)
245
+ elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
246
+ df = pd.read_excel(uploaded_file)
247
+ st.dataframe(df)
248
+ file_text = df.to_string(index=False)
249
+ elif uploaded_file.type == "application/json":
250
+ df = pd.read_json(uploaded_file)
251
+ st.json(df.to_dict())
252
+ file_text = df.to_string(index=False)
253
+ elif uploaded_file.type == "text/plain":
254
+ if uploaded_file.name.endswith(".log"):
255
+ file_text = uploaded_file.read().decode("utf-8")
256
+ log_summary = analyze_log_file(file_text.splitlines())
257
+ st.write("Log Summary:")
258
+ st.write(f"Total Lines: {log_summary['total_lines']}")
259
+ st.write(f"Error Count: {log_summary['error_count']}")
260
+ st.write(f"Warning Count: {log_summary['warning_count']}")
261
+ st.write(f"Info Count: {log_summary['info_count']}")
262
+
263
+ st.write("\nError Details:")
264
+ for error, count in log_summary['error_details'].items():
265
+ st.write(f"{count} occurrence(s): {error}")
266
+
267
+ st.write("\nWarning Details:")
268
+ for warning, count in log_summary['warning_details'].items():
269
+ st.write(f"{count} occurrence(s): {warning}")
270
+
271
+ st.write("\nInfo Details:")
272
+ for info, count in log_summary['info_details'].items():
273
+ st.write(f"{count} occurrence(s): {info}")
274
+ else:
275
+ st.error("Please upload a valid log file.")
276
+ else:
277
+ st.error("Unsupported file type.")
278
+ elif uploaded_url:
279
+ file_text = extract_text_from_url(uploaded_url)
280
+ st.text_area("Extracted Text from URL:", file_text, height=300)
281
+
282
+ # Initialize or update session state for context
283
+ if "context" not in st.session_state:
284
+ st.session_state.context = ""
285
+ if "faiss_index" not in st.session_state:
286
+ st.session_state.faiss_index = None
287
+ if "document_chunks" not in st.session_state:
288
+ st.session_state.document_chunks = []
289
+
290
+ def clear_previous_data():
291
+ st.session_state.faiss_index = None
292
+ st.session_state.document_chunks = []
293
+ st.session_state.context = ""
294
+
295
+ submit = st.button("Start Deep Diving 🀿", key="start_button")
296
+
297
+ if submit:
298
+ if input_prompt or file_text:
299
+ clear_previous_data()
300
+
301
+ prompt = input_prompt if input_prompt else ""
302
+ st.session_state.context += " " + file_text # Update the context with new extracted text
303
+
304
+ if file_text:
305
+ st.session_state.faiss_index, st.session_state.document_chunks = store_embeddings(file_text)
306
+
307
+ # Start spinner before processing
308
+ spinner = st.spinner("Processing..... Getting Results ⏳")
309
+ with spinner:
310
+ response = query_gemini(st.session_state.context, prompt, image)
311
+
312
+ # Stop spinner after processing
313
+ if response:
314
+ st.subheader("Extracted Data πŸ“‘")
315
+ st.write(response)
316
+
317
+ clean_response = clean_text(response)
318
+
319
+ # Text-to-Speech conversion
320
+ tts = gTTS(clean_response)
321
+ audio_file = BytesIO()
322
+ tts.write_to_fp(audio_file)
323
+ st.audio(audio_file, format='audio/mp3')
324
+ else:
325
+ st.warning("Please provide an input prompt or upload a file.")
326
+
327
+ # Q&A section with slider and radio button
328
+ st.markdown("-----")
329
+ st.markdown("**Q/A Section πŸ€”**")
330
+
331
+ query = st.text_input("Enter your query:", key="qa_query")
332
+ top_k = st.slider("Select the number of document chunks to retrieve:", min_value=1, max_value=10, value=5, step=1)
333
+ response_mode = st.radio("Select response mode:", ("Text", "Text-to-Speech"))
334
+
335
+ qa_button = st.button("Ask", key="qa_button")
336
+
337
+ if qa_button:
338
+ if query:
339
+ spinner = st.spinner("Processing your query...")
340
+ with spinner:
341
+ response = handle_qa(query, st.session_state.faiss_index, st.session_state.document_chunks, top_k)
342
+ if response:
343
+ st.divider()
344
+ st.markdown("**Q&A Response πŸ€–**")
345
+
346
+ clean_response = clean_text(response)
347
+
348
+ if response_mode == "Text":
349
+ st.write(response)
350
+ else:
351
+ st.write(response)
352
+ tts = gTTS(clean_response)
353
+ audio_file = BytesIO()
354
+ tts.write_to_fp(audio_file)
355
+ st.audio(audio_file, format='audio/mp3')
356
+ else:
357
+ st.warning("Please enter a query to ask.")
358
+
359
+ st.markdown("-----")
360
+
361
+ # Voice recognition section
362
+ # st.markdown("**Voice Input πŸ—£οΈ**")
363
+ # query = recognize_speech()
364
+ # if st.button("Start Voice Recognition") and query:
365
+ # with st.spinner("Processing your voice query..."):
366
+ # response = handle_qa(query, st.session_state.faiss_index, st.session_state.document_chunks, top_k)
367
+ # if response:
368
+ # st.divider()
369
+ # st.markdown("**Voice Q&A Response πŸ€–**")
370
+
371
+ # clean_response = clean_text(response)
372
+ # st.write(clean_response)
373
+ # tts = gTTS(clean_response)
374
+ # audio_file = BytesIO()
375
+ # tts.write_to_fp(audio_file)
376
+ # st.audio(audio_file, format='audio/mp3')
377
+ # st.markdown("---")
378
+ linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
379
+ st.markdown(f"Created with πŸ€— πŸ’– By Aditya Pandey [ LinkedIn πŸ”— ]({linkedin_url})")
380
+
381
+ # Description and Framework Section
382
+ def render_description_and_framework():
383
+ st.title("OxSecure RAG - Description and Framework")
384
+ st.markdown("----")
385
+ st.markdown("""
386
+ ## πŸš€ ***Project Description***
387
+ ----------------
388
+ **OxSecure RAG** is your cybersecurity research companion! Powered by the Gemini API and crafted with smart prompts, it can analyze various documents, extract key insights, create embeddings, and support question-answering (Q&A) like never before. πŸ”πŸ›‘οΈ
389
+
390
+ πŸ› οΈ ***Framework Used***
391
+ - **Streamlit**: The sleek and interactive interface 🎨.
392
+ - **FAISS**: Super-efficient similarity search and clustering for dense vectors ⚑.
393
+ - **Pandas**: Handling and processing data files like a pro (CSV, Excel) πŸ“Š.
394
+ - **PyPDF2**: Extracting text from PDFs with ease πŸ“„.
395
+ - **BeautifulSoup**: Scraping web data with precision 🌐.
396
+ - **gTTS**: Giving the bot a voice with text-to-speech πŸŽ™οΈ.
397
+ - **Google Generative AI (genai)**: Querying the powerful Gemini API 🧠.
398
+ - **SpeechRecognition**: Turning your voice into input for hands-free interaction 🎧.
399
+
400
+ ----------------
401
+
402
+ πŸ—οΈ ***Architecture***
403
+ 1. **Input Handling**:
404
+ - Upload various file types (PDF, CSV, Excel, JSON) or provide a URL πŸ”—.
405
+ - Input text prompts directly πŸ“.
406
+ - Speak your query using voice recognition 🎀.
407
+ 2. **Text Extraction**:
408
+ - Extract text from uploaded files or URLs using the right tools πŸ“„πŸŒ.
409
+ 3. **Text Embedding**:
410
+ - Split extracted text into chunks and convert them into embeddings 🧩.
411
+ - Store embeddings in a FAISS index for fast, relevant search results πŸš€.
412
+ 4. **Q&A System**:
413
+ - Ask questions based on uploaded or entered context ❓.
414
+ - Retrieve relevant text chunks from the FAISS index and query the Gemini API πŸ”.
415
+ 5. **Response Generation**:
416
+ - View the response from the Gemini API πŸ§‘β€πŸ’».
417
+ - Convert the response to speech for audio playback πŸ”Š.
418
+
419
+ ----------------
420
+
421
+ πŸ“‹ ***Instructions for Use***
422
+ 1. **Input**:
423
+ - Upload a file (PDF, CSV, Excel, or JSON), provide a URL, or enter a text prompt πŸ’».
424
+ 2. **Processing**:
425
+ - Click "Start Deep Diving" to process the input and extract valuable insights πŸ’‘.
426
+ 3. **Q&A**:
427
+ - Enter a query, choose how many document chunks to retrieve, and select response mode (Text or Text-to-Speech) 🎯.
428
+ - Click "Ask" to get your answer 🧠.
429
+ 4. **Voice Input**:
430
+ - Use "Start Voice Recognition" to ask a question verbally πŸŽ™οΈ.
431
+ - The answer will be generated and spoken aloud πŸ—£οΈ.
432
+ 5. **Results**:
433
+ - View extracted data and responses in a clear, readable format πŸ“‘.
434
+ - If Text-to-Speech is selected, listen to the response 🎧.
435
+ """)
436
+
437
+ if st.button("Go to Main App", key="description_go_to_main_app"):
438
+ st.session_state.show_main_app = True
439
+ st.experimental_rerun()
440
+
441
+ st.markdown("---")
442
+ linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
443
+ st.markdown(f"Created with πŸ€— πŸ’– By Aditya Pandey [ LinkedIn πŸ”— ]({linkedin_url})")
444
+
445
+ # Initialize the app with the description and framework
446
+ if "show_main_app" not in st.session_state:
447
+ st.session_state.show_main_app = False
448
+
449
+ if st.session_state.show_main_app:
450
+ render_main_app()
451
+ else:
452
+ render_description_and_framework()
453
+
ImaGen.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from PIL import Image
4
+ import google.generativeai as genai
5
+ from constants import gemini_key
6
+
7
+ # Streamlit framework configuration
8
+ st.set_page_config(
9
+ page_title="OxSecure Images",
10
+ page_icon="🎨",
11
+ layout="wide"
12
+ )
13
+
14
+ # API configuration
15
+ os.environ["GOOGLE_API_KEY"] = gemini_key
16
+ genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
17
+
18
+ # Function to load Gemini vision model and get responses
19
+ def get_gemini_response(input_text, image=None):
20
+ model = genai.GenerativeModel('gemini-pro-vision')
21
+ if image is not None:
22
+ response = model.generate_content([input_text, image])
23
+ else:
24
+ response = model.generate_content(input_text)
25
+ return response.text
26
+
27
+ def generate_gemini_image(prompt, num_images=1, size="1024x1024"):
28
+ model = genai.GenerativeModel('gemini-pro-vision')
29
+ # Placeholder for image generation method; replace with actual method
30
+ response = model.generate_content(prompt) # This line should be replaced with the correct method for image generation
31
+ # Assuming the API returns a list of images as URLs or base64 encoded strings
32
+ return response.images[:num_images] # Adjust this line based on actual API response format
33
+
34
+ # Streamlit Main Framework
35
+ st.header('OxSecure ImaGen 🎨')
36
+ st.title('GenAI ImaGen powers ♨️')
37
+ st.subheader('By :- Aadi πŸ§‘β€πŸ’»')
38
+
39
+ # Text input for prompt
40
+ input_text = st.text_input("Input Prompt: ", key="input")
41
+
42
+ # File uploader for image
43
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
44
+ image = None
45
+ if uploaded_file is not None:
46
+ image = Image.open(uploaded_file)
47
+ st.image(image, caption="Uploaded Image.", use_column_width=True)
48
+
49
+ # Button to get response about the image
50
+ submit_analyze = st.button("Tell me about the image")
51
+ if submit_analyze:
52
+ if input_text and image is not None:
53
+ response = get_gemini_response(input_text, image)
54
+ elif image is not None:
55
+ response = get_gemini_response("", image)
56
+ elif input_text:
57
+ response = get_gemini_response(input_text)
58
+ else:
59
+ response = "Please provide an input prompt or upload an image."
60
+ st.subheader("The Response is")
61
+ st.write(response)
62
+
63
+ # Button to generate an image from a prompt
64
+ submit_generate = st.button("Generate Image from Prompt")
65
+ if submit_generate and input_text:
66
+ num_images = st.number_input("Number of Images to Generate", min_value=1, max_value=5, value=1)
67
+ size = st.selectbox("Select Image Size", ["512x512", "1024x1024", "2048x2048"], index=1)
68
+ generated_images = generate_gemini_image(input_text, num_images=num_images, size=size)
69
+ for img in generated_images:
70
+ st.image(img, caption="Generated Image", use_column_width=True)
71
+ else:
72
+ if not input_text:
73
+ st.write("Please provide an input prompt to generate an image.")
app.py ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =============================================================================
2
+ # COPYRIGHT NOTICE
3
+ # -----------------------------------------------------------------------------
4
+ # This source code is the intellectual property of Aditya Pandey.
5
+ # Any unauthorized reproduction, distribution, or modification of this code
6
+ # is strictly prohibited.
7
+ # If you wish to use or modify this code for your project, please ensure
8
+ # to give full credit to Aditya Pandey.
9
+ #
10
+ # PROJECT DESCRIPTION
11
+ # -----------------------------------------------------------------------------
12
+ # This code is for a chatbot crafted with powerful prompts, designed to
13
+ # utilize the Gemini API. It is tailored to assist cybersecurity researchers.
14
+ #
15
+ # Author: Aditya Pandey
16
+ # =============================================================================
17
+ import os
18
+ import streamlit as st
19
+ from PIL import Image
20
+ import textwrap
21
+ from constants import gemini_key
22
+ from langchain_google_genai import ChatGoogleGenerativeAI
23
+ from langchain.llms import OpenAI
24
+ from langchain import PromptTemplate
25
+ import seaborn as sns
26
+ from langchain.chains import LLMChain
27
+ import google.generativeai as genai
28
+ from langchain.memory import ConversationBufferMemory
29
+ from google.generativeai.types import HarmCategory, HarmBlockThreshold, HarmProbability
30
+ from google.generativeai import GenerativeModel
31
+ from langchain.chains import SequentialChain
32
+
33
+ #API configuration
34
+ os.environ["GOOGLE_API_KEY"]=gemini_key
35
+ genai.configure(api_key = os.environ['GOOGLE_API_KEY'])
36
+
37
+ # Define correct username and password
38
+ CORRECT_USERNAME = "Oxsecure"
39
+ CORRECT_PASSWORD = "Oxsecure@123"
40
+
41
+ # streamlit framework
42
+ st.set_page_config(
43
+ page_title="OxSecure",
44
+ page_icon="πŸ”’",
45
+ layout="wide"
46
+ )
47
+
48
+ # Load custom CSS
49
+ def load_css(file_name):
50
+ with open(file_name) as f:
51
+ st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
52
+
53
+ # Load the CSS file
54
+ load_css("ui/Style.css")
55
+
56
+
57
+ def render_login_page():
58
+ st.title("Oxsecure 🧠 - Your Companion! πŸ”’")
59
+ st.markdown("---")
60
+ st.image('ui/Ox.jpg', width=200, use_column_width='always')
61
+ 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 πŸ‘οΈ")
62
+ st.markdown("---")
63
+ st.write("Please log in to continue.")
64
+ st.write("πŸ’³ Default Credentials Username = Oxsecure , Password = Oxsecure@123 ")
65
+ st.divider()
66
+ st.markdown("""
67
+ **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.
68
+
69
+ **Features**
70
+
71
+ **πŸ“– In-Depth Information on Cybersecurity Topics:**
72
+
73
+ 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.
74
+
75
+ **πŸ’» Secure Coding Principles:**
76
+
77
+ 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.
78
+
79
+ **🚨 Major Cyberattacks:**
80
+
81
+ 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.
82
+
83
+ **βš™οΈ Security Misconfiguration:**
84
+
85
+ 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.
86
+
87
+ **πŸ”Ž VirusTotal File Analysis:**
88
+
89
+ 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.
90
+
91
+ **πŸ” Comprehensive File Analysis:**
92
+
93
+ 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.
94
+
95
+ **πŸ€– Powered by Gemini LLM:**
96
+
97
+ 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.
98
+
99
+ **πŸ–ΌοΈ Image Analysis with Imagen:**
100
+
101
+ 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.
102
+
103
+ **Why Choose OxSecure Intelligence?**
104
+
105
+ - **🌐 Comprehensive Coverage:** From basic concepts to advanced practices, this app covers all aspects of cybersecurity.
106
+ - **πŸ“š Expert Guidance:** Learn from detailed articles and guides written by cybersecurity experts.
107
+ - **⚑ Advanced Tools:** Use powerful AI tools like Gemini LLM, Imagen, and VirusTotal to enhance your learning and problem-solving capabilities.
108
+ - **πŸ”„ Stay Updated:** Keep up with the latest trends, threats, and best practices in the cybersecurity field.
109
+
110
+ Join OxSecure Intelligence today and take your cybersecurity knowledge to the next level! πŸš€
111
+ """)
112
+ st.markdown("---")
113
+ linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
114
+ st.markdown(" Created with πŸ€—πŸ’– By Aditya Pandey " f"[ LinkedIn πŸ”—]({linkedin_url})")
115
+
116
+ username = st.sidebar.text_input("Username πŸ‘€")
117
+ password = st.sidebar.text_input("Password πŸ”‘", type="password")
118
+ login_button = st.sidebar.button("Login 🫒")
119
+
120
+ if login_button:
121
+ if username == CORRECT_USERNAME and password == CORRECT_PASSWORD:
122
+ st.session_state.authenticated = True
123
+ st.success("Login successful!")
124
+ st.experimental_rerun()
125
+ render_main_program()
126
+ else:
127
+ st.error("Invalid username or password. Please try again.")
128
+
129
+ ## Function to load Gemini vision model and get response
130
+ def get_gemini_response(input_prompt, image):
131
+ Model = genai.GenerativeModel('gemini-1.5-pro')
132
+ if input_prompt != "":
133
+ response = Model.generate_content([input_prompt, image])
134
+ else:
135
+ response = Model.generate_content(image)
136
+ return response.text
137
+
138
+ def render_main_program():
139
+ st.markdown("# πŸ”’ Unlock the Future of Cybersecurity with OxSecure ")
140
+ st.divider()
141
+ st.markdown("**Where Knowledge Meets Innovation! πŸš€ Dive into Cyber Brilliance with OxSecure** πŸ€– 🌟")
142
+ st.markdown("----")
143
+ app_choice = st.sidebar.radio("Choose App", ("OxSecure Chat πŸ€–", "OxSecure ImaGen 🎨"))
144
+
145
+ if app_choice == "OxSecure Chat πŸ€–":
146
+ render_gemini_api_app()
147
+ elif app_choice == "OxSecure ImaGen 🎨":
148
+ render_gemini_vision_app()
149
+
150
+ def render_gemini_api_app():
151
+ st.caption("πŸš€ Empower Tomorrow, πŸ›‘οΈ Secure Today: Unleash the Power of Cybersecurity Brilliance! πŸ’»βœ¨ πŸ›‘οΈπŸ’¬ ")
152
+ st.markdown("---")
153
+
154
+ st.title("OxSecure Intelligence 🧠")
155
+ st.markdown("-----")
156
+ input_text = st.text_input("Search your Security Related Topic πŸ”")
157
+
158
+ # Prompt Templates
159
+ first_input_prompt = PromptTemplate(
160
+ input_variables=['Topic'],
161
+ template = textwrap.dedent("""
162
+ As an experienced cybersecurity researcher, provide a comprehensive and detailed explanation about {Topic}. Cover the following aspects:
163
+ 1. Introduction and Importance in well informative
164
+ 2. Key Concepts and Terminologies
165
+ 3. Historical Background and Evolution
166
+ 4. Its Architecture and Types
167
+ 5. Current Trends and Best Practices
168
+ 6. Major Threats and Vulnerabilities
169
+ 7. Case Studies and Real-world Examples
170
+ 8. Future Outlook and Predictions
171
+
172
+ Ensure the information is professional, well-structured, key conceptual and suitable for someone with an advanced understanding and Beginner of cybersecurity.
173
+ """)
174
+ )
175
+
176
+ # Select the model
177
+ model = genai.GenerativeModel('gemini-1.5-pro')
178
+ safety_settings = {
179
+ HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
180
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
181
+ HarmCategory.HARM_CATEGORY_DANGEROUS: HarmBlockThreshold.BLOCK_NONE,
182
+ HarmCategory.HARM_CATEGORY_SEXUAL: HarmBlockThreshold.BLOCK_NONE,
183
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
184
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
185
+ HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
186
+ HarmCategory.HARM_CATEGORY_TOXICITY: HarmBlockThreshold.BLOCK_NONE,
187
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmProbability.HIGH
188
+ }
189
+
190
+ # Memory
191
+ Topic_memory = ConversationBufferMemory(input_key='Topic', memory_key='chat_history')
192
+ Policy_memory = ConversationBufferMemory(input_key='secure coding', memory_key='chat_history')
193
+ Practice_memory = ConversationBufferMemory(input_key='Practice', memory_key='description_history')
194
+
195
+ ## GEMINI LLMS
196
+ llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash")
197
+ chain = LLMChain(
198
+ llm=llm, prompt=first_input_prompt, verbose=True, output_key='secure coding', memory=Topic_memory)
199
+ safety_settings = {
200
+ HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
201
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
202
+ HarmCategory.HARM_CATEGORY_DANGEROUS: HarmBlockThreshold.BLOCK_NONE,
203
+ HarmCategory.HARM_CATEGORY_SEXUAL: HarmBlockThreshold.BLOCK_NONE,
204
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
205
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
206
+ HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
207
+ HarmCategory.HARM_CATEGORY_TOXICITY: HarmBlockThreshold.BLOCK_NONE,
208
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmProbability.HIGH
209
+ }
210
+
211
+ # Prompt Templates
212
+ second_input_prompt = PromptTemplate(
213
+ input_variables=['secure coding'],
214
+ 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."
215
+ )
216
+
217
+ chain2 = LLMChain(
218
+ llm=llm, prompt=second_input_prompt, verbose=True, output_key='Practice', memory=Policy_memory)
219
+ # Prompt Templates
220
+ third_input_prompt = PromptTemplate(
221
+ input_variables=['Practice'],
222
+ 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"
223
+ )
224
+ chain3 = LLMChain(llm=llm, prompt=third_input_prompt, verbose=True, output_key='description', memory=Practice_memory)
225
+ parent_chain = SequentialChain(
226
+ chains=[chain, chain2, chain3], input_variables=['Topic'], output_variables=['secure coding', 'Practice',
227
+ 'description'], verbose=True)
228
+
229
+ if input_text:
230
+ with st.spinner('Processing.... ⏳'):
231
+ st.text(parent_chain({'Topic': input_text}))
232
+
233
+ with st.expander('Your Topic'):
234
+ st.info(Topic_memory.buffer)
235
+
236
+ with st.expander('Major Practices'):
237
+ st.info(Practice_memory.buffer)
238
+ st.markdown("---")
239
+ linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
240
+ st.markdown(" Created with πŸ€—πŸ’– By Aditya Pandey " f"[ LinkedIn πŸ”—]({linkedin_url})")
241
+
242
+ def render_gemini_vision_app():
243
+ st.title('OxSecure ImaGen 🎨')
244
+ st.markdown("----")
245
+ input_prompt = st.text_input("Input Prompt: ", key="input")
246
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
247
+ image = ""
248
+ submit = False # Initialize submit variable
249
+
250
+ if uploaded_file is not None:
251
+ image = Image.open(uploaded_file)
252
+ st.image(image, caption="Uploaded Image.", use_column_width=True)
253
+ submit = st.button("Tell me about the image")
254
+
255
+ if submit:
256
+ response = get_gemini_response(input_prompt, image)
257
+ st.subheader("The Response is")
258
+ st.write(response)
259
+
260
+ st.markdown("---")
261
+ linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
262
+ st.markdown(" Created with πŸ€—πŸ’– By Aditya Pandey " f"[ LinkedIn πŸ”—]({linkedin_url})")
263
+
264
+ def main():
265
+ # Initialize session state
266
+ if 'authenticated' not in st.session_state:
267
+ st.session_state.authenticated = False
268
+
269
+ # If not authenticated, display login portal
270
+ if not st.session_state.authenticated:
271
+ render_login_page()
272
+ else:
273
+ render_main_program()
274
+
275
+ if __name__ == "__main__":
276
+ main()
constants.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ openai_key="sk-----"
2
+ gemini_key="AIzaSyD8DucjSYpGMiOTignA_okKlnmAEqhPyzw"
mod2.py ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ## Integrate our code GEMINI API
2
+ import os
3
+ import pathlib
4
+ import textwrap
5
+ from PIL import Image
6
+ from constants import gemini_key
7
+ from langchain_google_genai import ChatGoogleGenerativeAI
8
+ from langchain import PromptTemplate
9
+ from langchain.chains import LLMChain
10
+
11
+ import google.generativeai as genai
12
+
13
+ from langchain.memory import ConversationBufferMemory
14
+ from google.generativeai import GenerativeModel
15
+ from google.generativeai.types import HarmCategory, HarmBlockThreshold, HarmProbability
16
+ from langchain.chains import SequentialChain
17
+
18
+ import streamlit as st
19
+
20
+ # streamlit framework
21
+ st.set_page_config(
22
+ page_title="OxSecure A.I",
23
+ page_icon="πŸ”’",
24
+ layout="wide"
25
+ )
26
+
27
+ # Load custom CSS
28
+ def load_css(file_name):
29
+ with open(file_name) as f:
30
+ st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
31
+
32
+ # Load the CSS file
33
+ load_css("ui/Style.css")
34
+
35
+ #API configuration
36
+
37
+ os.environ["GOOGLE_API_KEY"]=gemini_key
38
+ genai.configure(api_key = os.environ['GOOGLE_API_KEY'])
39
+
40
+ ## Function to load OpenAI model and get respones
41
+
42
+ def get_gemini_response(input, image):
43
+ model = genai.GenerativeModel('gemini-1.5-pro-latest')
44
+ if input != "":
45
+ response = model.generate_content(
46
+ [input, image],
47
+ safety_settings={
48
+ HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
49
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
50
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
51
+ HarmProbability:HarmBlockThreshold.BLOCK_NONE,
52
+ HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE
53
+ }
54
+ )
55
+ else:
56
+ response = model.generate_content(
57
+ image,
58
+ safety_settings={
59
+ HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
60
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
61
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
62
+ HarmProbability:HarmBlockThreshold.BLOCK_NONE,
63
+ HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE
64
+ }
65
+ )
66
+ return response.text
67
+
68
+
69
+ st.title('OxSecure Intelligence 🧠')
70
+ st.caption('Cybersecurity Best practices for Infrastructure')
71
+ st.subheader('By :- Aadi πŸ§‘β€πŸ’»')
72
+ st.text('πŸš€ Empower Tomorrow, πŸ›‘οΈ Secure Today: Unleash the Power of Cybersecurity Brilliance! πŸ’»βœ¨ ')
73
+ input_text=st.text_input("Search Your Desire Security Related Topic πŸ”")
74
+ input=st.text_input("Input Prompt: ",key="input")
75
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
76
+ image=""
77
+ if uploaded_file is not None:
78
+ image = Image.open(uploaded_file)
79
+ st.image(image, caption="Uploaded Image.", use_column_width=True)
80
+ submit=st.button("Tell me about the image")
81
+ if submit:
82
+
83
+ response=get_gemini_response(input,image)
84
+ st.subheader("The Response is")
85
+ st.write(response)
86
+
87
+ # Prompt Templates
88
+
89
+ first_input_prompt=PromptTemplate(
90
+ input_variables=['Topic'],
91
+ template="Tell me everything about and explain in so informative descriptive way about {Topic} "
92
+ )
93
+
94
+ # Memory
95
+
96
+ Topic_memory = ConversationBufferMemory(input_key='Topic', memory_key='chat_history')
97
+ Policy_memory = ConversationBufferMemory(input_key='security policies', memory_key='chat_history')
98
+ Practice_memory = ConversationBufferMemory(input_key='Practice', memory_key='description_history')
99
+
100
+ # GEMINI LLMS
101
+ llm = ChatGoogleGenerativeAI(
102
+ model="gemini-1.5-pro-latest",
103
+ safety_settings={
104
+ HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
105
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
106
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
107
+ HarmProbability:HarmBlockThreshold.BLOCK_NONE,
108
+ HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE
109
+ }
110
+ )
111
+ chain=LLMChain(llm=llm,prompt=first_input_prompt,verbose=True,output_key='security policies',memory=Topic_memory)
112
+
113
+ # Prompt Templates
114
+
115
+ second_input_prompt=PromptTemplate(
116
+ input_variables=['security policies'],
117
+ template="write best {security policies} and perfect code snippet for implementing secure coding to this {Topic} and give me all important full secure coding principles about {Topic} use codes snippet for every countersome points . "
118
+ )
119
+ chain2=LLMChain(
120
+ llm=llm,prompt=second_input_prompt,verbose=True,output_key='Practice',memory=Policy_memory)
121
+ # Prompt Templates
122
+
123
+ third_input_prompt=PromptTemplate(
124
+ input_variables=['Practice'],
125
+ template="Implement 5 major best Cybersecurity {Practice} for this {Topic} that helps better security postures into infrastructure business. give Major cyberattack which is done by this {Topic} and write about malware which is developed by this {Topic}"
126
+ )
127
+ chain3=LLMChain(llm=llm,prompt=third_input_prompt,verbose=True,output_key='description',memory=Practice_memory)
128
+ parent_chain=SequentialChain(
129
+ chains=[chain,chain2,chain3],input_variables=['Topic'],output_variables=['security policies','Practice','description'],verbose=True)
130
+
131
+
132
+
133
+ if input_text:
134
+ st.text(parent_chain({'Topic':input_text}))
135
+
136
+ with st.expander('Your Topic'):
137
+ st.info(Topic_memory.buffer)
138
+
139
+ with st.expander('Major Practices'):
140
+ st.info(Practice_memory.buffer)
141
+ st.markdown("---")
142
+ st.markdown(" Created with ❀️ by Aditya Pandey ")
143
+
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ portaudio19-dev
requirements.txt ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiohttp==3.9.1
2
+ aiosignal==1.3.1
3
+ altair==5.2.0
4
+ annotated-types==0.6.0
5
+ anyio==4.2.0
6
+ attrs==23.2.0
7
+ blinker==1.7.0
8
+ bs4
9
+ cachetools==5.3.2
10
+ chardet
11
+ certifi==2023.11.17
12
+ charset-normalizer==3.3.2
13
+ click==8.1.7
14
+ dataclasses-json==0.6.3
15
+ distro==1.9.0
16
+ frozenlist==1.4.1
17
+ faiss-cpu
18
+ gitdb==4.0.11
19
+ gtts
20
+ GitPython==3.1.41
21
+ google-ai-generativelanguage==0.4.0
22
+ google-api-core==2.15.0
23
+ google-auth==2.26.2
24
+ google-generativeai==0.3.2
25
+ googleapis-common-protos==1.62.0
26
+ greenlet==3.0.3
27
+ grpcio==1.60.0
28
+ grpcio-status==1.60.0
29
+ h11==0.14.0
30
+ httpcore==1.0.2
31
+ httpx==0.26.0
32
+ idna==3.6
33
+ importlib-metadata==7.0.1
34
+ Jinja2==3.1.3
35
+ jsonpatch==1.33
36
+ jsonpointer==2.4
37
+ jsonschema==4.21.0
38
+ jsonschema-specifications==2023.12.1
39
+ langchain==0.1.1
40
+ langchain-community==0.0.13
41
+ langchain_google_genai
42
+ langchain-core==0.1.12
43
+ langsmith==0.0.83
44
+ markdown-it-py==3.0.0
45
+ MarkupSafe==2.1.3
46
+ marshmallow==3.20.2
47
+ mdurl==0.1.2
48
+ multidict==6.0.4
49
+ mypy-extensions==1.0.0
50
+ numpy==1.26.3
51
+ openai==1.8.0
52
+ packaging==23.2
53
+ pandas==2.1.4
54
+ Pillow==10.2.0
55
+ proto-plus==1.23.0
56
+ protobuf==4.25.2
57
+ pyarrow==14.0.2
58
+ pyasn1==0.5.1
59
+ pyasn1-modules==0.3.0
60
+ pydantic==2.5.3
61
+ pydantic_core==2.14.6
62
+ pydeck==0.8.1b0
63
+ PyPDF2
64
+ Pygments==2.17.2
65
+ python-dateutil==2.8.2
66
+ python-dotenv==1.0.0
67
+ pytz==2023.3.post1
68
+ PyYAML==6.0.1
69
+ referencing==0.32.1
70
+ requests==2.31.0
71
+ rich==13.7.0
72
+ rpds-py==0.17.1
73
+ rsa==4.9
74
+ six==1.16.0
75
+ smmap==5.0.1
76
+ sniffio==1.3.0
77
+ SQLAlchemy==2.0.25
78
+ streamlit==1.34.0
79
+ tenacity==8.2.3
80
+ toml==0.10.2
81
+ toolz==0.12.0
82
+ tornado==6.4
83
+ tqdm==4.66.1
84
+ typing-inspect==0.9.0
85
+ typing_extensions==4.9.0
86
+ tzdata==2023.4
87
+ tzlocal==5.2
88
+ urllib3==2.1.0
89
+ validators==0.22.0
90
+ watchdog==3.0.0
91
+ yarl==1.9.4
92
+ zipp==3.17.0
93
+ SpeechRecognition
94
+ pydub
95
+ pyaudio
test.py ADDED
@@ -0,0 +1,842 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # =============================================================================
2
+ # COPYRIGHT NOTICE
3
+ # -----------------------------------------------------------------------------
4
+ # This source code is the intellectual property of Aditya Pandey.
5
+ # Any unauthorized reproduction, distribution, or modification of this code
6
+ # is strictly prohibited.
7
+ # If you wish to use or modify this code for your project, please ensure
8
+ # to give full credit to Aditya Pandey.
9
+ #
10
+ # PROJECT DESCRIPTION
11
+ # -----------------------------------------------------------------------------
12
+ # This code is for a chatbot crafted with powerful prompts, designed to
13
+ # utilize the Gemini API. It is tailored to assist cybersecurity researchers.
14
+ #
15
+ # Author: Aditya Pandey
16
+ # =============================================================================
17
+
18
+ import os
19
+ import streamlit as st
20
+ from PIL import Image
21
+ import textwrap
22
+ from io import BytesIO
23
+ import io
24
+ import chardet
25
+ from constants import gemini_key
26
+ from langchain_google_genai import ChatGoogleGenerativeAI
27
+ from langchain.llms import OpenAI
28
+ from langchain import PromptTemplate
29
+ from langchain.chains import LLMChain
30
+ import google.generativeai as genai
31
+ from langchain.memory import ConversationBufferMemory
32
+ from google.generativeai.types import HarmCategory, HarmBlockThreshold, HarmProbability
33
+ from google.generativeai import GenerativeModel
34
+ from langchain.chains import SequentialChain
35
+ from datetime import datetime
36
+ import matplotlib.pyplot as plt
37
+ import seaborn as sns
38
+ import pandas as pd
39
+ import numpy as np
40
+ import requests
41
+ from pefile import PE, PEFormatError
42
+ import re
43
+ import hashlib
44
+
45
+ # VirusTotal API details
46
+ VIRUSTOTAL_API_KEY = 'ed48e6407e0b7975be7d19c797e1217f500183c9ae84d1119af8628ba4c98c3d'
47
+
48
+
49
+ # API configuration
50
+ os.environ["GOOGLE_API_KEY"] = gemini_key
51
+ genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
52
+
53
+ # Define correct username and password
54
+ CORRECT_USERNAME = "Oxsecure"
55
+ CORRECT_PASSWORD = "Oxsecure@123"
56
+
57
+ # Streamlit framework
58
+ st.set_page_config(
59
+ page_title="OxSecure",
60
+ page_icon="πŸ”’",
61
+ layout="wide"
62
+ )
63
+
64
+ # Load custom CSS
65
+ def load_css(file_name):
66
+ with open(file_name) as f:
67
+ st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
68
+
69
+ # Load the CSS file
70
+ load_css("ui/Style.css")
71
+
72
+ def render_login_page():
73
+ st.title("Oxsecure 🧠 - Your Companion! πŸ”’")
74
+ st.markdown("---")
75
+ st.image('ui/Ox.jpg', width=200, use_column_width='always')
76
+ 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 πŸ‘οΈ")
77
+ st.markdown("---")
78
+ st.write("Please log in to continue.")
79
+ st.write("πŸ’³ Default Credentials Username = Oxsecure , Password = Oxsecure@123 ")
80
+ st.divider()
81
+ st.markdown("""
82
+ **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.
83
+
84
+ **Features**
85
+
86
+ **πŸ“– In-Depth Information on Cybersecurity Topics:**
87
+
88
+ 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.
89
+
90
+ **πŸ’» Secure Coding Principles:**
91
+
92
+ 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.
93
+
94
+ **🚨 Major Cyberattacks:**
95
+
96
+ 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.
97
+
98
+ **βš™οΈ Security Misconfiguration:**
99
+
100
+ 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.
101
+
102
+ **πŸ”Ž VirusTotal File Analysis:**
103
+
104
+ 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.
105
+
106
+ **πŸ” Comprehensive File Analysis:**
107
+
108
+ 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.
109
+
110
+ **πŸ€– Powered by Gemini LLM:**
111
+
112
+ 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.
113
+
114
+ **πŸ–ΌοΈ Image Analysis with Imagen:**
115
+
116
+ 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.
117
+
118
+ **Why Choose OxSecure Intelligence?**
119
+
120
+ - **🌐 Comprehensive Coverage:** From basic concepts to advanced practices, this app covers all aspects of cybersecurity.
121
+ - **πŸ“š Expert Guidance:** Learn from detailed articles and guides written by cybersecurity experts.
122
+ - **⚑ Advanced Tools:** Use powerful AI tools like Gemini LLM, Imagen, and VirusTotal to enhance your learning and problem-solving capabilities.
123
+ - **πŸ”„ Stay Updated:** Keep up with the latest trends, threats, and best practices in the cybersecurity field.
124
+
125
+ Join OxSecure Intelligence today and take your cybersecurity knowledge to the next level! πŸš€
126
+ """)
127
+ st.markdown("---")
128
+ linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
129
+ st.markdown(" Created with πŸ€—πŸ’– By Aditya Pandey " f"[ LinkedIn πŸ”—]({linkedin_url})")
130
+
131
+ username = st.sidebar.text_input("Username πŸ‘€")
132
+ password = st.sidebar.text_input("Password πŸ”‘", type="password")
133
+ login_button = st.sidebar.button("Login 🫒")
134
+
135
+ if login_button:
136
+ if username == CORRECT_USERNAME and password == CORRECT_PASSWORD:
137
+ st.session_state.authenticated = True
138
+ st.success("Login successful!")
139
+ st.experimental_rerun()
140
+ render_main_program()
141
+ else:
142
+ st.error("Invalid username or password. Please try again.")
143
+
144
+ def features():
145
+ st.write("***πŸ”‘ Key Features of OxSecure Intelligence***")
146
+
147
+ with st.expander("πŸ“– In-Depth Information on Cybersecurity Topics"):
148
+ st.write("""
149
+ **Expand Your Cybersecurity Knowledge**
150
+ Stay informed with detailed articles and guides covering a wide range of cybersecurity topics. Whether you're
151
+ learning basic concepts or exploring advanced techniques, this resource ensures you're well-equipped to handle
152
+ the latest cybersecurity challenges.
153
+ """)
154
+
155
+ with st.expander("πŸ’» Secure Coding Principles"):
156
+ st.write("""
157
+ **Write Code that Stands the Test of Time**
158
+ Learn essential best practices for writing secure, reliable code. Our secure coding guides offer practical tips
159
+ and real-world examples to help you minimize vulnerabilities in your software.
160
+ """)
161
+
162
+ with st.expander("🚨 Major Cyberattacks"):
163
+ st.write("""
164
+ **Stay Informed on Critical Threats**
165
+ Keep up-to-date on the most significant cyberattacks around the world. Analyze real-world incidents, learn the
166
+ attack vectors used, and discover defensive strategies to protect against similar threats.
167
+ """)
168
+
169
+ with st.expander("βš™οΈ Security Misconfiguration"):
170
+ st.write("""
171
+ **Configure with Confidence**
172
+ Learn how to avoid common misconfigurations that leave systems exposed. This section provides a comprehensive
173
+ guide to correctly configuring security settings, protecting your organization from unnecessary risks.
174
+ """)
175
+
176
+ with st.expander("πŸ”Ž VirusTotal File Analysis"):
177
+ st.write("""
178
+ **Instant Malware Scanning**
179
+ Upload your files to run advanced malware scans via VirusTotal API. Get real-time reports with detailed threat
180
+ intelligence and analysis, ensuring your files are secure before you use or share them.
181
+ """)
182
+
183
+ with st.expander("πŸ” Comprehensive File Analysis"):
184
+ st.write("""
185
+ **Analyze Multiple File Types**
186
+ OxSecure Intelligence allows you to scan PDFs, images, executables, and logs with ease. From extracting metadata
187
+ to conducting thorough file content analysis, you'll have all the tools you need to secure your files.
188
+ """)
189
+
190
+ with st.expander("πŸ€– Powered by Gemini LLM"):
191
+ st.write("""
192
+ **AI-Powered Insights**
193
+ Harness the cutting-edge power of Gemini LLM to get instant, accurate answers to your cybersecurity queries.
194
+ With AI-driven insights, you can navigate complex data and extract valuable knowledge faster than ever before.
195
+ """)
196
+
197
+ with st.expander("πŸ–ΌοΈ Image Analysis with Imagen"):
198
+ st.write("""
199
+ **Visual Intelligence at Your Fingertips**
200
+ Upload images for detailed analysis using the Imagen feature. Whether you're assessing a security measure or
201
+ scanning for vulnerabilities, this tool ensures you get the most out of every image.
202
+ """)
203
+
204
+ def use_app():
205
+ st.write("***πŸ“‹ How to Use OxSecure Intelligence***")
206
+
207
+ st.write("""
208
+ πŸš€ **OxSecure Intelligence: Use Cases**
209
+
210
+ 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:
211
+
212
+ πŸ›‘οΈ **1. OxSecure Chat**
213
+
214
+ ***Use Case:***
215
+ **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:
216
+
217
+ - **Learning and Research:** Enter any cybersecurity topic to receive a thorough explanation, including secure coding principles and major attack vectors.
218
+ - **Training and Development:** Use the detailed outputs to educate teams or individuals about specific security concepts and practices.
219
+ - **Consultation and Advisory:** Provide clients or stakeholders with well-researched and comprehensive information on cybersecurity issues.
220
+
221
+ **How It Works:**
222
+ 1. Enter a security topic related to cybersecurity.
223
+ 2. Receive a detailed response including:
224
+ - **Secure Coding Principles:** Best practices and guidelines.
225
+ - **Major Attacks:** Common threats and attack methods.
226
+
227
+ πŸ–ΌοΈ **2. OxSecure ImaGen**
228
+
229
+ ***Use Case:***
230
+ **OxSecure ImaGen** offers advanced image analysis by allowing users to input prompts and retrieve detailed information about the image. This tool is perfect for:
231
+
232
+ - **Image Verification:** Analyze images to extract metadata and ensure they are authentic and unaltered.
233
+ - **Content Analysis:** Understand the content and context of images through custom prompts.
234
+ - **Forensic Analysis:** Utilize the tool in digital forensics to scrutinize image details for investigative purposes.
235
+
236
+ **How It Works:**
237
+ 1. Upload an image.
238
+ 2. Enter prompts to specify the desired output.
239
+ 3. Receive detailed information and insights based on the image content.
240
+
241
+ πŸ“‚ **3. File Analysis**
242
+
243
+ ***Use Case:***
244
+ **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:
245
+
246
+ - **File Verification:** Extract metadata and hash information to verify file integrity and authenticity.
247
+ - **Threat Detection:** Communicate with VirusTotal API to assess the file’s security status and identify potential threats.
248
+ - **Visual Analytics:** Obtain graphical representations of file analysis results to visualize threat levels and security metrics.
249
+
250
+ **How It Works:**
251
+ 1. Upload a file.
252
+ 2. Extract metadata and hash information.
253
+ 3. Integrate with VirusTotal API for detailed security analysis.
254
+ 4. View graphical reports and insights about the file.
255
+
256
+ ---
257
+
258
+ **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!
259
+ """)
260
+
261
+ ## Function to load Gemini vision model and get response
262
+ def get_gemini_response(input_prompt, image):
263
+ Model = genai.GenerativeModel('gemini-1.5-pro')
264
+ if input_prompt != "":
265
+ response = Model.generate_content([input_prompt, image])
266
+ else:
267
+ response = Model.generate_content(image)
268
+ return response.text
269
+
270
+
271
+ def render_main_program():
272
+ st.markdown("# πŸ”’ Unlock the Future of Cybersecurity with OxSecure")
273
+ st.divider()
274
+ st.markdown("**Where Knowledge Meets Innovation! πŸš€ Dive into Cyber Brilliance with OxSecure** πŸ€– 🌟")
275
+ st.markdown("----")
276
+
277
+ # Sidebar for navigation
278
+ app_choice = st.sidebar.radio("Choose App",
279
+ ("Features πŸ€ΉπŸ»β€β™€οΈ",
280
+ "OxSecure Chat πŸ€–",
281
+ "OxSecure ImaGen 🎨",
282
+ "File Analysis πŸ“",
283
+ "Help & Uses πŸ’πŸ»"))
284
+
285
+ #Main content selector
286
+ # app_choice = st.selectbox(
287
+ # "Choose App",
288
+ # ["Features πŸ€ΉπŸ»β€β™€οΈ", "OxSecure Chat πŸ€–", "OxSecure ImaGen 🎨", "File Analysis πŸ“", "Help & Uses πŸ’πŸ»"]
289
+ # )
290
+
291
+ # Render the selected app based on user's choice
292
+ if app_choice == "OxSecure Chat πŸ€–":
293
+ render_gemini_api_app()
294
+ elif app_choice == "OxSecure ImaGen 🎨":
295
+ render_gemini_vision_app()
296
+ elif app_choice == "File Analysis πŸ“":
297
+ render_file_analysis_app()
298
+ elif app_choice == "Features πŸ€ΉπŸ»β€β™€οΈ":
299
+ features()
300
+ elif app_choice == "Help & Uses πŸ’πŸ»":
301
+ use_app()
302
+
303
+ def render_gemini_api_app():
304
+ st.caption("πŸš€ Empower Tomorrow, πŸ›‘οΈ Secure Today: Unleash the Power of Cybersecurity Brilliance! πŸ’»βœ¨ πŸ›‘οΈπŸ’¬ ")
305
+ st.markdown("---")
306
+
307
+ st.title("OxSecure Intelligence 🧠")
308
+ st.markdown("-----")
309
+ input_text = st.text_input("Search your Security Related Topic πŸ”")
310
+
311
+ # Prompt Templates
312
+ first_input_prompt = PromptTemplate(
313
+ input_variables=['Topic'],
314
+ template=textwrap.dedent("""
315
+ As an experienced cybersecurity researcher, provide a comprehensive and detailed explanation about {Topic}. Cover the following aspects:
316
+ 1. Introduction and Importance in well informative
317
+ 2. Key Concepts and Terminologies
318
+ 3. Historical Background and Evolution
319
+ 4. Its Architecture and Types
320
+ 5. Current Trends and Best Practices
321
+ 6. Major Threats and Vulnerabilities
322
+ 7. Case Studies and Real-world Examples
323
+ 8. Future Outlook and Predictions
324
+
325
+ Ensure the information is professional, well-structured, key conceptual and suitable for someone with an advanced understanding and Beginner of cybersecurity.
326
+ """)
327
+ )
328
+
329
+ # Select the model
330
+ model = genai.GenerativeModel('gemini-1.5-pro')
331
+ safety_settings = {
332
+ HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
333
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
334
+ HarmCategory.HARM_CATEGORY_DANGEROUS: HarmBlockThreshold.BLOCK_NONE,
335
+ HarmCategory.HARM_CATEGORY_SEXUAL: HarmBlockThreshold.BLOCK_NONE,
336
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
337
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
338
+ HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
339
+ HarmCategory.HARM_CATEGORY_TOXICITY: HarmBlockThreshold.BLOCK_NONE,
340
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmProbability.HIGH
341
+ }
342
+
343
+ # Memory
344
+ Topic_memory = ConversationBufferMemory(input_key='Topic', memory_key='chat_history')
345
+ Policy_memory = ConversationBufferMemory(input_key='secure coding', memory_key='chat_history')
346
+ Practice_memory = ConversationBufferMemory(input_key='Practice', memory_key='description_history')
347
+
348
+ ## GEMINI LLMS
349
+ llm = ChatGoogleGenerativeAI(model="gemini-1.5-pro")
350
+ chain = LLMChain(
351
+ llm=llm, prompt=first_input_prompt, verbose=True, output_key='secure coding', memory=Topic_memory)
352
+ safety_settings = {
353
+ HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
354
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
355
+ HarmCategory.HARM_CATEGORY_DANGEROUS: HarmBlockThreshold.BLOCK_NONE,
356
+ HarmCategory.HARM_CATEGORY_SEXUAL: HarmBlockThreshold.BLOCK_NONE,
357
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
358
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
359
+ HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
360
+ HarmCategory.HARM_CATEGORY_TOXICITY: HarmBlockThreshold.BLOCK_NONE,
361
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmProbability.HIGH
362
+ }
363
+ # Prompt Templates
364
+ second_input_prompt = PromptTemplate(
365
+ input_variables=['secure coding'],
366
+ 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."
367
+ )
368
+
369
+ chain2 = LLMChain(
370
+ llm=llm, prompt=second_input_prompt, verbose=True, output_key='Practice', memory=Policy_memory)
371
+ # Prompt Templates
372
+ third_input_prompt = PromptTemplate(
373
+ input_variables=['Practice'],
374
+ 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"
375
+ )
376
+ chain3 = LLMChain(llm=llm, prompt=third_input_prompt, verbose=True, output_key='description', memory=Practice_memory)
377
+ parent_chain = SequentialChain(
378
+ chains=[chain, chain2, chain3], input_variables=['Topic'], output_variables=['secure coding', 'Practice',
379
+ 'description'], verbose=True)
380
+
381
+ if input_text:
382
+ with st.spinner('Processing.... ⏳'):
383
+ st.text(parent_chain({'Topic': input_text}))
384
+
385
+ with st.expander('Your Topic'):
386
+ st.info(Topic_memory.buffer)
387
+
388
+ with st.expander('Major Practices'):
389
+ st.info(Practice_memory.buffer)
390
+ st.markdown("---")
391
+ linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
392
+ st.markdown(" Created with πŸ€—πŸ’– By Aditya Pandey " f"[ LinkedIn πŸ”—]({linkedin_url})")
393
+
394
+
395
+ def render_gemini_vision_app():
396
+ st.title('OxSecure ImaGen 🎨')
397
+ st.markdown("----")
398
+ input_prompt = st.text_input("Input Prompt: ", key="input")
399
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
400
+ image = ""
401
+ submit = False # Initialize submit variable
402
+
403
+ if uploaded_file is not None:
404
+ image = Image.open(uploaded_file)
405
+ st.image(image, caption="Uploaded Image.", use_column_width=True)
406
+ submit = st.button("Tell me about the image")
407
+
408
+ if submit:
409
+ response = get_gemini_response(input_prompt, image)
410
+ st.subheader("The Response is")
411
+ st.write(response)
412
+
413
+ st.markdown("---")
414
+ linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
415
+ st.markdown(" Created with πŸ€—πŸ’– By Aditya Pandey " f"[ LinkedIn πŸ”—]({linkedin_url})")
416
+
417
+
418
+ def get_file_hash(file):
419
+ file.seek(0) # Reset file pointer to the beginning
420
+ file_hash = hashlib.sha256(file.read()).hexdigest()
421
+ file.seek(0) # Reset file pointer to the beginning
422
+ return file_hash
423
+
424
+ # Function to analyze the file using VirusTotal
425
+ def virustotal_analysis(file_hash):
426
+ url = f"https://www.virustotal.com/api/v3/files/{file_hash}"
427
+ headers = {"x-apikey": VIRUSTOTAL_API_KEY}
428
+ response = requests.get(url, headers=headers)
429
+ if response.status_code == 200:
430
+ return response.json()
431
+ else:
432
+ st.error("Error with VirusTotal API request. Please check your API key or the file hash.")
433
+ return None
434
+
435
+ # Function to extract metadata from PE files
436
+ def extract_metadata(file):
437
+ try:
438
+ pe = PE(data=file.read())
439
+ metadata = {
440
+ "Number of Sections": pe.FILE_HEADER.NumberOfSections,
441
+ "Time Date Stamp": pe.FILE_HEADER.TimeDateStamp,
442
+ "Characteristics": pe.FILE_HEADER.Characteristics,
443
+ }
444
+ return metadata
445
+ except PEFormatError:
446
+ st.error("Uploaded file is not a valid PE format.")
447
+ return None
448
+
449
+
450
+ def analyze_log_file(log_content):
451
+ # Data storage structures for IPs, Domains, Headers, Sessions
452
+ ip_data = []
453
+ domain_data = []
454
+ header_data = []
455
+ id_data = []
456
+
457
+ # Regular expressions for matching
458
+ ip_regex = re.compile(r'\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b')
459
+ domain_regex = re.compile(r'\b[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b')
460
+ header_regex = re.compile(r'(User-Agent|Content-Type|Authorization):\s*(.*)', re.IGNORECASE)
461
+ id_regex = re.compile(r'\b(?:SessionID|UserID|ID|id|sessionid|userid)\s*[:=\s]\s*([a-zA-Z0-9-]+)', re.IGNORECASE)
462
+
463
+ log_entries = []
464
+
465
+ for line in log_content.splitlines():
466
+ # Match IPs
467
+ ips = ip_regex.findall(line)
468
+ if ips:
469
+ ip_data.extend(ips)
470
+
471
+ # Match Domains
472
+ domains = domain_regex.findall(line)
473
+ if domains:
474
+ domain_data.extend(domains)
475
+
476
+ # Match Headers
477
+ headers = header_regex.findall(line)
478
+ if headers:
479
+ header_data.extend(headers)
480
+
481
+ # Match IDs (Session IDs, User IDs, etc.)
482
+ ids = id_regex.findall(line)
483
+ if ids:
484
+ id_data.extend(ids)
485
+
486
+ log_entries.append(line)
487
+
488
+ # Convert to DataFrame
489
+ log_df = pd.DataFrame(log_entries, columns=["Log Entries"])
490
+
491
+ # Additional DataFrames for captured data
492
+ ip_df = pd.DataFrame(ip_data, columns=["IP Addresses"])
493
+ domain_df = pd.DataFrame(domain_data, columns=["Domains"])
494
+ header_df = pd.DataFrame(header_data, columns=["Header Name", "Header Value"])
495
+ id_df = pd.DataFrame(id_data, columns=["IDs"])
496
+
497
+ # Summary of findings
498
+ summary = {
499
+ "log_dataframe": log_df,
500
+ "ip_dataframe": ip_df,
501
+ "domain_dataframe": domain_df,
502
+ "header_dataframe": header_df,
503
+ "id_dataframe": id_df
504
+ }
505
+
506
+ return summary
507
+
508
+ # Function to create charts from VirusTotal results with theme selection
509
+ def create_virus_total_charts(virus_total_results, theme="light"):
510
+ if not virus_total_results:
511
+ return None
512
+
513
+ # Extract the data for the charts
514
+ stats = virus_total_results['data']['attributes']['last_analysis_stats']
515
+ labels = list(stats.keys())
516
+ values = list(stats.values())
517
+
518
+ # Convert data to DataFrame for better handling
519
+ df = pd.DataFrame({'Analysis Types': labels, 'Count': values})
520
+
521
+ # Set the background color theme based on user input
522
+ if theme == "dark":
523
+ plt.style.use("dark_background")
524
+ text_color = 'white'
525
+ else:
526
+ plt.style.use("default")
527
+ text_color = 'black'
528
+
529
+ # Create a container (figure) with 3 rows and 2 columns of charts
530
+ fig, axs = plt.subplots(3, 2, figsize=(18, 18)) # 3 rows and 2 columns of charts
531
+
532
+ # --- Bar Chart ---
533
+ sns.barplot(x='Analysis Types', y='Count', data=df, palette="coolwarm", ax=axs[0, 0])
534
+ axs[0, 0].set_title("VirusTotal Analysis Results (Bar Chart)", fontsize=14, fontweight='bold', color=text_color)
535
+ axs[0, 0].tick_params(axis='x', rotation=45, labelsize=10, labelcolor=text_color) # Rotate x-axis labels
536
+
537
+ # Add value labels on the bar chart
538
+ for p in axs[0, 0].patches:
539
+ axs[0, 0].annotate(f'{int(p.get_height())}', (p.get_x() + p.get_width() / 2., p.get_height()),
540
+ ha='center', va='baseline', fontsize=10, color=text_color, xytext=(0, 3),
541
+ textcoords='offset points')
542
+
543
+ # --- Horizontal Bar Chart ---
544
+ sns.barplot(y='Analysis Types', x='Count', data=df, palette="magma", ax=axs[0, 1], orient='h')
545
+ axs[0, 1].set_title("VirusTotal Analysis Results (Horizontal Bar)", fontsize=14, fontweight='bold', color=text_color)
546
+ axs[0, 1].tick_params(axis='y', labelsize=10, labelcolor=text_color)
547
+
548
+ # Add value labels on horizontal bar chart
549
+ for p in axs[0, 1].patches:
550
+ axs[0, 1].annotate(f'{int(p.get_width())}', (p.get_width(), p.get_y() + p.get_height() / 2),
551
+ ha='center', va='center_baseline', fontsize=10, color=text_color, xytext=(5, 0),
552
+ textcoords='offset points')
553
+
554
+ # --- Pie Chart ---
555
+ wedges, texts, autotexts = axs[1, 0].pie(values, labels=labels, autopct='%1.1f%%', startangle=90,
556
+ colors=sns.color_palette("coolwarm", len(labels)),
557
+ wedgeprops=dict(edgecolor=text_color))
558
+
559
+ # Format the text and labels
560
+ for text in texts:
561
+ text.set_fontsize(10)
562
+ text.set_color(text_color)
563
+
564
+ for autotext in autotexts:
565
+ autotext.set_color(text_color)
566
+
567
+ axs[1, 0].set_title("VirusTotal Analysis Results (Pie Chart)", fontsize=14, fontweight='bold', color=text_color)
568
+ axs[1, 0].axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
569
+
570
+ # --- Donut Chart ---
571
+ wedges, texts, autotexts = axs[1, 1].pie(values, labels=labels, autopct='%1.1f%%', startangle=90,
572
+ pctdistance=0.85, colors=sns.color_palette("Set2", len(labels)),
573
+ wedgeprops=dict(width=0.4, edgecolor=text_color)) # Donut chart
574
+
575
+ # Format the text and labels for Donut Chart
576
+ for text in texts:
577
+ text.set_fontsize(10)
578
+ text.set_color(text_color)
579
+
580
+ for autotext in autotexts:
581
+ autotext.set_color(text_color)
582
+
583
+ axs[1, 1].set_title("VirusTotal Analysis Results (Donut Chart)", fontsize=14, fontweight='bold', color=text_color)
584
+ axs[1, 1].axis('equal') # Equal aspect ratio for donut shape
585
+
586
+ # --- Heatmap (Random Example) ---
587
+ random_data = np.random.rand(len(labels), len(labels)) # Create a dummy heatmap based on the stats
588
+ sns.heatmap(random_data, annot=True, cmap="Blues", ax=axs[2, 0], cbar_kws={'label': 'Intensity'})
589
+ axs[2, 0].set_title("Random Heatmap (Dummy)", fontsize=14, fontweight='bold', color=text_color)
590
+ axs[2, 0].set_xticklabels(labels, rotation=45, color=text_color)
591
+ axs[2, 0].set_yticklabels(labels, rotation=0, color=text_color)
592
+
593
+ # --- Scatter Plot ---
594
+ sns.scatterplot(x=labels, y=values, hue=values, palette="deep", s=100, ax=axs[2, 1], legend=False)
595
+ axs[2, 1].set_title("VirusTotal Analysis Results (Scatter Plot)", fontsize=14, fontweight='bold', color=text_color)
596
+ axs[2, 1].set_xlabel("Analysis Types", fontsize=12, color=text_color)
597
+ axs[2, 1].set_ylabel("Count", fontsize=12, color=text_color)
598
+ axs[2, 1].tick_params(axis='x', rotation=45, labelcolor=text_color)
599
+ axs[2, 1].tick_params(axis='y', labelcolor=text_color)
600
+
601
+ # Adjust layout for better spacing and clarity
602
+ fig.tight_layout(pad=4.0)
603
+
604
+ # Set background based on theme
605
+ fig.patch.set_facecolor('black' if theme == "dark" else 'white')
606
+
607
+ return fig
608
+
609
+ # Function to create detailed tables from JSON data
610
+ def create_detailed_table(data, title):
611
+ st.write(f"{title}")
612
+
613
+ # Normalize JSON data into a DataFrame
614
+ df = pd.json_normalize(data)
615
+
616
+ # Debug: Show raw data and DataFrame
617
+ st.write("Raw Data:", data)
618
+
619
+ if df.empty:
620
+ st.write("No data available.")
621
+ else:
622
+ # Apply minimal styling for debugging
623
+ styled_df = df.style.background_gradient(cmap='viridis') \
624
+ .format(na_rep='N/A', precision=2)
625
+
626
+ # Display the styled DataFrame
627
+ st.dataframe(styled_df)
628
+
629
+ # Function to display the analysis results on the dashboard
630
+ def display_analysis_results(metadata, virus_total_results, log_analysis=None):
631
+ st.write("## Analysis Results")
632
+
633
+ # Metadata
634
+ if metadata:
635
+ st.write("### πŸ“‚ PE File Metadata")
636
+ create_detailed_table(metadata, "PE File Metadata")
637
+
638
+ # VirusTotal Results
639
+ if virus_total_results:
640
+ st.write("### 🦠 VirusTotal Results")
641
+ create_detailed_table(virus_total_results['data'], "VirusTotal Results")
642
+ st.write("#### πŸ“Š VirusTotal Analysis Stats")
643
+ st.markdown("------")
644
+ fig = create_virus_total_charts(virus_total_results)
645
+ if fig:
646
+ st.pyplot(fig)
647
+
648
+ # Log Analysis
649
+ if log_analysis is not None:
650
+ st.write("### πŸ“ Log Analysis")
651
+ st.markdown("------")
652
+ col1, col2 = st.columns(2)
653
+
654
+ with col1:
655
+ st.write("**IP Addresses:**")
656
+ st.dataframe(log_analysis.get("ip_dataframe"))
657
+
658
+ with col2:
659
+ st.write("**Domains:**")
660
+ st.dataframe(log_analysis.get("domain_dataframe"))
661
+
662
+ col3, col4, col5 = st.columns([2, 1, 1])
663
+ st.markdown("----------")
664
+
665
+ with col3:
666
+ st.write("**Log Entries:**")
667
+ st.dataframe(log_analysis.get("log_dataframe"))
668
+
669
+ with col4:
670
+ st.write("**IDs (Session/User/Generic):**")
671
+ if not log_analysis.get("id_dataframe").empty:
672
+ st.dataframe(log_analysis.get("id_dataframe"))
673
+ else:
674
+ st.write("No IDs found.")
675
+
676
+
677
+ with col5:
678
+ st.write("**Headers:**")
679
+ if not log_analysis.get("header_dataframe").empty:
680
+ st.dataframe(log_analysis.get("header_dataframe"))
681
+ else:
682
+ st.write("No headers found.")
683
+
684
+ def read_file_with_fallback(byte_data):
685
+ try:
686
+ # Attempt to read the file with UTF-8 encoding
687
+ return byte_data.decode("utf-8")
688
+ except UnicodeDecodeError:
689
+ # If UTF-8 decoding fails, try to detect encoding
690
+ byte_stream = io.BytesIO(byte_data)
691
+ detected_encoding = chardet.detect(byte_data)['encoding']
692
+ byte_stream.seek(0) # Reset stream pointer
693
+ return byte_stream.read().decode(detected_encoding, errors='replace')
694
+
695
+
696
+ def render_file_analysis_app():
697
+ st.title("πŸ” File Analysis Dashboard")
698
+ st.markdown("---")
699
+ st.image('ui/antivirus.png', width=80, use_column_width='none')
700
+
701
+ uploaded_file = st.file_uploader("Upload any file for analysis", type=["exe", "dll", "log", "pdf", "png", "jpg", "jpeg", "gif", "txt", "zip", "rar", "apk"])
702
+
703
+ if uploaded_file:
704
+ file_hash = get_file_hash(uploaded_file)
705
+ st.write(f"SHA-256 Hash: {file_hash}")
706
+
707
+ file_extension = uploaded_file.name.split('.')[-1].lower()
708
+
709
+ # Handle different file types
710
+ if file_extension in ['png', 'jpg', 'jpeg', 'gif']:
711
+ st.write("### πŸ“„ Image Preview")
712
+ image = Image.open(uploaded_file)
713
+ image.thumbnail((512, 512)) # Resize for preview
714
+ st.image(image, width=240, caption='Uploaded Image', use_column_width=True)
715
+ # Save uploaded file temporarily
716
+ file_path = f"./temp/{uploaded_file.name}"
717
+ os.makedirs(os.path.dirname(file_path), exist_ok=True)
718
+ with open(file_path, "wb") as f:
719
+ f.write(uploaded_file.getbuffer())
720
+
721
+ try:
722
+ with open(file_path, "rb") as file:
723
+ file_hash = get_file_hash(file)
724
+ metadata = extract_metadata(file)
725
+ virus_total_results = virustotal_analysis(file_hash)
726
+
727
+ finally:
728
+ # Clean up
729
+ os.remove(file_path)
730
+
731
+ log_analysis = None
732
+
733
+
734
+ elif file_extension == 'pdf':
735
+ st.write("### πŸ“„ PDF File")
736
+ st.write("PDF preview is not supported. Please use other tools to view.")
737
+ st.download_button(label="Download PDF", data=uploaded_file, file_name=uploaded_file.name)
738
+ # Save uploaded file temporarily
739
+ file_path = f"./temp/{uploaded_file.name}"
740
+ os.makedirs(os.path.dirname(file_path), exist_ok=True)
741
+ with open(file_path, "wb") as f:
742
+ f.write(uploaded_file.getbuffer())
743
+
744
+ try:
745
+ with open(file_path, "rb") as file:
746
+ file_hash = get_file_hash(file)
747
+ metadata = extract_metadata(file)
748
+ virus_total_results = virustotal_analysis(file_hash)
749
+
750
+ finally:
751
+ # Clean up
752
+ os.remove(file_path)
753
+
754
+ log_analysis = None
755
+
756
+ elif file_extension in ['txt', 'log']:
757
+ st.write("### πŸ“ Log File Content")
758
+ log_content = read_file_with_fallback(uploaded_file.getvalue())
759
+ log_analysis = analyze_log_file(log_content)
760
+ # Save uploaded file temporarily
761
+ file_path = f"./temp/{uploaded_file.name}"
762
+ os.makedirs(os.path.dirname(file_path), exist_ok=True)
763
+ with open(file_path, "wb") as f:
764
+ f.write(uploaded_file.getbuffer())
765
+
766
+ try:
767
+ with open(file_path, "rb") as file:
768
+ file_hash = get_file_hash(file)
769
+ metadata = extract_metadata(file)
770
+ virus_total_results = virustotal_analysis(file_hash)
771
+
772
+ finally:
773
+ # Clean up
774
+ os.remove(file_path)
775
+
776
+ log_analysis = analyze_log_file(log_content)
777
+
778
+ elif file_extension in ['zip', 'rar']:
779
+ st.write("### πŸ“¦ Compressed File")
780
+ st.write("Compressed files require further extraction and analysis.")
781
+ # Save uploaded file temporarily
782
+ file_path = f"./temp/{uploaded_file.name}"
783
+ os.makedirs(os.path.dirname(file_path), exist_ok=True)
784
+ with open(file_path, "wb") as f:
785
+ f.write(uploaded_file.getbuffer())
786
+
787
+ try:
788
+ with open(file_path, "rb") as file:
789
+ file_hash = get_file_hash(file)
790
+ metadata = extract_metadata(file)
791
+ virus_total_results = virustotal_analysis(file_hash)
792
+
793
+ finally:
794
+ # Clean up
795
+ os.remove(file_path)
796
+
797
+ log_analysis = None
798
+
799
+ elif file_extension in ['apk', 'exe', 'dll']:
800
+ # Save uploaded file temporarily
801
+ file_path = f"./temp/{uploaded_file.name}"
802
+ os.makedirs(os.path.dirname(file_path), exist_ok=True)
803
+ with open(file_path, "wb") as f:
804
+ f.write(uploaded_file.getbuffer())
805
+
806
+ try:
807
+ with open(file_path, "rb") as file:
808
+ file_hash = get_file_hash(file)
809
+ metadata = extract_metadata(file)
810
+ virus_total_results = virustotal_analysis(file_hash)
811
+
812
+ finally:
813
+ # Clean up
814
+ os.remove(file_path)
815
+
816
+ log_analysis = None
817
+
818
+ else:
819
+ st.error("Unsupported file type.")
820
+ metadata = None
821
+ virus_total_results = None
822
+ log_analysis = None
823
+
824
+ display_analysis_results(metadata, virus_total_results, log_analysis)
825
+
826
+
827
+ st.markdown("---")
828
+ linkedin_url = "https://www.linkedin.com/in/aditya-pandey-896109224"
829
+ st.markdown(" Created with πŸ€—πŸ’– By Aditya Pandey " f"[ LinkedIn πŸ”—]({linkedin_url})")
830
+
831
+
832
+ def main():
833
+ if 'authenticated' not in st.session_state:
834
+ st.session_state.authenticated = False
835
+
836
+ if st.session_state.authenticated:
837
+ render_main_program()
838
+ else:
839
+ render_login_page()
840
+
841
+ if __name__ == "__main__":
842
+ main()
vt.py ADDED
@@ -0,0 +1,317 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import hashlib
4
+ import pandas as pd
5
+ import json
6
+ import matplotlib.pyplot as plt
7
+ import seaborn as sns
8
+ from io import BytesIO
9
+ from PIL import Image
10
+ from pefile import PE, PEFormatError
11
+ import os
12
+ import re
13
+
14
+ # VirusTotal API details
15
+ VIRUSTOTAL_API_KEY = 'ed48e6407e0b7975be7d19c797e1217f500183c9ae84d1119af8628ba4c98c3d'
16
+
17
+ # streamlit framework
18
+ st.set_page_config(
19
+ page_title="OxThreat",
20
+ page_icon="πŸ”",
21
+ layout="wide"
22
+ )
23
+
24
+ # Function to calculate the file's SHA-256 hash
25
+ def get_file_hash(file):
26
+ file.seek(0) # Reset file pointer to the beginning
27
+ file_hash = hashlib.sha256(file.read()).hexdigest()
28
+ file.seek(0) # Reset file pointer to the beginning
29
+ return file_hash
30
+
31
+ # Function to analyze the file using VirusTotal
32
+ def virustotal_analysis(file_hash):
33
+ url = f"https://www.virustotal.com/api/v3/files/{file_hash}"
34
+ headers = {"x-apikey": VIRUSTOTAL_API_KEY}
35
+ response = requests.get(url, headers=headers)
36
+ if response.status_code == 200:
37
+ return response.json()
38
+ else:
39
+ st.error("Error with VirusTotal API request. Please check your API key or the file hash.")
40
+ return None
41
+
42
+ # Function to extract metadata from PE files
43
+ def extract_metadata(file):
44
+ try:
45
+ pe = PE(data=file.read())
46
+ metadata = {
47
+ "Number of Sections": pe.FILE_HEADER.NumberOfSections,
48
+ "Time Date Stamp": pe.FILE_HEADER.TimeDateStamp,
49
+ "Characteristics": pe.FILE_HEADER.Characteristics,
50
+ }
51
+ return metadata
52
+ except PEFormatError:
53
+ st.error("Uploaded file is not a valid PE format.")
54
+ return None
55
+
56
+ # Function to analyze log files
57
+ def analyze_log_file(log_content):
58
+ errors = re.findall(r'ERROR.*', log_content)
59
+ return pd.DataFrame(errors, columns=["Errors"])
60
+
61
+ # Function to create charts from VirusTotal results
62
+ def create_virus_total_charts(virus_total_results):
63
+ if not virus_total_results:
64
+ return None
65
+
66
+ stats = virus_total_results['data']['attributes']['last_analysis_stats']
67
+ labels = list(stats.keys())
68
+ values = list(stats.values())
69
+
70
+ fig, ax = plt.subplots(figsize=(10, 5))
71
+ sns.barplot(x=labels, y=values, palette="viridis", ax=ax)
72
+ ax.set_title("VirusTotal Analysis Results", fontsize=16, fontweight='bold')
73
+ ax.set_xlabel("Analysis Types", fontsize=14)
74
+ ax.set_ylabel("Count", fontsize=14)
75
+
76
+ return fig
77
+
78
+ # Function to create detailed tables from JSON data
79
+ def create_detailed_table(data, title):
80
+ st.write(f"### {title}")
81
+
82
+ # Normalize JSON data into a DataFrame
83
+ df = pd.json_normalize(data)
84
+
85
+ # Debug: Show raw data and DataFrame
86
+ st.write("Raw Data:", data)
87
+
88
+ if df.empty:
89
+ st.write("No data available.")
90
+ else:
91
+ # Apply minimal styling for debugging
92
+ styled_df = df.style.background_gradient(cmap='viridis') \
93
+ .format(na_rep='N/A', precision=2)
94
+
95
+ # Display the styled DataFrame
96
+ st.dataframe(styled_df)
97
+
98
+ # Function to display the analysis results on the dashboard
99
+ def display_analysis_results(metadata, virus_total_results, log_analysis=None):
100
+ st.write("## Analysis Results")
101
+
102
+ col1, col2 = st.columns([2, 1])
103
+
104
+ # Metadata
105
+ with col1:
106
+ if metadata:
107
+ st.write("### πŸ“‚ PE File Metadata")
108
+ create_detailed_table(metadata, "PE File Metadata")
109
+
110
+ # VirusTotal Results
111
+ with col1:
112
+ if virus_total_results:
113
+ st.write("### 🦠 VirusTotal Results")
114
+ create_detailed_table(virus_total_results['data'], "VirusTotal Results")
115
+ st.write("#### πŸ“Š VirusTotal Analysis Stats")
116
+ fig = create_virus_total_charts(virus_total_results)
117
+ if fig:
118
+ st.pyplot(fig)
119
+
120
+ # Log Analysis
121
+ with col2:
122
+ if log_analysis is not None:
123
+ st.write("### πŸ“ Log Analysis")
124
+ st.table(log_analysis)
125
+
126
+ # Main page of the Streamlit app
127
+ def main_page():
128
+ st.title("🦠 Malware Analysis Tool")
129
+ st.markdown("---")
130
+ st.image('ui/antivirus.png', width=200, use_column_width='always')
131
+
132
+ if st.button("Go to File Analysis πŸ—‚οΈ"):
133
+ st.session_state.page = "file_analysis"
134
+ st.experimental_rerun()
135
+
136
+ # File analysis page where the user can upload files for analysis
137
+ def file_analysis_page():
138
+ st.title("πŸ” File Analysis Dashboard")
139
+ st.markdown("---")
140
+ st.image('ui/antivirus.png', width=80, use_column_width='none')
141
+
142
+ uploaded_file = st.file_uploader("Upload any file for analysis", type=["exe", "dll", "log", "pdf", "png", "jpg", "jpeg", "gif", "txt", "zip", "rar", "apk"])
143
+
144
+ if uploaded_file:
145
+ file_hash = get_file_hash(uploaded_file)
146
+ st.write(f"SHA-256 Hash: {file_hash}")
147
+
148
+ file_extension = uploaded_file.name.split('.')[-1].lower()
149
+
150
+ # Handle different file types
151
+ if file_extension in ['png', 'jpg', 'jpeg', 'gif']:
152
+ st.write("### πŸ“„ Image Preview")
153
+ image = Image.open(uploaded_file)
154
+ image.thumbnail((150, 150)) # Resize for preview
155
+ st.image(image, caption='Uploaded Image', use_column_width=True)
156
+ metadata = None
157
+ virus_total_results = None
158
+ log_analysis = None
159
+
160
+ elif file_extension == 'pdf':
161
+ st.write("### πŸ“„ PDF File")
162
+ st.write("PDF preview is not supported. Please use other tools to view.")
163
+ st.download_button(label="Download PDF", data=uploaded_file, file_name=uploaded_file.name)
164
+ metadata = None
165
+ virus_total_results = None
166
+ log_analysis = None
167
+
168
+ elif file_extension in ['txt', 'log']:
169
+ st.write("### πŸ“ Log File Content")
170
+ log_content = uploaded_file.getvalue().decode("utf-8")
171
+ log_analysis = analyze_log_file(log_content)
172
+ metadata = None
173
+ virus_total_results = None
174
+
175
+ elif file_extension in ['zip', 'rar']:
176
+ st.write("### πŸ“¦ Compressed File")
177
+ st.write("Compressed files require further extraction and analysis.")
178
+ metadata = None
179
+ virus_total_results = None
180
+ log_analysis = None
181
+
182
+ elif file_extension in ['apk', 'exe', 'dll']:
183
+ # Save uploaded file temporarily
184
+ file_path = f"./temp/{uploaded_file.name}"
185
+ os.makedirs(os.path.dirname(file_path), exist_ok=True)
186
+ with open(file_path, "wb") as f:
187
+ f.write(uploaded_file.getbuffer())
188
+
189
+ try:
190
+ with open(file_path, "rb") as file:
191
+ file_hash = get_file_hash(file)
192
+ metadata = extract_metadata(file)
193
+ virus_total_results = virustotal_analysis(file_hash)
194
+
195
+ finally:
196
+ # Clean up
197
+ os.remove(file_path)
198
+
199
+ log_analysis = None
200
+
201
+ else:
202
+ st.error("Unsupported file type.")
203
+ metadata = None
204
+ virus_total_results = None
205
+ log_analysis = None
206
+
207
+ display_analysis_results(metadata, virus_total_results, log_analysis)
208
+
209
+ # Initialize session state for page navigation
210
+ if 'page' not in st.session_state:
211
+ st.session_state.page = "main"
212
+
213
+ # Routing based on page state
214
+ if st.session_state.page == "main":
215
+ main_page()
216
+ elif st.session_state.page == "file_analysis":
217
+ file_analysis_page()
218
+
219
+
220
+
221
+
222
+
223
+
224
+
225
+ def analyze_log_file(log_content):
226
+ # Data storage structures for IPs, Domains, Headers, Sessions
227
+ ip_data = []
228
+ domain_data = []
229
+ header_data = []
230
+ session_data = []
231
+
232
+ # Regular expressions for matching
233
+ ip_regex = re.compile(r'\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b')
234
+ domain_regex = re.compile(r'\b[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b')
235
+ header_regex = re.compile(r'(User-Agent|Content-Type|Authorization):\s*(.*)', re.IGNORECASE)
236
+ session_regex = re.compile(r'SessionID:\s*([a-zA-Z0-9]+)')
237
+
238
+ log_entries = []
239
+
240
+ for line in log_content.splitlines():
241
+ # Match IPs
242
+ ips = ip_regex.findall(line)
243
+ if ips:
244
+ ip_data.extend(ips)
245
+
246
+ # Match Domains
247
+ domains = domain_regex.findall(line)
248
+ if domains:
249
+ domain_data.extend(domains)
250
+
251
+ # Match Headers
252
+ headers = header_regex.findall(line)
253
+ if headers:
254
+ header_data.extend(headers)
255
+
256
+ # Match Sessions
257
+ sessions = session_regex.findall(line)
258
+ if sessions:
259
+ session_data.extend(sessions)
260
+
261
+ log_entries.append(line)
262
+
263
+ # Convert to DataFrame
264
+ log_df = pd.DataFrame(log_entries, columns=["Log Entries"])
265
+
266
+ # Additional DataFrames for captured data
267
+ ip_df = pd.DataFrame(ip_data, columns=["IP Addresses"])
268
+ domain_df = pd.DataFrame(domain_data, columns=["Domains"])
269
+ header_df = pd.DataFrame(header_data, columns=["Header Name", "Header Value"])
270
+ session_df = pd.DataFrame(session_data, columns=["Session IDs"])
271
+
272
+ # Summary of findings
273
+ summary = {
274
+ "log_dataframe": log_df,
275
+ "ip_dataframe": ip_df,
276
+ "domain_dataframe": domain_df,
277
+ "header_dataframe": header_df,
278
+ "session_dataframe": session_df
279
+ }
280
+
281
+ return summary
282
+
283
+ # Log Analysis Section
284
+ if log_analysis is not None:
285
+ st.write("### πŸ“ Log Analysis")
286
+
287
+ # First row: IP Addresses and Domains
288
+ col1, col2 = st.columns(2)
289
+
290
+ with col1:
291
+ st.write("**IP Addresses:**")
292
+ st.dataframe(log_analysis.get("ip_dataframe"))
293
+
294
+ with col2:
295
+ st.write("**Domains:**")
296
+ st.dataframe(log_analysis.get("domain_dataframe"))
297
+
298
+ # Second row: Log Entries, Session IDs, Headers
299
+ col3, col4, col5 = st.columns([2, 1, 1])
300
+
301
+ with col3:
302
+ st.write("**Log Entries:**")
303
+ st.dataframe(log_analysis.get("log_dataframe"))
304
+
305
+ with col4:
306
+ st.write("**Session IDs:**")
307
+ if not log_analysis.get("session_dataframe").empty:
308
+ st.dataframe(log_analysis.get("session_dataframe"))
309
+ else:
310
+ st.write("No session IDs found.")
311
+
312
+ with col5:
313
+ st.write("**Headers:**")
314
+ if not log_analysis.get("header_dataframe").empty:
315
+ st.dataframe(log_analysis.get("header_dataframe"))
316
+ else:
317
+ st.write("No headers found.")