Skip to content

Commit f34db8c

Browse files
author
Pandora
committed
Check if the given sample has already been analyzed before uploading it.
1 parent ab51a1b commit f34db8c

6 files changed

Lines changed: 94 additions & 8 deletions

File tree

bazaar/core/api_view.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
from django.conf import settings
44
from django.core.files.storage import default_storage
5+
from django.http import JsonResponse
6+
from rest_framework.reverse import reverse_lazy
57
from elasticsearch import Elasticsearch
68
from rest_framework.authentication import TokenAuthentication
79
from rest_framework.decorators import api_view, authentication_classes, permission_classes, throttle_classes
8-
from rest_framework.permissions import IsAuthenticated
10+
from rest_framework.permissions import IsAuthenticated, AllowAny
911
from rest_framework.response import Response
1012
from rest_framework.throttling import UserRateThrottle
1113

@@ -104,3 +106,28 @@ def search(request):
104106
return Response(results)
105107
except Exception as e:
106108
return Response([])
109+
110+
111+
@api_view(['GET'])
112+
@authentication_classes([])
113+
@permission_classes([AllowAny])
114+
def sample_exists(request, sha256):
115+
if not sha256:
116+
return Response({
117+
'ret_code': -1,
118+
'requested_hash': sha256,
119+
'message': 'Parameter sha256 not specified'
120+
})
121+
if default_storage.exists(sha256):
122+
return Response({
123+
'ret_code': 0,
124+
'requested_hash': sha256,
125+
'message': 'A sample with the same sha256 already exists',
126+
'report_url': reverse_lazy('front:report', [sha256]),
127+
})
128+
else:
129+
return Response({
130+
'ret_code': -2,
131+
'requested_hash': sha256,
132+
'message': 'Sample not found'
133+
})

bazaar/core/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
path("upload", apk_upload, name="bazaar-api"),
99
path("report/<str:sha256>", apk_analysis_report, name="bazaar-api"),
1010
path("status/<str:sha256>", analysis_tasks_status, name="bazaar-api"),
11+
path("exists/<str:sha256>", sample_exists, name="bazaar-api"),
1112
path("search/", search, name="bazaar-api"),
1213
]

bazaar/static/js/jquery-3.3.1.slim.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bazaar/static/js/project.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,57 @@ $(function () {
66
$(this).tab('show')
77
})
88
$('#menu-tabs a[href="#fingerprints"]').tab('show')
9+
$('#file_upload_selector').change(function () {
10+
const upload_btn = $('#upload_btn')
11+
const show_report_btn = $('#show_report_btn')
12+
upload_btn.hide()
13+
show_report_btn.hide()
14+
hashfile(this, upload_btn, show_report_btn)
15+
})
916
})
1017

18+
19+
function hashfile(file_selector, upload_btn, report_btn) {
20+
return readbinaryfile(file_selector.files[0])
21+
.then(function (result) {
22+
result = new Uint8Array(result);
23+
return window.crypto.subtle.digest('SHA-256', result);
24+
}).then(function (result) {
25+
result = new Uint8Array(result);
26+
const hash = Uint8ArrayToHexString(result);
27+
$.ajax(`/api/exists/${hash}`).done(function (data){
28+
if(data.ret_code == 0){
29+
report_btn.attr('href', data.report_url)
30+
report_btn.show()
31+
} else {
32+
upload_btn.show()
33+
}
34+
})
35+
});
36+
}
37+
38+
function readbinaryfile(file) {
39+
return new Promise((resolve, reject) => {
40+
var fr = new FileReader();
41+
fr.onload = () => {
42+
resolve(fr.result)
43+
};
44+
fr.readAsArrayBuffer(file);
45+
});
46+
}
47+
48+
function Uint8ArrayToHexString(ui8array) {
49+
var hexstring = '',
50+
h;
51+
for (var i = 0; i < ui8array.length; i++) {
52+
h = ui8array[i].toString(16);
53+
if (h.length == 1) {
54+
h = '0' + h;
55+
}
56+
hexstring += h;
57+
}
58+
var p = Math.pow(2, Math.ceil(Math.log2(hexstring.length)));
59+
hexstring = hexstring.padStart(p, '0');
60+
return hexstring;
61+
}
62+

bazaar/templates/base.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@
125125
<script src="{% static 'js/clipboard.js' %}"></script>
126126
{% endcompress %}
127127
{% endblock javascript %}
128+
{% block inline_js %}
129+
{% endblock inline_js %}
128130
</body>
129131

130132
</html>

bazaar/templates/front/index.html

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,29 @@ <h3 class=" text-white">Mobile threat intelligence for the masses</h3>
4343
<form class="form-horizontal text-center" method="post" action="{% url "front:basic_upload" %}"
4444
enctype="multipart/form-data">{% csrf_token %}
4545
<div class="custom-file ">
46-
<input type="file" class="custom-file-input" id="customFile" name="apk">
47-
<label class="custom-file-label" for="customFile">APK to be analyzed ({{ max_size|filesizeformat }}
46+
<input type="file" class="custom-file-input" id="file_upload_selector" oncchange="javascript:hashfile();" name="apk">
47+
<label class="custom-file-label" for="file_upload_selector">APK to be analyzed ({{ max_size|filesizeformat }}
4848
max.)</label>
4949
</div>
5050
<div class="control-group mt-1">
5151
<div class="controls">
52-
<button type="submit" class="btn btn-primary">
52+
<button type="submit" class="btn btn-primary" id="upload_btn" style="display: none">
5353
Upload
5454
</button>
55+
<a href="#" id="show_report_btn" style="display: none" class="btn btn-primary">
56+
Show report
57+
</a>
5558
</div>
5659
</div>
5760
</form>
5861
</div>
5962
</div>
6063

61-
64+
6265
<div class="row justify-content-center">
6366
<div class="col text-center">
64-
Try a real life example of Pithus on TryHackMe: <a href="https://tryhackme.com/room/androidmalwareanalysis">Android Malware Analysis</a>
67+
Try a real life example of Pithus on TryHackMe: <a href="https://tryhackme.com/room/androidmalwareanalysis">Android
68+
Malware Analysis</a>
6569
</div>
6670
</div>
6771
<div class="row justify-content-center">

0 commit comments

Comments
 (0)