2019-04-23 11:38:56 +02:00
|
|
|
import json
|
|
|
|
from pyzbar import pyzbar
|
|
|
|
import cv2
|
|
|
|
import re
|
|
|
|
import binascii
|
|
|
|
import np
|
|
|
|
|
|
|
|
misperrors = {'error': 'Error'}
|
|
|
|
mispattributes = {'input': ['attachment'],
|
|
|
|
'output': ['url', 'btc']}
|
2024-08-12 11:23:10 +02:00
|
|
|
moduleinfo = {
|
|
|
|
'version': '0.1',
|
|
|
|
'author': 'Sascha Rommelfangen',
|
|
|
|
'description': 'Module to decode QR codes.',
|
|
|
|
'module-type': ['expansion', 'hover'],
|
|
|
|
'name': 'QR Code Decode',
|
|
|
|
'logo': '',
|
|
|
|
'requirements': ['cv2: The OpenCV python library.', 'pyzbar: Python library to read QR codes.'],
|
|
|
|
'features': 'The module reads the QR code and returns the related address, which can be an URL or a bitcoin address.',
|
|
|
|
'references': [],
|
|
|
|
'input': 'A QR code stored as attachment attribute.',
|
|
|
|
'output': 'The URL or bitcoin address the QR code is pointing to.',
|
|
|
|
}
|
2019-04-23 11:38:56 +02:00
|
|
|
|
|
|
|
debug = True
|
|
|
|
debug_prefix = "[DEBUG] QR Code module: "
|
|
|
|
# format example: bitcoin:1GXZ6v7FZzYBEnoRaG77SJxhu7QkvQmFuh?amount=0.15424
|
|
|
|
# format example: http://example.com
|
2019-04-23 15:40:22 +02:00
|
|
|
cryptocurrencies = ['bitcoin']
|
|
|
|
schemas = ['http://', 'https://', 'ftp://']
|
2019-04-23 11:38:56 +02:00
|
|
|
moduleconfig = []
|
|
|
|
|
2019-04-23 14:37:27 +02:00
|
|
|
|
2019-04-23 11:38:56 +02:00
|
|
|
def handler(q=False):
|
|
|
|
if q is False:
|
|
|
|
return False
|
|
|
|
q = json.loads(q)
|
|
|
|
filename = q['attachment']
|
|
|
|
try:
|
2023-07-01 15:13:52 +02:00
|
|
|
img_array = np.frombuffer(binascii.a2b_base64(q['data']), np.uint8)
|
2019-04-23 14:37:27 +02:00
|
|
|
except Exception as e:
|
2019-04-23 11:38:56 +02:00
|
|
|
err = "Couldn't fetch attachment (JSON 'data' is empty). Are you using the 'Query enrichment' action?"
|
|
|
|
misperrors['error'] = err
|
|
|
|
print(err)
|
2019-04-23 14:44:00 +02:00
|
|
|
print(e)
|
2019-04-23 11:38:56 +02:00
|
|
|
return misperrors
|
|
|
|
image = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
|
|
|
|
if q:
|
|
|
|
barcodes = pyzbar.decode(image)
|
|
|
|
for item in barcodes:
|
|
|
|
try:
|
|
|
|
result = item.data.decode()
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
return
|
|
|
|
if debug:
|
|
|
|
print(debug_prefix + result)
|
|
|
|
for item in cryptocurrencies:
|
|
|
|
if item in result:
|
|
|
|
try:
|
2019-04-23 14:44:00 +02:00
|
|
|
currency, address, extra = re.split(r'\:|\?', result)
|
2019-04-23 11:38:56 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
if currency in cryptocurrencies:
|
|
|
|
try:
|
|
|
|
amount = re.split('=', extra)[1]
|
|
|
|
if debug:
|
|
|
|
print(debug_prefix + address)
|
|
|
|
print(debug_prefix + amount)
|
|
|
|
return {'results': [{'types': ['btc'], 'values': address, 'comment': "BTC: " + amount + " from file " + filename}]}
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
else:
|
|
|
|
print(address)
|
2019-04-23 14:37:27 +02:00
|
|
|
for item in schemas:
|
2019-04-23 11:38:56 +02:00
|
|
|
if item in result:
|
|
|
|
try:
|
|
|
|
url = result
|
|
|
|
if debug:
|
|
|
|
print(debug_prefix + url)
|
|
|
|
return {'results': [{'types': ['url'], 'values': url, 'comment': "from QR code of file " + filename}]}
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
return {'results': [{'types': ['text'], 'values': result, 'comment': "from QR code of file " + filename}]}
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
misperrors['error'] = "Couldn't decode QR code in attachment."
|
|
|
|
return misperrors
|
|
|
|
|
2019-04-23 14:37:27 +02:00
|
|
|
|
2019-04-23 11:38:56 +02:00
|
|
|
def introspection():
|
|
|
|
return mispattributes
|
|
|
|
|
|
|
|
|
|
|
|
def version():
|
|
|
|
moduleinfo['config'] = moduleconfig
|
|
|
|
return moduleinfo
|