2024-10-01 13:12:15 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
The OcrExtractor Module
|
|
|
|
======================
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
##################################
|
|
|
|
# Import External packages
|
|
|
|
##################################
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2024-10-02 13:07:21 +00:00
|
|
|
import cv2
|
|
|
|
from pyzbar.pyzbar import decode
|
|
|
|
from qreader import QReader
|
|
|
|
|
2024-10-01 13:12:15 +00:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from modules.abstract_module import AbstractModule
|
|
|
|
from lib.ConfigLoader import ConfigLoader
|
2024-11-05 13:10:30 +00:00
|
|
|
from lib.objects import BarCodes
|
2024-10-01 13:12:15 +00:00
|
|
|
from lib.objects import QrCodes
|
|
|
|
|
|
|
|
|
2024-11-05 13:10:30 +00:00
|
|
|
class CodeReader(AbstractModule):
|
2024-10-01 13:12:15 +00:00
|
|
|
"""
|
|
|
|
QrCodeReader for AIL framework
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2024-11-05 13:10:30 +00:00
|
|
|
super(CodeReader, self).__init__()
|
2024-10-01 13:12:15 +00:00
|
|
|
|
|
|
|
# Waiting time in seconds between to message processed
|
|
|
|
self.pending_seconds = 1
|
|
|
|
|
|
|
|
config_loader = ConfigLoader()
|
|
|
|
self.r_cache = config_loader.get_redis_conn("Redis_Cache")
|
|
|
|
|
2024-11-05 13:10:30 +00:00
|
|
|
self.barcode_type = {'CODABAR', 'CODE39', 'CODE93', 'CODE128', 'EAN8', 'EAN13', 'I25'} # 2 - 5
|
|
|
|
|
2024-10-01 13:12:15 +00:00
|
|
|
# Send module state to logs
|
|
|
|
self.logger.info(f'Module {self.module_name} initialized')
|
|
|
|
|
|
|
|
def is_cached(self):
|
|
|
|
return self.r_cache.exists(f'qrcode:no:{self.obj.type}:{self.obj.id}')
|
|
|
|
|
|
|
|
def add_to_cache(self):
|
|
|
|
self.r_cache.setex(f'qrcode:no:{self.obj.type}:{self.obj.id}', 86400, 0)
|
|
|
|
|
2024-11-05 13:10:30 +00:00
|
|
|
def extract_codes(self, path):
|
|
|
|
barcodes = []
|
|
|
|
qrcodes = []
|
2024-10-02 13:07:21 +00:00
|
|
|
qr_codes = False
|
2024-10-02 13:30:29 +00:00
|
|
|
try:
|
|
|
|
image = cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB)
|
|
|
|
except cv2.error:
|
2024-10-02 13:35:04 +00:00
|
|
|
self.logger.warning(f'Invalid image: {self.obj.get_global_id()}')
|
2024-11-05 13:10:30 +00:00
|
|
|
return [], []
|
2024-10-02 13:30:29 +00:00
|
|
|
|
2024-10-01 13:12:15 +00:00
|
|
|
try:
|
2024-10-02 13:07:21 +00:00
|
|
|
decodeds = decode(image)
|
|
|
|
for decoded in decodeds:
|
2024-11-05 13:10:30 +00:00
|
|
|
# print(decoded)
|
2024-10-02 14:54:33 +00:00
|
|
|
if decoded.type == 'QRCODE':
|
|
|
|
qr_codes = True
|
|
|
|
if decoded.data:
|
2024-11-05 13:10:30 +00:00
|
|
|
qrcodes.append(decoded.data.decode())
|
|
|
|
elif decoded.type in self.barcode_type:
|
|
|
|
if decoded.data:
|
|
|
|
rect = decoded.rect
|
|
|
|
if rect.width and rect.height and decoded.quality > 1:
|
|
|
|
barcodes.append(decoded.data.decode())
|
|
|
|
elif decoded.type:
|
|
|
|
self.logger.error(f'Unsupported pyzbar code type {decoded.type}: {self.obj.get_global_id()}')
|
2024-10-02 13:07:21 +00:00
|
|
|
except ValueError as e:
|
|
|
|
self.logger.error(f'{e}: {self.obj.get_global_id()}')
|
|
|
|
|
2024-11-05 13:10:30 +00:00
|
|
|
if not qrcodes:
|
2024-10-02 13:07:21 +00:00
|
|
|
detector = cv2.QRCodeDetector()
|
2024-10-03 07:24:42 +00:00
|
|
|
try:
|
|
|
|
qr, decodeds, qarray, _ = detector.detectAndDecodeMulti(image)
|
|
|
|
if qr:
|
|
|
|
qr_codes = True
|
|
|
|
for d in decodeds:
|
|
|
|
if d:
|
2024-11-05 13:10:30 +00:00
|
|
|
qrcodes.append(d)
|
2024-10-03 07:24:42 +00:00
|
|
|
except cv2.error as e:
|
|
|
|
self.logger.error(f'{e}: {self.obj.get_global_id()}')
|
|
|
|
try:
|
|
|
|
data_qr, box, qrcode_image = detector.detectAndDecode(image)
|
|
|
|
if data_qr:
|
2024-11-05 13:10:30 +00:00
|
|
|
qrcodes.append(data_qr)
|
2024-10-03 07:24:42 +00:00
|
|
|
qr_codes = True
|
|
|
|
except cv2.error as e:
|
|
|
|
self.logger.error(f'{e}: {self.obj.get_global_id()}')
|
2024-10-02 13:07:21 +00:00
|
|
|
|
2024-11-05 13:10:30 +00:00
|
|
|
if qr_codes and not qrcodes:
|
2024-10-02 13:07:21 +00:00
|
|
|
# # # # 0.5s per image
|
|
|
|
try:
|
|
|
|
qreader = QReader()
|
|
|
|
decoded_text = qreader.detect_and_decode(image=image)
|
|
|
|
for d in decoded_text:
|
2024-11-05 13:10:30 +00:00
|
|
|
qrcodes.append(d)
|
2024-10-02 14:41:23 +00:00
|
|
|
qr_codes = True
|
2024-10-02 13:07:21 +00:00
|
|
|
except ValueError as e:
|
|
|
|
self.logger.error(f'{e}: {self.obj.get_global_id()}')
|
2024-11-05 13:10:30 +00:00
|
|
|
if not qr_codes:
|
|
|
|
self.logger.warning(f'Can notextract qr code: {self.obj.get_global_id()}')
|
|
|
|
|
|
|
|
return barcodes, qrcodes
|
2024-10-01 13:12:15 +00:00
|
|
|
|
|
|
|
def compute(self, message):
|
|
|
|
obj = self.get_obj()
|
|
|
|
|
|
|
|
if self.is_cached():
|
|
|
|
return None
|
|
|
|
|
|
|
|
if obj.type == 'image':
|
|
|
|
if self.obj.is_gif():
|
|
|
|
self.logger.warning(f'Ignoring GIF: {self.obj.id}')
|
|
|
|
return None
|
|
|
|
|
|
|
|
# image - screenshot
|
|
|
|
path = self.obj.get_filepath()
|
2024-11-05 13:10:30 +00:00
|
|
|
barcodes, qrcodes = self.extract_codes(path)
|
|
|
|
if not barcodes and not qrcodes:
|
2024-10-01 13:12:15 +00:00
|
|
|
self.add_to_cache()
|
|
|
|
return None
|
|
|
|
|
2024-11-05 13:10:30 +00:00
|
|
|
for content in qrcodes:
|
2024-10-02 15:20:27 +00:00
|
|
|
if not content:
|
|
|
|
continue
|
2024-10-01 13:12:15 +00:00
|
|
|
print(content)
|
|
|
|
qr_code = QrCodes.create(content, self.obj) # copy screenshot + image daterange
|
2024-10-01 13:37:05 +00:00
|
|
|
if not qr_code:
|
|
|
|
print('Error Empty content', self.obj.get_global_id())
|
2024-10-01 13:12:15 +00:00
|
|
|
qr_code.add(qr_code.get_date(), self.obj)
|
|
|
|
|
|
|
|
for obj_type in ['chat', 'domain', 'message']: # TODO ITEM ???
|
|
|
|
for c_id in self.obj.get_correlation(obj_type).get(obj_type, []):
|
|
|
|
o_subtype, o_id = c_id.split(':', 1)
|
|
|
|
qr_code.add_correlation(obj_type, o_subtype, o_id)
|
|
|
|
|
|
|
|
# TODO only if new ???
|
|
|
|
self.add_message_to_queue(obj=qr_code, queue='Item')
|
|
|
|
|
2024-11-05 13:10:30 +00:00
|
|
|
for content in barcodes:
|
|
|
|
if not content:
|
|
|
|
continue
|
|
|
|
print(content)
|
|
|
|
barcode = BarCodes.create(content, self.obj) # copy screenshot + image daterange
|
|
|
|
if not barcode:
|
|
|
|
print('Error Empty content', self.obj.get_global_id())
|
|
|
|
barcode.add(barcode.get_date(), self.obj)
|
|
|
|
|
|
|
|
for obj_type in ['chat', 'domain', 'message']: # TODO ITEM ???
|
|
|
|
for c_id in self.obj.get_correlation(obj_type).get(obj_type, []):
|
|
|
|
o_subtype, o_id = c_id.split(':', 1)
|
|
|
|
barcode.add_correlation(obj_type, o_subtype, o_id)
|
|
|
|
|
|
|
|
self.add_message_to_queue(obj=barcode, queue='Item')
|
|
|
|
|
|
|
|
if qrcodes:
|
2024-10-02 13:07:21 +00:00
|
|
|
tag = 'infoleak:automatic-detection="qrcode"'
|
|
|
|
self.add_message_to_queue(obj=self.obj, message=tag, queue='Tags')
|
|
|
|
|
2024-11-05 13:10:30 +00:00
|
|
|
if barcodes:
|
|
|
|
tag = 'infoleak:automatic-detection="barcode"'
|
|
|
|
self.add_message_to_queue(obj=self.obj, message=tag, queue='Tags')
|
|
|
|
|
2024-10-01 13:12:15 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2024-11-05 13:10:30 +00:00
|
|
|
module = CodeReader()
|
2024-10-01 13:12:15 +00:00
|
|
|
module.run()
|