From 7d9d4bfbe6293cb312875ef758b73ebd3396fa29 Mon Sep 17 00:00:00 2001
From: gayan <ger38@drexel.edu>
Date: Thu, 11 Apr 2024 13:36:35 -0500
Subject: [PATCH] Shodan commit-1
---
backend/.idea/misc.xml | 6 ++++
backend/db.sqlite3 | Bin 212992 -> 212992 bytes
backend/shodan_scan/__init__.py | 0
backend/shodan_scan/admin.py | 3 ++
backend/shodan_scan/apps.py | 6 ++++
backend/shodan_scan/migrations/__init__.py | 0
backend/shodan_scan/models.py | 3 ++
backend/shodan_scan/tests.py | 3 ++
backend/shodan_scan/urls.py | 7 ++++
backend/shodan_scan/utils.py | 37 +++++++++++++++++++++
backend/shodan_scan/views.py | 19 +++++++++++
backend/watchstone_backend/settings.py | 1 +
backend/watchstone_backend/urls.py | 1 +
13 files changed, 86 insertions(+)
create mode 100644 backend/shodan_scan/__init__.py
create mode 100644 backend/shodan_scan/admin.py
create mode 100644 backend/shodan_scan/apps.py
create mode 100644 backend/shodan_scan/migrations/__init__.py
create mode 100644 backend/shodan_scan/models.py
create mode 100644 backend/shodan_scan/tests.py
create mode 100644 backend/shodan_scan/urls.py
create mode 100644 backend/shodan_scan/utils.py
create mode 100644 backend/shodan_scan/views.py
diff --git a/backend/.idea/misc.xml b/backend/.idea/misc.xml
index 118839927..9fc0e11c5 100644
--- a/backend/.idea/misc.xml
+++ b/backend/.idea/misc.xml
@@ -1,4 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
+ <component name="Black">
+ <option name="sdkName" value="Python 3.12 (backend)" />
+ </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (backend)" project-jdk-type="Python SDK" />
+ <component name="PyCharmProfessionalAdvertiser">
+ <option name="shown" value="true" />
+ </component>
</project>
\ No newline at end of file
diff --git a/backend/db.sqlite3 b/backend/db.sqlite3
index bec0bc4efd28705af71be67f45d4ca5a2fcd0879..2594849a915f05ef5472c2475ef5937c13cae8d9 100644
GIT binary patch
delta 252
zcmZo@;B9E&ogmG~F;T{um4iXAtaD>Zm_8eyI5V>`<K%-p!kaJa8ybjlaz16?iQ&)W
zyTB*U+s%EPTZpTM^XX<oh0~nOjUs%LbMlqyP2G+2!_$*8j4H~5iVPwP6C=F5z0*~U
z3L*+4jMI&(GV}6Xd_zKtgYpbQi#@#xLj7}n+|vWxOcI^_!z=T03_@+wixNvR(o>6!
zEgVvcQY%t(^ioqwjSP%TbPY^&4Gk3x&8$q!txQez%*>4pEDalld3hNcZ5cSkd9^t<
zcV<<y@^SJ{W8lBazkvTS|2O_=n*|D5_$M!@XJG@I%-6i2zI{PG<MsvhObG`7)Z$HL
delta 109
zcmZo@;B9E&ogmG~K2gS*m7PH^Z|BC8Fnu;&ab{*?#>oeHgg0N-H#898;HqKZiQ&)W
zyTB*U+s%EPTZpS>v!Ox?*XEk64ptEk{sj#DSNRw4Kj#0&zl#4Y{|^3xn*|#>_?s8h
Pw=bw?+`gcmDd7MBhz25+
diff --git a/backend/shodan_scan/__init__.py b/backend/shodan_scan/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/backend/shodan_scan/admin.py b/backend/shodan_scan/admin.py
new file mode 100644
index 000000000..8c38f3f3d
--- /dev/null
+++ b/backend/shodan_scan/admin.py
@@ -0,0 +1,3 @@
+from django.contrib import admin
+
+# Register your models here.
diff --git a/backend/shodan_scan/apps.py b/backend/shodan_scan/apps.py
new file mode 100644
index 000000000..5072ba8d6
--- /dev/null
+++ b/backend/shodan_scan/apps.py
@@ -0,0 +1,6 @@
+from django.apps import AppConfig
+
+
+class ShodanScanConfig(AppConfig):
+ default_auto_field = 'django.db.models.BigAutoField'
+ name = 'shodan_scan'
diff --git a/backend/shodan_scan/migrations/__init__.py b/backend/shodan_scan/migrations/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/backend/shodan_scan/models.py b/backend/shodan_scan/models.py
new file mode 100644
index 000000000..71a836239
--- /dev/null
+++ b/backend/shodan_scan/models.py
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
diff --git a/backend/shodan_scan/tests.py b/backend/shodan_scan/tests.py
new file mode 100644
index 000000000..7ce503c2d
--- /dev/null
+++ b/backend/shodan_scan/tests.py
@@ -0,0 +1,3 @@
+from django.test import TestCase
+
+# Create your tests here.
diff --git a/backend/shodan_scan/urls.py b/backend/shodan_scan/urls.py
new file mode 100644
index 000000000..e41295118
--- /dev/null
+++ b/backend/shodan_scan/urls.py
@@ -0,0 +1,7 @@
+
+from django.urls import path
+from .views import ShodanScanView
+
+urlpatterns = [
+ path('shodan-scan/', ShodanScanView.as_view(), name='shodan_scan'),
+]
\ No newline at end of file
diff --git a/backend/shodan_scan/utils.py b/backend/shodan_scan/utils.py
new file mode 100644
index 000000000..cd3cd2315
--- /dev/null
+++ b/backend/shodan_scan/utils.py
@@ -0,0 +1,37 @@
+import shodan
+
+
+def shodan_scan(api_key, ips):
+
+ api = shodan.Shodan(api_key)
+ scan_results = []
+
+ for ip in ips:
+ try:
+ # Host information
+ host_info = api.host(ip)
+
+ # Append scan results to the list
+ scan_results.append({
+ 'ip_address': ip,
+ 'open_ports': [{
+ 'port': port_info['port'],
+ 'protocol': port_info['_shodan']['module'],
+
+ 'banner': port_info.get('banner', None)
+ } for port_info in host_info['data']],
+ 'geolocation': {
+ 'country': host_info.get('country_name', None),
+ 'city': host_info.get('city', None),
+ 'latitude': host_info.get('latitude', None),
+ 'longitude': host_info.get('longitude', None)
+ },
+ 'vulnerabilities': [{
+ 'vuln': host_info['vulns'][vuln]['summary']
+ } for vuln in host_info.get('vulns', {})]
+ })
+
+ except shodan.APIError as e:
+ print(f"Error: {e}")
+
+ return scan_results
\ No newline at end of file
diff --git a/backend/shodan_scan/views.py b/backend/shodan_scan/views.py
new file mode 100644
index 000000000..721f14ec6
--- /dev/null
+++ b/backend/shodan_scan/views.py
@@ -0,0 +1,19 @@
+from django.http import JsonResponse
+from django.views.decorators.csrf import csrf_exempt
+from rest_framework.views import APIView
+from .utils import shodan_scan
+from rest_framework.permissions import AllowAny
+
+
+class ShodanScanView(APIView):
+ permission_classes = [AllowAny]
+
+ @csrf_exempt
+ def post(self, request, format=None):
+ api_key = '39NV6HlSBtbQCmp0oKOmHZp51pdC99CA'
+ ip_list = request.data.get('ips', [])
+ scan_results = shodan_scan(api_key, ip_list)
+ return JsonResponse({'scan_results': scan_results}, safe=False)
+
+
+
diff --git a/backend/watchstone_backend/settings.py b/backend/watchstone_backend/settings.py
index aec931a37..4e8508250 100644
--- a/backend/watchstone_backend/settings.py
+++ b/backend/watchstone_backend/settings.py
@@ -34,6 +34,7 @@ INSTALLED_APPS = [
"rest_framework",
"rest_framework_simplejwt",
"breach_check",
+ "shodan_scan",
]
diff --git a/backend/watchstone_backend/urls.py b/backend/watchstone_backend/urls.py
index b37fe668a..eb7d3fbc5 100644
--- a/backend/watchstone_backend/urls.py
+++ b/backend/watchstone_backend/urls.py
@@ -11,5 +11,6 @@ urlpatterns = [
path('api/breaches/', include('breach_check.urls')), # Include breaches app URLs
path('haveibeenpwned/<str:identifier>/', have_i_been_pwned, name='have_i_been_pwned'),
path('haveibeenpwned/passwords/<str:identifier>/', breached_password_info, name='breached_password_info'),
+ path('', include('shodan_scan.urls')),
# Add other URL patterns as needed
]
--
GitLab