2022-06-11 18:25:10 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2022-07-07 19:59:14 +02:00
|
|
|
#
|
|
|
|
# This file is part of the Pandora-box distribution (https://github.com/dbarzin/pandora-box).
|
|
|
|
# Copyright (c) 2022 Didier Barzin.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, version 3.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but
|
|
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
2022-06-11 21:50:01 +02:00
|
|
|
|
2022-06-11 18:25:10 +02:00
|
|
|
import curses
|
2022-06-12 00:26:34 +02:00
|
|
|
from curses import wrapper
|
2022-06-11 18:25:10 +02:00
|
|
|
import pypandora
|
2022-06-11 21:06:59 +02:00
|
|
|
import time
|
2022-06-11 18:25:10 +02:00
|
|
|
import sys
|
2022-06-11 20:06:30 +02:00
|
|
|
import pyudev
|
|
|
|
import psutil
|
2022-06-11 21:06:59 +02:00
|
|
|
import os
|
2022-06-12 11:14:38 +02:00
|
|
|
import logging
|
2022-06-12 20:37:49 +02:00
|
|
|
import time
|
2022-06-16 13:24:36 +02:00
|
|
|
import configparser
|
2022-06-17 22:09:21 +02:00
|
|
|
import shutil
|
|
|
|
from datetime import datetime
|
2022-06-11 20:06:30 +02:00
|
|
|
|
2022-06-11 21:06:59 +02:00
|
|
|
# -----------------------------------------------------------
|
|
|
|
# Config variables
|
|
|
|
# -----------------------------------------------------------
|
2022-06-11 20:06:30 +02:00
|
|
|
|
2022-06-16 13:24:36 +02:00
|
|
|
USB_AUTO_MOUNT = False
|
2022-06-11 21:06:59 +02:00
|
|
|
PANDORA_ROOT_URL = "http://127.0.0.1:6100"
|
2022-06-16 13:24:36 +02:00
|
|
|
FAKE_SCAN = False
|
2022-06-17 22:09:21 +02:00
|
|
|
QUARANTINE = False
|
2022-06-28 17:52:32 +02:00
|
|
|
CURSES = True
|
2022-06-16 13:24:36 +02:00
|
|
|
|
|
|
|
""" read configuration file """
|
|
|
|
def config():
|
2022-06-17 23:30:37 +02:00
|
|
|
global USB_AUTO_MOUNT, PANDORA_ROOT_URL
|
2022-06-17 21:18:30 +02:00
|
|
|
global FAKE_SCAN, QUARANTINE, QUARANTINE_FOLDER
|
2022-07-12 16:42:00 +00:00
|
|
|
global CURSES
|
2022-06-16 13:24:36 +02:00
|
|
|
# intantiate a ConfirParser
|
|
|
|
config = configparser.ConfigParser()
|
|
|
|
# read the config file
|
2022-06-28 19:42:16 +00:00
|
|
|
config.read('pandora-box.ini')
|
2022-06-16 13:24:36 +02:00
|
|
|
# set values
|
2022-06-17 21:18:30 +02:00
|
|
|
FAKE_SCAN=config['DEFAULT']['FAKE_SCAN'].lower()=="true"
|
2022-06-16 17:32:11 +02:00
|
|
|
USB_AUTO_MOUNT=config['DEFAULT']['USB_AUTO_MOUNT'].lower()=="true"
|
2022-06-16 13:24:36 +02:00
|
|
|
PANDORA_ROOT_URL=config['DEFAULT']['PANDORA_ROOT_URL']
|
2022-06-17 21:18:30 +02:00
|
|
|
# Quarantine
|
2022-06-17 22:09:21 +02:00
|
|
|
QUARANTINE = config['DEFAULT']['QUARANTINE'].lower()=="true"
|
2022-06-17 21:18:30 +02:00
|
|
|
QUARANTINE_FOLDER = config['DEFAULT']['QUARANTINE_FOLDER']
|
2022-06-28 17:52:32 +02:00
|
|
|
# Curses
|
|
|
|
CURSES = config['DEFAULT']['CURSES'].lower()=="true"
|
2022-06-28 14:29:46 +02:00
|
|
|
|
2022-06-12 12:50:04 +02:00
|
|
|
# ----------------------------------------------------------
|
|
|
|
|
|
|
|
""" Convert size to human readble string """
|
|
|
|
def human_readable_size(size, decimal_places=1):
|
|
|
|
for unit in ['B','KB','MB','GB','TB']:
|
|
|
|
if size < 1024.0:
|
|
|
|
break
|
|
|
|
size /= 1024.0
|
|
|
|
return f"{size:.{decimal_places}f}{unit}"
|
2022-06-11 20:06:30 +02:00
|
|
|
|
2022-06-28 17:52:32 +02:00
|
|
|
|
|
|
|
# -----------------------------------------------------------
|
|
|
|
# Image Screen
|
|
|
|
# -----------------------------------------------------------
|
|
|
|
|
|
|
|
def display_image(status):
|
|
|
|
if status=="WAIT":
|
2022-06-28 19:42:16 +00:00
|
|
|
image = "images/pandora-box1.png"
|
2022-06-28 17:52:32 +02:00
|
|
|
elif status=="WORK":
|
2022-06-28 19:42:16 +00:00
|
|
|
image = "images/pandora-box2.png"
|
2022-06-28 17:52:32 +02:00
|
|
|
elif status=="OK":
|
2022-06-28 19:42:16 +00:00
|
|
|
image = "images/pandora-box3.png"
|
2022-06-28 17:52:32 +02:00
|
|
|
elif status=="BAD":
|
2022-06-28 19:42:16 +00:00
|
|
|
image = "images/pandora-box4.png"
|
2022-06-30 12:36:46 +00:00
|
|
|
elif status=="ERROR":
|
|
|
|
image = "images/pandora-box5.png"
|
2022-06-28 19:42:16 +00:00
|
|
|
else:
|
2022-06-28 18:58:29 +02:00
|
|
|
return
|
2022-07-12 16:14:35 +00:00
|
|
|
os.system("killall fim 2>/dev/null")
|
|
|
|
os.system("fim -qa %s </dev/null 2>/dev/null &" % image)
|
2022-06-28 18:58:29 +02:00
|
|
|
|
2022-06-30 10:44:21 +02:00
|
|
|
# -----------------------------------------------------------
|
|
|
|
|
|
|
|
def waitMouseClick():
|
|
|
|
mouse = open( "/dev/input/mice", "rb" )
|
|
|
|
while True:
|
|
|
|
buf = mouse.read(3)
|
|
|
|
if ((buf[0] & 0x1)==1):
|
|
|
|
break;
|
|
|
|
mouse.close()
|
|
|
|
|
2022-06-11 21:06:59 +02:00
|
|
|
# -----------------------------------------------------------
|
2022-06-28 17:52:32 +02:00
|
|
|
# CURSES Screen
|
2022-06-11 21:06:59 +02:00
|
|
|
# -----------------------------------------------------------
|
|
|
|
|
2022-06-11 21:50:01 +02:00
|
|
|
"""Initialise curses"""
|
2022-06-28 19:42:16 +00:00
|
|
|
def init_curses():
|
2022-06-11 21:06:59 +02:00
|
|
|
global screen
|
2022-06-28 18:58:29 +02:00
|
|
|
if CURSES:
|
2022-06-30 10:44:21 +02:00
|
|
|
screen = curses.initscr()
|
|
|
|
screen.keypad(1)
|
|
|
|
curses.mousemask(curses.ALL_MOUSE_EVENTS | curses.REPORT_MOUSE_POSITION)
|
|
|
|
curses.flushinp()
|
|
|
|
curses.noecho()
|
2022-06-28 18:58:29 +02:00
|
|
|
curses.curs_set(0)
|
2022-06-28 19:42:16 +00:00
|
|
|
else:
|
2022-06-28 18:58:29 +02:00
|
|
|
display_image("WAIT")
|
2022-06-11 21:06:59 +02:00
|
|
|
|
2022-06-11 21:50:01 +02:00
|
|
|
"""Print FS Label"""
|
2022-06-12 00:26:34 +02:00
|
|
|
def print_fslabel(label):
|
|
|
|
global status_win
|
2022-06-28 18:58:29 +02:00
|
|
|
if CURSES:
|
|
|
|
status_win.addstr(1, 1, "Partition : %-32s" % label, curses.color_pair(2))
|
|
|
|
status_win.refresh()
|
2022-06-12 00:26:34 +02:00
|
|
|
|
|
|
|
"""Print FS Size"""
|
|
|
|
def print_size(label):
|
|
|
|
global status_win
|
2022-06-28 18:58:29 +02:00
|
|
|
if CURSES:
|
|
|
|
if label == None:
|
|
|
|
status_win.addstr(2, 1, "Size : ",curses.color_pair(2))
|
|
|
|
else:
|
|
|
|
status_win.addstr(2, 1, "Size : %s " % label,curses.color_pair(2))
|
|
|
|
logging.info("Size: %s" % label)
|
|
|
|
status_win.refresh()
|
2022-06-12 00:26:34 +02:00
|
|
|
|
|
|
|
"""Print FS Used Size"""
|
|
|
|
def print_used(label):
|
|
|
|
global status_win
|
2022-06-28 18:58:29 +02:00
|
|
|
if CURSES:
|
|
|
|
if label == None:
|
|
|
|
status_win.addstr(3, 1, "Used : ",curses.color_pair(2))
|
|
|
|
else:
|
|
|
|
status_win.addstr(3, 1, "Used : %s " % label,curses.color_pair(2))
|
|
|
|
logging.info("Used: %s" % label)
|
|
|
|
status_win.refresh()
|
2022-06-11 21:06:59 +02:00
|
|
|
|
2022-06-12 00:26:34 +02:00
|
|
|
def print_fstype(label):
|
|
|
|
global status_win
|
2022-06-28 18:58:29 +02:00
|
|
|
if CURSES:
|
|
|
|
status_win.addstr(1, 50, "Part / Type : %-32s" % label, curses.color_pair(2))
|
|
|
|
status_win.refresh()
|
2022-06-11 20:06:30 +02:00
|
|
|
|
2022-06-12 00:26:34 +02:00
|
|
|
def print_model(label):
|
|
|
|
global status_win
|
2022-06-28 18:58:29 +02:00
|
|
|
if CURSES:
|
|
|
|
status_win.addstr(2, 50, "Model : %-32s" % label, curses.color_pair(2))
|
|
|
|
status_win.refresh()
|
2022-06-11 21:06:59 +02:00
|
|
|
|
2022-06-12 00:26:34 +02:00
|
|
|
def print_serial(label):
|
|
|
|
global status_win
|
2022-06-28 18:58:29 +02:00
|
|
|
if CURSES:
|
|
|
|
status_win.addstr(3, 50, "Serial : %-32s" % label, curses.color_pair(2))
|
|
|
|
status_win.refresh()
|
2022-06-11 20:06:30 +02:00
|
|
|
|
2022-06-11 21:50:01 +02:00
|
|
|
"""Initialise progress bar"""
|
|
|
|
def init_bar():
|
2022-06-11 20:06:30 +02:00
|
|
|
global progress_win
|
2022-06-28 18:58:29 +02:00
|
|
|
if CURSES:
|
|
|
|
progress_win = curses.newwin(3, curses.COLS-12, 17, 5)
|
|
|
|
progress_win.border(0)
|
|
|
|
progress_win.refresh()
|
2022-06-11 21:06:59 +02:00
|
|
|
|
2022-06-11 21:50:01 +02:00
|
|
|
"""Update progress bar"""
|
|
|
|
def update_bar(progress):
|
2022-06-11 20:06:30 +02:00
|
|
|
global progress_win
|
2022-06-28 18:58:29 +02:00
|
|
|
if CURSES:
|
|
|
|
if progress == 0:
|
|
|
|
progress_win.clear()
|
|
|
|
progress_win.border(0)
|
|
|
|
time.sleep(0)
|
|
|
|
progress_win.addstr(0, 1, "Progress:")
|
|
|
|
else:
|
|
|
|
pos = ((curses.COLS-14) * progress) // 100
|
|
|
|
progress_win.addstr(1, 1, "#"*pos)
|
|
|
|
progress_win.addstr(0, 1, "Progress: %d%%" % progress)
|
|
|
|
progress_win.refresh()
|
2022-06-12 00:26:34 +02:00
|
|
|
|
2022-06-12 11:14:38 +02:00
|
|
|
"""Splash screen"""
|
|
|
|
s = [None] * 10;
|
|
|
|
s[0] = " ██▓███ ▄▄▄ ███▄ █ ▓█████▄ ▒█████ ██▀███ ▄▄▄ ▄▄▄▄ ▒█████ ▒██ ██▒"
|
|
|
|
s[1] = " ▓██░ ██▒▒████▄ ██ ▀█ █ ▒██▀ ██▌▒██▒ ██▒▓██ ▒ ██▒▒████▄ ▓█████▄ ▒██▒ ██▒▒▒ █ █ ▒░"
|
|
|
|
s[2] = " ▓██░ ██▓▒▒██ ▀█▄ ▓██ ▀█ ██▒░██ █▌▒██░ ██▒▓██ ░▄█ ▒▒██ ▀█▄ ▒██▒ ▄██▒██░ ██▒░░ █ ░"
|
|
|
|
s[3] = " ▒██▄█▓▒ ▒░██▄▄▄▄██ ▓██▒ ▐▌██▒░▓█▄ ▌▒██ ██░▒██▀▀█▄ ░██▄▄▄▄██ ▒██░█▀ ▒██ ██░ ░ █ █ ▒ "
|
|
|
|
s[4] = " ▒██▒ ░ ░ ▓█ ▓██▒▒██░ ▓██░░▒████▓ ░ ████▓▒░░██▓ ▒██▒ ▓█ ▓██▒ ░▓█ ▀█▓░ ████▓▒░▒██▒ ▒██▒"
|
|
|
|
s[5] = " ▒▓▒░ ░ ░ ▒▒ ▓▒█░░ ▒░ ▒ ▒ ▒▒▓ ▒ ░ ▒░▒░▒░ ░ ▒▓ ░▒▓░ ▒▒ ▓▒█░ ░▒▓███▀▒░ ▒░▒░▒░ ▒▒ ░ ░▓ ░"
|
|
|
|
s[6] = " ░▒ ░ ▒ ▒▒ ░░ ░░ ░ ▒░ ░ ▒ ▒ ░ ▒ ▒░ ░▒ ░ ▒░ ▒ ▒▒ ░ ▒░▒ ░ ░ ▒ ▒░ ░░ ░▒ ░"
|
|
|
|
s[7] = " ░░ ░ ▒ ░ ░ ░ ░ ░ ░ ░ ░ ░ ▒ ░░ ░ ░ ▒ ░ ░ ░ ░ ░ ▒ ░ ░ "
|
|
|
|
s[8] = " ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ "
|
|
|
|
s[9] = " ░ ░ "
|
2022-06-11 21:06:59 +02:00
|
|
|
|
2022-06-16 13:58:07 +02:00
|
|
|
#curses.LINES, curses.COLS
|
2022-06-16 13:24:36 +02:00
|
|
|
|
2022-06-12 00:26:34 +02:00
|
|
|
"""Print main screen"""
|
2022-06-11 21:50:01 +02:00
|
|
|
def print_screen():
|
2022-06-12 00:26:34 +02:00
|
|
|
global status_win
|
2022-06-28 18:58:29 +02:00
|
|
|
if CURSES:
|
|
|
|
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
|
|
|
|
curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK)
|
|
|
|
curses.init_pair(3, curses.COLOR_GREEN, curses.COLOR_BLACK)
|
|
|
|
title_win = curses.newwin(12, curses.COLS, 0, 0)
|
|
|
|
# title_win.border(0)
|
|
|
|
title_col = (curses.COLS - len(s[0]))//2
|
|
|
|
title_win.addstr(1, title_col, s[0], curses.color_pair(1))
|
|
|
|
title_win.addstr(2, title_col, s[1], curses.color_pair(1))
|
|
|
|
title_win.addstr(3, title_col, s[2], curses.color_pair(1))
|
|
|
|
title_win.addstr(4, title_col, s[3], curses.color_pair(1))
|
|
|
|
title_win.addstr(5, title_col, s[4], curses.color_pair(1))
|
|
|
|
title_win.addstr(6, title_col, s[5], curses.color_pair(1))
|
|
|
|
title_win.addstr(7, title_col, s[6], curses.color_pair(1))
|
|
|
|
title_win.addstr(8, title_col, s[7], curses.color_pair(1))
|
|
|
|
title_win.addstr(9, title_col, s[8], curses.color_pair(1))
|
|
|
|
title_win.addstr(10, title_col, s[9], curses.color_pair(1))
|
|
|
|
title_win.refresh()
|
|
|
|
status_win = curses.newwin(5, curses.COLS, 12, 0)
|
|
|
|
status_win.border(0)
|
|
|
|
status_win.addstr(0, 1, "USB Key Information")
|
|
|
|
print_fslabel("")
|
|
|
|
print_size(None)
|
|
|
|
print_used(None)
|
|
|
|
print_fstype("")
|
|
|
|
print_model("")
|
|
|
|
print_serial("")
|
|
|
|
init_bar()
|
|
|
|
update_bar(0)
|
2022-06-12 00:26:34 +02:00
|
|
|
log('Ready.')
|
2022-06-11 18:25:10 +02:00
|
|
|
|
2022-06-12 00:26:34 +02:00
|
|
|
"""Closes curses"""
|
2022-06-11 21:50:01 +02:00
|
|
|
def end_curses():
|
2022-06-28 19:42:16 +00:00
|
|
|
curses.endwin()
|
|
|
|
curses.flushinp()
|
2022-06-11 21:06:59 +02:00
|
|
|
|
2022-06-28 14:29:46 +02:00
|
|
|
# -----------------------------------------------------------
|
|
|
|
# Logging windows
|
|
|
|
# -----------------------------------------------------------
|
|
|
|
|
|
|
|
def init_log():
|
|
|
|
global log_win, logging
|
2022-06-28 18:58:29 +02:00
|
|
|
if CURSES:
|
|
|
|
log_win = curses.newwin(curses.LINES-20, curses.COLS, 20, 0)
|
|
|
|
log_win.border(0)
|
2022-06-28 14:29:46 +02:00
|
|
|
logging.basicConfig(
|
2022-06-28 17:52:32 +02:00
|
|
|
filename='pandora-box.log',
|
2022-06-28 14:29:46 +02:00
|
|
|
level=logging.INFO,
|
|
|
|
format='%(asctime)s - %(message)s',
|
|
|
|
datefmt='%m/%d/%y %H:%M'
|
|
|
|
)
|
|
|
|
|
|
|
|
logs = []
|
|
|
|
def log(str):
|
|
|
|
global log_win, logging
|
|
|
|
logging.info(str)
|
2022-06-28 18:58:29 +02:00
|
|
|
if CURSES:
|
|
|
|
# display log on screen
|
|
|
|
logs.append(str)
|
|
|
|
if len(logs)>(curses.LINES-22):
|
|
|
|
logs.pop(0)
|
|
|
|
log_win.clear()
|
|
|
|
log_win.border(0)
|
|
|
|
for i in range(min(curses.LINES-22,len(logs))):
|
|
|
|
log_win.addstr(i+1,1,logs[i][:curses.COLS-2],curses.color_pair(3))
|
|
|
|
log_win.refresh()
|
2022-06-11 18:25:10 +02:00
|
|
|
|
2022-06-11 21:06:59 +02:00
|
|
|
# -----------------------------------------------------------
|
2022-06-12 00:26:34 +02:00
|
|
|
# Device
|
2022-06-11 21:06:59 +02:00
|
|
|
# -----------------------------------------------------------
|
|
|
|
|
2022-06-12 00:26:34 +02:00
|
|
|
"""Mount USB device"""
|
2022-06-11 21:50:01 +02:00
|
|
|
def mount_device(device):
|
2022-06-11 20:06:30 +02:00
|
|
|
if USB_AUTO_MOUNT:
|
2022-06-11 21:06:59 +02:00
|
|
|
found = False
|
|
|
|
loop = 0
|
|
|
|
while (not found) and (loop < 10):
|
|
|
|
# need to sleep before devide is mounted
|
2022-06-11 18:25:10 +02:00
|
|
|
time.sleep(1)
|
|
|
|
for partition in psutil.disk_partitions():
|
2022-06-11 21:06:59 +02:00
|
|
|
if partition.device == device.device_node:
|
2022-06-12 11:14:38 +02:00
|
|
|
log("Device mounted at {}".format(partition.mountpoint))
|
2022-06-11 21:06:59 +02:00
|
|
|
found = True
|
|
|
|
loop += 1
|
2022-06-12 00:26:34 +02:00
|
|
|
if loop < 10:
|
|
|
|
return partition.mountpoint
|
|
|
|
else:
|
2022-06-16 17:32:11 +02:00
|
|
|
return None
|
2022-06-11 18:25:10 +02:00
|
|
|
else:
|
2022-06-13 20:10:33 +00:00
|
|
|
res = os.system("pmount " + device.device_node + " /media/box")
|
|
|
|
found = False
|
|
|
|
loop = 0
|
|
|
|
while (not found) and (loop < 10):
|
|
|
|
time.sleep(1)
|
|
|
|
try:
|
|
|
|
statvfs=os.statvfs(mount_point)
|
|
|
|
except Exception as e :
|
|
|
|
loop +=1
|
|
|
|
continue
|
|
|
|
break;
|
2022-06-12 11:14:38 +02:00
|
|
|
log("Device mounted at /media/box")
|
2022-06-13 20:10:33 +00:00
|
|
|
return "/media/box"
|
2022-06-11 21:06:59 +02:00
|
|
|
|
2022-06-12 00:26:34 +02:00
|
|
|
"""Unmount USB device"""
|
2022-06-11 21:50:01 +02:00
|
|
|
def umount_device():
|
2022-06-11 21:06:59 +02:00
|
|
|
if not USB_AUTO_MOUNT:
|
2022-06-30 12:45:55 +00:00
|
|
|
if os.path.exists("/media/box"):
|
|
|
|
log("Unmounting device /media/box")
|
|
|
|
res = os.system("pumount /media/box")
|
2022-06-11 18:25:10 +02:00
|
|
|
|
2022-06-12 00:26:34 +02:00
|
|
|
"""Main device loop"""
|
2022-06-11 21:50:01 +02:00
|
|
|
def device_loop():
|
2022-06-30 12:36:46 +00:00
|
|
|
# First unmount remaining device
|
|
|
|
umount_device()
|
|
|
|
# Loop
|
2022-06-11 21:06:59 +02:00
|
|
|
context = pyudev.Context()
|
|
|
|
monitor = pyudev.Monitor.from_netlink(context)
|
|
|
|
monitor.filter_by("block")
|
2022-06-12 20:37:49 +02:00
|
|
|
try:
|
|
|
|
for device in iter(monitor.poll, None):
|
|
|
|
if device.get("ID_FS_USAGE") == "filesystem" and device.device_node[5:7] == "sd":
|
|
|
|
if device.action == "add":
|
|
|
|
log("Device inserted")
|
|
|
|
log_device_info(device)
|
2022-06-28 19:42:16 +00:00
|
|
|
if not CURSES:
|
2022-06-28 18:58:29 +02:00
|
|
|
display_image("WORK")
|
|
|
|
else:
|
|
|
|
# display device type
|
|
|
|
print_fslabel(device.get("ID_FS_LABEL"))
|
|
|
|
print_fstype(device.get("ID_PART_TABLE_TYPE") + " " + device.get("ID_FS_TYPE"))
|
|
|
|
print_model(device.get("ID_MODEL"))
|
|
|
|
print_serial(device.get("ID_SERIAL_SHORT"))
|
2022-06-12 20:37:49 +02:00
|
|
|
# Mount device
|
|
|
|
mount_point = mount_device(device)
|
2022-06-16 17:32:11 +02:00
|
|
|
if mount_point == None:
|
2022-06-17 23:30:37 +02:00
|
|
|
# no partition
|
2022-06-16 17:32:11 +02:00
|
|
|
continue
|
2022-06-12 20:37:49 +02:00
|
|
|
try:
|
|
|
|
statvfs=os.statvfs(mount_point)
|
|
|
|
except Exception as e :
|
2022-06-17 22:09:21 +02:00
|
|
|
log("Unexpected error: %s" % e)
|
2022-06-12 20:37:49 +02:00
|
|
|
continue
|
|
|
|
print_size(human_readable_size(statvfs.f_frsize * statvfs.f_blocks))
|
|
|
|
print_used(human_readable_size(statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree)))
|
2022-06-17 23:30:37 +02:00
|
|
|
|
|
|
|
# Scan files
|
2022-06-12 20:37:49 +02:00
|
|
|
log("Scan started...........")
|
2022-06-17 23:30:37 +02:00
|
|
|
infected_files = scan(mount_point, statvfs.f_frsize * (statvfs.f_blocks - statvfs.f_bfree))
|
|
|
|
|
|
|
|
# Clean files
|
|
|
|
if len(infected_files) > 0:
|
|
|
|
log('%d infected files found !' % len(infected_files))
|
2022-06-28 19:42:16 +00:00
|
|
|
if not CURSES:
|
2022-06-28 18:58:29 +02:00
|
|
|
display_image("BAD")
|
2022-06-30 10:44:21 +02:00
|
|
|
waitMouseClick()
|
2022-06-28 18:58:29 +02:00
|
|
|
else:
|
|
|
|
log('PRESS KEY TO CLEAN')
|
2022-06-30 10:44:21 +02:00
|
|
|
screen.getch()
|
2022-06-17 23:30:37 +02:00
|
|
|
# Remove infected files
|
|
|
|
for file in infected_files:
|
|
|
|
try :
|
|
|
|
os.remove(file)
|
|
|
|
log('%s removed' % file)
|
|
|
|
except Exception as e :
|
|
|
|
log("Unexpected error: %s" % str(e))
|
2022-06-28 19:42:16 +00:00
|
|
|
os.system("sync")
|
2022-06-17 23:30:37 +02:00
|
|
|
log("Clean done.")
|
2022-06-28 19:42:16 +00:00
|
|
|
if not CURSES:
|
|
|
|
display_image("OK")
|
2022-06-28 18:58:29 +02:00
|
|
|
else:
|
2022-06-28 19:42:16 +00:00
|
|
|
if not CURSES:
|
2022-06-28 18:58:29 +02:00
|
|
|
display_image("OK")
|
2022-07-12 16:49:29 +00:00
|
|
|
umount_device()
|
2022-06-12 20:37:49 +02:00
|
|
|
|
|
|
|
if device.action == "remove":
|
|
|
|
log("Device removed")
|
2022-06-28 19:42:16 +00:00
|
|
|
if not CURSES:
|
2022-06-28 18:58:29 +02:00
|
|
|
display_image("WAIT")
|
|
|
|
else:
|
|
|
|
print_fslabel("")
|
|
|
|
print_size(None)
|
|
|
|
print_used(None)
|
|
|
|
print_fstype("")
|
|
|
|
print_model("")
|
|
|
|
print_serial("")
|
|
|
|
update_bar(0)
|
2022-06-12 20:37:49 +02:00
|
|
|
except Exception as e:
|
2022-06-17 23:30:37 +02:00
|
|
|
log("Unexpected error: %s" % str(e) )
|
2022-06-12 20:37:49 +02:00
|
|
|
finally:
|
|
|
|
log("Done.")
|
2022-06-11 21:06:59 +02:00
|
|
|
|
|
|
|
|
2022-06-12 11:14:38 +02:00
|
|
|
def log_device_info(dev):
|
|
|
|
logging.info("Device name: %s" % dev.get("DEVNAME"))
|
|
|
|
logging.info("Path id: %s" % dev.get("ID_PATH"))
|
|
|
|
logging.info("Bus system: %s" % dev.get("ID_BUS"))
|
|
|
|
logging.info("USB driver: %s" % dev.get("ID_USB_DRIVER"))
|
|
|
|
logging.info("Device type: %s" % dev.get("DEVTYPE"))
|
|
|
|
logging.info("Device usage: %s" % dev.get("ID_FS_USAGE"))
|
|
|
|
logging.info("Partition type: %s" % dev.get("ID_PART_TABLE_TYPE"))
|
|
|
|
logging.info("FS type: %s" % dev.get("ID_FS_TYPE"))
|
|
|
|
logging.info("Partition label: %s" % dev.get("ID_FS_LABEL"))
|
|
|
|
# logging.info("FS: %s" % dev.get("ID_FS_SYSTEM_ID"))
|
|
|
|
logging.info("Device model: %s" % dev.get("ID_MODEL"))
|
|
|
|
# logging.info('Usage: %s' % dev.get("ID_FS_USAGE"))
|
|
|
|
logging.info('Model: %s' % dev.get("ID_MODEL_ID"))
|
|
|
|
logging.info('Serial short: %s' % dev.get("ID_SERIAL_SHORT"))
|
|
|
|
logging.info('Serial: %s' % dev.get("ID_SERIAL"))
|
|
|
|
# logging.info(os.stat(dev.get("DEVNAME")))
|
|
|
|
|
2022-06-11 21:06:59 +02:00
|
|
|
# -----------------------------------------------------------
|
2022-06-11 18:25:10 +02:00
|
|
|
# pandora
|
2022-06-11 21:06:59 +02:00
|
|
|
# -----------------------------------------------------------
|
|
|
|
|
2022-06-12 00:26:34 +02:00
|
|
|
"""Scan a mount point with Pandora"""
|
2022-06-12 12:50:04 +02:00
|
|
|
def scan(mount_point, used):
|
2022-06-12 20:37:49 +02:00
|
|
|
global infected_filed
|
2022-06-13 20:10:33 +00:00
|
|
|
infected_files = []
|
2022-06-12 12:50:04 +02:00
|
|
|
scanned = 0
|
2022-06-12 20:37:49 +02:00
|
|
|
file_count = 0
|
|
|
|
scan_start_time = time.time()
|
2022-06-17 22:09:21 +02:00
|
|
|
if QUARANTINE:
|
|
|
|
quanrantine_folder = os.path.join(QUARANTINE_FOLDER,datetime.now().strftime("%y%m%d-%H%M"))
|
2022-06-13 20:10:33 +00:00
|
|
|
if not FAKE_SCAN:
|
|
|
|
pandora = pypandora.PyPandora(root_url=PANDORA_ROOT_URL)
|
2022-06-30 12:36:46 +00:00
|
|
|
try:
|
|
|
|
for root, dirs, files in os.walk(mount_point):
|
|
|
|
for file in files:
|
2022-06-17 23:30:37 +02:00
|
|
|
status = None
|
2022-06-13 20:10:33 +00:00
|
|
|
full_path = os.path.join(root,file)
|
|
|
|
file_size = os.path.getsize(full_path)
|
2022-06-13 21:00:07 +00:00
|
|
|
# log("Check %s [%s]" % (file, human_readable_size(file_size)))
|
2022-06-13 20:10:33 +00:00
|
|
|
file_scan_start_time = time.time()
|
|
|
|
if FAKE_SCAN :
|
2022-06-12 20:37:49 +02:00
|
|
|
time.sleep(0.1)
|
2022-06-13 20:10:33 +00:00
|
|
|
status = "SKIPPED"
|
|
|
|
else:
|
|
|
|
if file_size > (1024*1024*1024):
|
|
|
|
status = "TOO BIG"
|
|
|
|
else:
|
|
|
|
res = pandora.submit_from_disk(full_path)
|
|
|
|
time.sleep(0.1)
|
2022-06-17 22:09:21 +02:00
|
|
|
loop = 0
|
2022-07-05 20:14:07 +02:00
|
|
|
while True and (loop < 960):
|
2022-06-13 20:10:33 +00:00
|
|
|
res = pandora.task_status(res["taskId"])
|
|
|
|
status = res["status"]
|
|
|
|
if status != "WAITING":
|
|
|
|
break
|
|
|
|
time.sleep(0.5)
|
2022-06-17 22:09:21 +02:00
|
|
|
loop += 1
|
2022-06-13 20:10:33 +00:00
|
|
|
file_scan_end_time = time.time()
|
2022-06-13 21:00:07 +00:00
|
|
|
log("Scan %s [%s] -> %s (%ds)" % (
|
|
|
|
file,
|
|
|
|
human_readable_size(file_size),
|
|
|
|
status,
|
2022-07-05 20:14:07 +02:00
|
|
|
(file_scan_end_time - file_scan_start_time)))
|
2022-06-13 20:10:33 +00:00
|
|
|
scanned += os.path.getsize(full_path)
|
|
|
|
file_count += 1
|
|
|
|
update_bar(scanned * 100 // used)
|
2022-06-17 23:30:37 +02:00
|
|
|
|
|
|
|
if status == "ALERT":
|
|
|
|
infected_files.append(full_path)
|
|
|
|
if QUARANTINE:
|
|
|
|
if not os.path.isdir(quanrantine_folder) :
|
|
|
|
os.mkdir(quanrantine_folder)
|
|
|
|
shutil.copyfile(full_path, os.path.join(quanrantine_folder,file))
|
2022-06-30 12:36:46 +00:00
|
|
|
except Exception as e :
|
|
|
|
log("Unexpected error: %s" % e)
|
2022-06-30 12:45:55 +00:00
|
|
|
log("Scan failed !")
|
2022-06-30 12:36:46 +00:00
|
|
|
if not CURSES:
|
|
|
|
display_image("ERROR")
|
|
|
|
raise
|
2022-06-13 20:10:33 +00:00
|
|
|
update_bar(100)
|
2022-06-17 22:09:21 +02:00
|
|
|
log("Scan done in %ds, %d files scanned, %d files infected" %
|
|
|
|
((time.time() - scan_start_time),file_count,len(infected_files)))
|
2022-06-17 23:30:37 +02:00
|
|
|
return infected_files
|
2022-06-11 18:25:10 +02:00
|
|
|
|
2022-06-11 21:06:59 +02:00
|
|
|
# --------------------------------------
|
2022-06-11 18:25:10 +02:00
|
|
|
|
2022-06-28 20:44:04 +00:00
|
|
|
def moveToScriptFolder():
|
|
|
|
abspath = os.path.abspath(__file__)
|
|
|
|
dname = os.path.dirname(abspath)
|
|
|
|
os.chdir(dname)
|
|
|
|
|
2022-06-12 00:26:34 +02:00
|
|
|
"""Main entry point"""
|
|
|
|
def main(stdscr):
|
2022-06-12 20:37:49 +02:00
|
|
|
try :
|
2022-06-28 20:44:04 +00:00
|
|
|
moveToScriptFolder()
|
2022-06-12 11:41:35 +02:00
|
|
|
init_log()
|
2022-06-16 13:24:36 +02:00
|
|
|
config()
|
2022-06-28 19:42:16 +00:00
|
|
|
init_curses()
|
2022-06-11 21:50:01 +02:00
|
|
|
print_screen()
|
2022-06-12 20:37:49 +02:00
|
|
|
while True:
|
|
|
|
device_loop()
|
|
|
|
except Exception as e :
|
2022-06-28 19:42:16 +00:00
|
|
|
end_curses()
|
2022-06-16 13:24:36 +02:00
|
|
|
print("Unexpected error: ", e)
|
2022-06-11 21:06:59 +02:00
|
|
|
finally:
|
2022-06-28 19:42:16 +00:00
|
|
|
end_curses()
|
2022-06-11 18:25:10 +02:00
|
|
|
|
2022-06-11 21:06:59 +02:00
|
|
|
# --------------------------------------
|
2022-06-11 18:25:10 +02:00
|
|
|
|
2022-06-11 21:06:59 +02:00
|
|
|
if __name__ == "__main__":
|
2022-06-12 00:26:34 +02:00
|
|
|
wrapper(main)
|
2022-06-28 18:58:29 +02:00
|
|
|
|