python脚本 -- 模拟按键输入文本/文件到不能粘贴与上传的服务器

背景

EDA服务器为了保密禁止通过VNC进行复制粘贴等操作,我手上暂时也没有跳板机账号进行上传文件。不过就算有跳板机,经常想上传比较短的代码还得折腾一圈。然后就想又没禁止键盘输入,那么就用脚本来模拟键盘输入好了。

然后试了下在VNC Viewer中是可以的(本来还打算如果普通的系统事件会被阻止的话就用HID驱动来模拟输入设备来进行驱动层的输入,这样就不用了😂)。

代码

import multiprocessing
from multiprocessing import Process
from typing import Optional
from ctypes import c_bool
import time
import keyboard
import pyperclip 
import win32clipboard
import base64

keys_to_check = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
                     'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 
                     'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', 
                     '5', '6', '7', '8', '9', '0', 'enter', 'space', 
                     'shift', 'ctrl', 'alt']

def wait_key_release():
    start_time = time.time()
    current_time = start_time 
    while current_time - start_time < 3.0:
        if any(map(keyboard.is_pressed, keys_to_check)):
            time.sleep(0.02)
            current_time = time.time()
            continue
        else:
            break

def getText():
    wait_key_release()
    win32clipboard.OpenClipboard()
    text = ''
    try:
        text = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)     
    except Exception as e:
        try:
            data = win32clipboard.GetClipboardData(win32clipboard.CF_HDROP)
            path = data[0]
            with open(path, 'rb') as file:
                file_content = file.read()
                file_base64 = base64.b64encode(file_content)
                text = file_base64.decode('utf-8')
        except Exception as e:
            print(e)
    win32clipboard.CloseClipboard()
    return text

def target(start_flag: multiprocessing.Value):
    text = getText()
    # print(text)  
    keyboard.write(text.replace('\t', '    '))
    start_flag.value = False


if __name__ == '__main__':

    start_flag = multiprocessing.Value(c_bool, False)
    exec_proc: Optional[Process] = None

    def start():
        global exec_proc
        if start_flag.value == False:
            print('start')
            start_flag.value = True
            exec_proc = Process(target=target, args=(start_flag,))
            exec_proc.start()

    def stop():
        global exec_proc
        if start_flag.value == True:
            print('end')
            if exec_proc:
                exec_proc.terminate()
                start_flag.value = False
                exec_proc = None

    keyboard.add_hotkey('ctrl+alt+[', start)
    keyboard.add_hotkey('ctrl+alt+]', stop)
    while True:
        time.sleep(0.01)
        

将文字或文件复制到剪切板,然后在VNC viewer中按代码中的快捷键ctrl+alt+[就可以输入了

不过如果是文件的话,会将文件内容编码为base64,因此需要在服务器端再恢复

import base64
import argparse

def save_base64_as_file(base64_content, file_path):
    with open(file_path, 'wb') as file:
        decoded_content = base64.b64decode(base64_content)
        file.write(decoded_content)

parser = argparse.ArgumentParser(description='recover Base64 file to origin file')
parser.add_argument('base64_path', type=str, help='Base64 content file path')
parser.add_argument('save_path', type=str, help='save file path')

args = parser.parse_args()

with open(args.base64_path, 'r') as f:
    save_base64_as_file(f.read(), args.save_path)

python decode.py [上一步得到的base64文件路径] [输出文件路径]

posted @ 2024-06-14 04:20  一鸣惊人_001  阅读(40)  评论(0)    收藏  举报