-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
118 lines (85 loc) · 3.34 KB
/
test.py
File metadata and controls
118 lines (85 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import uvicorn
from fastapi import FastAPI, Request, HTTPException, File, UploadFile
import joblib, os
from pydantic import BaseModel
from io import BytesIO
import zipfile
app = FastAPI()
# pkl
phish_model = open('phishing1.pkl', 'rb')
phish_model_ls = joblib.load(phish_model)
modelMail = joblib.load('spam_model.pkl')
class InputData(BaseModel):
email: str
# Placeholder database of URL categories (this would ideally come from an external service)
url_categories = {
"google.com": "Search Engine",
"bing.com": "Search Engine",
"facebook.com": "Social Media",
"instagram.com": "Social Media",
"flipkart.com": "Shopping",
"amazon.com": "Shopping",
"snapdeal.com": "Shopping",
# Add more URLs and their respective categories as needed
}
# ML Aspect
@app.get('/predict/{feature}')
async def predict(request: Request, features: str):
url_length = len(features)
has_https = features.startswith("https://")
risk_score = 0.5 * url_length + 0.3 if has_https else 0.8
# Extracting Client IP
client_ip = request.client.host
# Extracting Timestamp
from datetime import datetime
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Simulating URL categorization (using the placeholder database)
url_category = url_categories.get(features, "Uncategorized")
X_predict = []
X_predict.append(str(features))
y_Predict = phish_model_ls.predict(X_predict)
if y_Predict == 'bad':
result = "True"
else:
result = "False"
return features, result, risk_score, client_ip, timestamp, url_category
# ---- Email Spam -----
@app.post('/predictmail')
def predict_mail(data: InputData):
try:
prediction = modelMail.predict([data.email]) # Pass a list with a single email
return {"prediction": prediction.tolist()[0]} # Assuming your model returns a single prediction
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
# ------- Malware -------
def extract_manifest_info(apk_content):
try:
with zipfile.ZipFile(BytesIO(apk_content), 'r') as zip_ref:
manifest_data = zip_ref.read('AndroidManifest.xml')
return manifest_data.decode('latin-1')
except Exception as e:
print(f"Error extracting manifest info: {str(e)}")
raise
def predict_malware(manifest_info):
try:
# Your logic to process the manifest_info and predict malware here
# This is a placeholder, replace it with your actual malware prediction logic
if "malicious_pattern" in manifest_info:
return "Malware found"
else:
return "Safe"
except Exception as e:
print(f"Error predicting malware: {str(e)}")
raise
@app.post("/predict_malware/")
async def predict_malware_endpoint(file: UploadFile = File(...)):
try:
content = await file.read()
manifest_info = extract_manifest_info(content)
prediction = predict_malware(manifest_info)
return {"filename": file.filename, "prediction": prediction}
except Exception as e:
print(f"Error processing APK file: {str(e)}")
return {"error": str(e)}
if __name__ == '__main__':
uvicorn.run(app, host="192.168.1.50", port=8000)