python Trojan module (I forget a few) - the establishment of the communication tunnel

0X01 SSH to establish 

I think the first step is to achieve a simple ssh communication again, similar to that of other high-end C & C will gradually research

For ssh, python has a module called paramiko, looks like the Japanese word for yes

The introductions of some modules are as follow:

 

1, SSHClient class
SSH is advanced SHClient class service session, said package transmission, channel and parity SFTPClient established methods, commonly used to execute commands.
 
1) connect method
connect(self,hostname,port=22,username=None,password=None,pkey=None,key_filename=None,timeout=None,allow_agent=True,look_for_keys=True,compress=False)
Parameter Description:
hostname: the host address of the connection destination
port: port connected directory, defaults to 22
username: Username
password: password
pkey: user authentication private key way
key_filename: private key file name
timeout: Connection timeout time
allow_agent: whether to allow the use of ssh agent
look_for_keys: whether to allow private key file search
compress: whether to compress the open
 
2) exec_command method
exec_command(self,command,bufsize=-1)
Parameter Description:
command: command executed
bufsize: File the buffer size, is not limited -1
 
3) load_system_host_keys method
load_system_host_keys(self,filename=None)
Parameter Description:
filename: Specifies the remote host's public key file, the default for the next .ssh directory known_hosts file
 
4)set_missing_host_key_policy方法
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
Parameter Description:
AutoAddPolicy: automatically added to the local host name and key and saved, do not rely load_system_host_keys () configuration, that is, if there are no public known_hosts remote host, the default connection will be prompted yes / no, yes automatic
RejectPolicy: unknown host name and automatically reject key, dependent load_system_host_keys ()
WarnningPlicy: AutoAddPolicy same function, but unknown host will be prompted yes / no
 
2, SFTPClient class
According to sftp session SSH transfer protocol, remote file upload, download and other operations.
 
1) from_transport method
 
classmethod from_transport(cls,t)
Parameter Description:
t: a transport object is verified by
 
Example:
>>> import paramiko
>>> a = paramiko.Transport((“127.0.0.1″,2222))
>>> a.connect(username=”root”, password=’123456′)
>>> sftp = paramiko.SFTPClient.from_transport(a)
 
2) put method
 
put(self,localpath,remotepath,callback=None,confirm=True)
Parameter Description:
Local path to upload the source file: localpath
remotepath: target path
callback: Get the total number of bytes transferred and received
confirm: whether to call stat () method after the upload, to confirm the file size
 
Example:
>>> localpath=’ftp-test.log’
>>> remotepath=’/data/ftp-test.log’
>>> sftp.put(localpath,remotepath)

 

 
3) get method
 
get(self, remotepath, localpath, callback=None)
Parameter Description:
remotepath: need to download remote files
localpath: local storage path
callback: put the same method
 
4) Other methods
 
mkdir: create a directory for
remove: remove directory
rename: Rename
stat: Getting file information
listdir: get a directory listing
 
Understand the complete module, we first try to achieve what connection ssh:
Import paramiko
 Import datetime
 Import OS 

hostname = " 47.10.84. " 
Port = 22 is 
username = " the root " 
password = "" 

ssh = paramiko.SSHClient () Create a # ssh connection 
ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ())   # object It is to accept the host is not under local known hosts file. missing host key it is obvious that means to make any operation in the case of loss of the host key, 
                                    automatically add the appropriate policy
ssh.connect (hostname
= hostname, Port = 22 is, username = username, password = password) stdin , stdout, stderr = ssh.exec_command ('ls')    #传送指令 res,err = stdout.read(),stderr.read() result = res if res else err print(result) ssh.close()

Unfortunately, this approach can only transfer instructions several times, here I introduce another way

 

Guess you like

Origin www.cnblogs.com/pwn2web/p/11117761.html