diff --git a/startScan/api/serializers.py b/startScan/api/serializers.py index 124f571d..101199fa 100644 --- a/startScan/api/serializers.py +++ b/startScan/api/serializers.py @@ -54,6 +54,7 @@ class TechnologyCountSerializer(serializers.Serializer): count = serializers.CharField() name = serializers.CharField() + class TechnologySerializer(serializers.ModelSerializer): class Meta: model = Technology diff --git a/startScan/api/urls.py b/startScan/api/urls.py index 1637d068..f1a77669 100644 --- a/startScan/api/urls.py +++ b/startScan/api/urls.py @@ -26,7 +26,9 @@ router.register(r'listIps', IpAddressViewSet) urlpatterns = [ url('^', include(router.urls)), - path('listTechnologies', ListTechnologies.as_view(), name='listTechnologies'), + path('listTechnologies/', ListTechnology.as_view(), name='listTechnologies'), + path('listPorts/', ListPorts.as_view(), name='listPorts'), + path('listIPs/', ListIPs.as_view(), name='listIPs'), ] urlpatterns += router.urls diff --git a/startScan/api/views.py b/startScan/api/views.py index b1ab2e48..4f96cb4b 100644 --- a/startScan/api/views.py +++ b/startScan/api/views.py @@ -17,20 +17,49 @@ from rest_framework.response import Response from rest_framework import status from rest_framework.decorators import api_view, action -class ListTechnologies(APIView): + +class ListTechnology(APIView): def get(self, request, format=None): req = self.request scan_id = req.query_params.get('scan_id') if scan_id: - tech = Technology.objects.filter(technologies__in=Subdomain.objects.filter(scan_history__id=scan_id)).annotate(count=Count('name')) + tech = Technology.objects.filter(technologies__in=Subdomain.objects.filter(scan_history__id=scan_id)).annotate(count=Count('name')).order_by('-count') serializer = TechnologyCountSerializer(tech, many=True) return Response({"technologies": serializer.data}) else: - all_tech = Technology.objects.all() - serializer = TechnologySerializer(all_tech, many=True) + tech = Technology.objects.filter(technologies__in=Subdomain.objects.all()).annotate(count=Count('name')).order_by('-count') + serializer = TechnologyCountSerializer(tech, many=True) return Response({"technologies": serializer.data}) +class ListPorts(APIView): + def get(self, request, format=None): + req = self.request + scan_id = req.query_params.get('scan_id') + if scan_id: + port = Port.objects.filter(ports__in=IpAddress.objects.filter(ip_addresses__in=Subdomain.objects.filter(scan_history__id=scan_id))).distinct() + serializer = PortSerializer(port, many=True) + return Response({"ports": serializer.data}) + else: + port = Port.objects.filter(ports__in=IpAddress.objects.filter(ip_addresses__in=Subdomain.objects.all())).distinct() + serializer = PortSerializer(port, many=True) + return Response({"ports": serializer.data}) + + +class ListIPs(APIView): + def get(self, request, format=None): + req = self.request + scan_id = req.query_params.get('scan_id') + if scan_id: + ips = IpAddress.objects.filter(ip_addresses__in=Subdomain.objects.filter(scan_history__id=scan_id)).distinct() + serializer = IpSerializer(ips, many=True) + return Response({"ips": serializer.data}) + else: + ips = IpAddress.objects.filter(ip_addresses__in=Subdomain.objects.all()).distinct() + serializer = IpSerializer(ips, many=True) + return Response({"ips": serializer.data}) + + class IpAddressViewSet(viewsets.ModelViewSet): queryset = Subdomain.objects.none() serializer_class = IpSubdomainSerializer diff --git a/startScan/migrations/0035_auto_20210609_1847.py b/startScan/migrations/0035_auto_20210609_1847.py new file mode 100644 index 00000000..495ab5dd --- /dev/null +++ b/startScan/migrations/0035_auto_20210609_1847.py @@ -0,0 +1,18 @@ +# Generated by Django 3.1.6 on 2021-06-09 18:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('startScan', '0034_auto_20210609_0406'), + ] + + operations = [ + migrations.AlterField( + model_name='ipaddress', + name='ports', + field=models.ManyToManyField(related_name='ports', to='startScan.Port'), + ), + ] diff --git a/startScan/models.py b/startScan/models.py index 8048a063..657b4422 100644 --- a/startScan/models.py +++ b/startScan/models.py @@ -231,7 +231,7 @@ class Technology(models.Model): class IpAddress(models.Model): address = models.CharField(max_length=100, blank=True, null=True) is_cdn = models.BooleanField(default=False) - ports = models.ManyToManyField('Port', related_name='port') + ports = models.ManyToManyField('Port', related_name='ports') def __str__(self): return str(self.address) diff --git a/startScan/templates/startScan/detail_scan.html b/startScan/templates/startScan/detail_scan.html index 866166c7..1ef0a457 100644 --- a/startScan/templates/startScan/detail_scan.html +++ b/startScan/templates/startScan/detail_scan.html @@ -261,18 +261,13 @@ Detailed Scan Results for {{history.domain.name}}