0

I made this code and works fine. The only thing is that when scp starts to send the file to my kaly linux, it ask me the password but it is already set in my code:

# importando as bibliotecas
import os
import paramiko

# criando o cliente ssh
client = paramiko.SSHClient()

# carregando as chaves de autenticação
client.load_system_host_keys()

# adicionando uma chave de autenticação
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

print('Enviando o arquivo de descriptografia')

# conectando ao servidor
client.connect('192.168.15.21', username='kali', password='kali')

# abrindo uma sessão
sess = client.get_transport().open_session()

# enviando o arquivo via scp
os.system('scp file.py kali@192.168.15.21:/home/kali')

# fechando a conexão
client.close()

Any idea how to resolve that? (dont ask the password in terminal cmd)

BloodHound
  • 15
  • 1
  • 6
  • 1
    `scp` doesn't know anything about the work you just did with paramiko; it's a completely separate program. If you want to send files via paramiko, you need to use paramiko to send them, not a scp program from OpenSSH or elsewhere. – Charles Duffy Dec 16 '22 at 21:48
  • 1
    (also, unless you have a very good reason, you should be using `sftp` rather than `scp`; sftp is a well-defined, standardized protocol, whereas scp is basically a poorly-documented historical hack) – Charles Duffy Dec 16 '22 at 21:49
  • 1
    (also, don't use `os.system` -- it introduces serious security problems. If, for example, someone gave your program that instead of being called `file.py` was called `$(rm -rf ~).py` to transfer, you don't want a local shell executing that command substitution; if you _were_ going to use an external tool instead of paramiko, you should be using `subprocess.run` with the default `shell=False` to invoke it). – Charles Duffy Dec 16 '22 at 21:51
  • thanks so much by your teachings, subprocess.run blocks rm -rf? – BloodHound Dec 16 '22 at 22:07
  • @CharlesDuffy yes sftp doesnt solves my problem, i need a protocol like scp but i dont want to pass a password, maybe create a id_rsa, idk,.... – BloodHound Dec 16 '22 at 22:11
  • 1
    Right, the `sftp` _program_ still has the same problem (because it's still a separate program!), but the sftp _paramiko functions_ will work over the transport you already have. Stop trying to use separate programs. Use functions provided by paramiko instead. – Charles Duffy Dec 16 '22 at 22:23
  • 1
    "subprocess.run blocks rm -rf" -- it doesn't "block" anything; it just avoids starting a shell, and in the `scp $(rm -rf ~).py` example, the `$( )` part is shell syntax; if there's no shell, there's nothing to try to execute it as a command. That is to say, if you run `subprocess.run(['program1', 'arg1', 'arg2'])`, only `program1` is treated a program's name, and `arg1` and `arg2` are only treated as arguments; the values in `arg1` and `arg2` can't start other programs unless `program1` decides to take that action. – Charles Duffy Dec 16 '22 at 22:25
  • 1
    You might start with https://docs.paramiko.org/en/stable/api/sftp.html -- or the duplicate question linked at the top of this page. – Charles Duffy Dec 16 '22 at 22:27
  • great! sftp from paramiko worked good and is encrypted :) if they didnt close the top i would give right answer :/ – BloodHound Dec 16 '22 at 22:32

0 Answers0