# Buggy code
def get_vnc_screenshots(target, scan_id, proctimeout):
    scan_dir = utils.get_scan_dir(scan_id)
    outFile = os.path.join(scan_dir, f"vncsnapshot.{scan_id}.jpg")

    logger.info(f"Attempting to take VNC screenshot for {target}")

    process = subprocess.Popen(
        ["xvfb-run", "vncsnapshot", "-quality", "50", target, outFile],
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
    )  # nosec
    try:
        process.communicate(timeout=proctimeout)
        if process.returncode == 0:
            return True
    except Exception:
        try:
            logger.warning(f"TIMEOUT: Killing vncsnapshot against {target}")
            process.kill()
            return False
        except Exception:
            pass

    return False


# Traceback (PIL.UnidentifiedImageError)
[2020-07-25 00:22:59,784] ERROR in app: Exception on /api/submit [POST]
Traceback (most recent call last):
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/.venv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/.venv/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/opt/natlas/natlas-server/app/auth/wrappers.py", line 49, in decorated_function
return f(*args, **kwargs)
File "/opt/natlas/natlas-server/app/api/routes.py", line 187, in submit
newhost["screenshots"], newhost["num_screenshots"] = process_screenshots(
File "/opt/natlas/natlas-server/app/api/processing/screenshot.py", line 52, in process_screenshots
item["thumb_hash"] = create_thumbnail(fname, file_ext)
File "/opt/natlas/natlas-server/app/api/processing/screenshot.py", line 10, in create_thumbnail
thumb = Image.open(fname)
File "/.venv/lib/python3.8/site-packages/PIL/Image.py", line 2895, in open
raise UnidentifiedImageError(
PIL.UnidentifiedImageError: cannot identify image file '/data/media/original/e3/b0/e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.png'

# Fixed code
def get_vnc_screenshots(target, scan_id, proctimeout):
    scan_dir = utils.get_scan_dir(scan_id)
    output_file = os.path.join(scan_dir, f"vncsnapshot.{scan_id}.jpg")

    logger.info(f"Attempting to take VNC screenshot for {target}")

    vncsnapshot_args = [
        "xvfb-run",
        "vncsnapshot",
        "-quality",
        "50",
        target,
        output_file,
    ]

    process = subprocess.Popen(
        vncsnapshot_args, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
    )  # nosec
    try:
        process.communicate(timeout=proctimeout)
    except subprocess.TimeoutExpired:
        logger.warning(f"TIMEOUT: Killing vncsnapshot against {target}")
        process.kill()

    if not is_valid_image(output_file):
        return {}

    logger.info(f"VNC screenshot acquired for {target} on port 5900")
    return {
        "host": target,
        "port": 5900,
        "service": "VNC",
        "data": base64_file(output_file),
    }