From 7134f2280b67c246bfc48202d05f888ff16a1fe9 Mon Sep 17 00:00:00 2001 From: "mend-bolt-for-github[bot]" <42819689+mend-bolt-for-github[bot]@users.noreply.github.com> Date: Mon, 22 May 2023 15:45:14 +0000 Subject: [PATCH 1/9] Add .whitesource configuration file --- .whitesource | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .whitesource diff --git a/.whitesource b/.whitesource new file mode 100644 index 0000000..9c7ae90 --- /dev/null +++ b/.whitesource @@ -0,0 +1,14 @@ +{ + "scanSettings": { + "baseBranches": [] + }, + "checkRunSettings": { + "vulnerableCheckRunConclusionLevel": "failure", + "displayMode": "diff", + "useMendCheckNames": true + }, + "issueSettings": { + "minSeverityLevel": "LOW", + "issueType": "DEPENDENCY" + } +} \ No newline at end of file From 886d66d08e01e85f4038053d09fff863ddb1db81 Mon Sep 17 00:00:00 2001 From: mymistoe <1105579357@qq.com> Date: Sat, 5 Apr 2025 22:42:16 +0800 Subject: [PATCH 2/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pwn_init.sh | 95 ++++++++++++++++++++++++++++++++++++++++++++----- pwn_init_py2.sh | 57 ----------------------------- pwn_init_py3.sh | 56 ----------------------------- 3 files changed, 87 insertions(+), 121 deletions(-) mode change 100644 => 100755 pwn_init.sh delete mode 100644 pwn_init_py2.sh delete mode 100644 pwn_init_py3.sh diff --git a/pwn_init.sh b/pwn_init.sh old mode 100644 new mode 100755 index 3984cd8..34cd2d6 --- a/pwn_init.sh +++ b/pwn_init.sh @@ -1,14 +1,46 @@ #!/bin/bash +set -eux + +# 错误处理函数 +error_exit() { + echo "错误: $1" >&2 + exit 1 +} + +# 命令执行检查函数 +check_command() { + if [ $? -ne 0 ]; then + error_exit "命令执行失败: $1" + fi +} + +# 依赖包检查函数 +check_dependency() { + if ! command -v $1 &> /dev/null; then + error_exit "缺少必要依赖: $1" + fi +} + echo "Author : giantbranch " echo "" echo "Github : https://github.com/giantbranch/pwn-env-init" echo "" -echo "This is the previous script, please use pwn_init_py2.sh or pwn_init_py3.sh" -echo "If you want to install python2 pwn env, execute pwn_init_py2.sh" -echo "If you want to install python3 pwn env, execute pwn_init_py3.sh" -exit 1 +# 检查必要依赖 +echo "检查系统依赖..." +check_dependency "apt-get" +check_dependency "git" +check_dependency "gdb" + +# 询问用户选择Python版本 +echo "请选择Python版本 (2/3):" +read python_version + +if [[ $python_version != "2" && $python_version != "3" ]]; then + error_exit "无效的选择,请输入2或3" +fi + cd ~/ # change sourse to ustc echo "I suggest you modify the /etc/apt/sources.list file to speed up the download." @@ -17,39 +49,86 @@ echo "I suggest you modify the /etc/apt/sources.list file to speed up the downlo #sudo sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list # change sourse —— deb-src sudo sed -i 's/# deb-src/deb-src/' "/etc/apt/sources.list" +check_command "修改sources.list失败" + # change pip source -mkdir ~/.pip +if [ ! -d ~/.pip ]; then + mkdir ~/.pip +fi echo -e "[global]\nindex-url = https://pypi.douban.com/simple/\n[install]\ntrusted-host = pypi.douban.com" > ~/.pip/pip.conf +check_command "配置pip源失败" + # support 32 bit dpkg --add-architecture i386 +check_command "添加32位架构支持失败" + sudo apt-get update +check_command "更新软件源失败" + # sudo apt-get -y install lib32z1 sudo apt-get -y install libc6-i386 +check_command "安装libc6-i386失败" + # maybe git? sudo apt-get -y install git gdb +check_command "安装git和gdb失败" + # install pwndbg +echo "正在安装pwndbg..." git clone https://github.com/pwndbg/pwndbg +check_command "克隆pwndbg失败" cd pwndbg ./setup.sh +check_command "安装pwndbg失败" + # install peda +echo "正在安装peda..." git clone https://github.com/longld/peda.git ~/peda +check_command "克隆peda失败" echo "source ~/peda/peda.py" >> ~/.gdbinit +check_command "配置peda失败" + # download the libc source to current directory(you can use gdb with this example command: directory ~/glibc-2.24/malloc/) +echo "正在下载libc源码..." sudo apt-get source libc6-dev -# install pwntools -sudo apt-get -y install python python-pip -pip install pwntools +check_command "下载libc源码失败" + +# 根据用户选择安装不同版本的Python环境 +if [ "$python_version" = "2" ]; then + echo "正在安装Python2环境..." + sudo apt-get -y install python python-pip + check_command "安装Python2失败" + pip install more-itertools==5.0.0 + check_command "安装more-itertools失败" + pip install pwntools + check_command "安装pwntools失败" +else + echo "正在安装Python3环境..." + sudo apt-get -y install python3 python3-pip + check_command "安装Python3失败" + pip3 install pwntools + check_command "安装pwntools失败" +fi + # install one_gadget +echo "正在安装one_gadget..." sudo apt-get -y install ruby +check_command "安装ruby失败" sudo gem install one_gadget +check_command "安装one_gadget失败" + # download +echo "正在安装libc-database..." git clone https://github.com/niklasb/libc-database.git ~/libc-database +check_command "克隆libc-database失败" + echo "Do you want to download libc-database now(Y/n)?" read input if [[ $input = "n" ]] || [[ $input = "N" ]]; then echo "you can cd ~/libc-database and run ./get to download the libc at anytime you want" else cd ~/libc-database && ./get + check_command "下载libc-database失败" fi echo "=========================================" echo "=============Good, Enjoy it.=============" diff --git a/pwn_init_py2.sh b/pwn_init_py2.sh deleted file mode 100644 index 0ecf8c5..0000000 --- a/pwn_init_py2.sh +++ /dev/null @@ -1,57 +0,0 @@ -#!/bin/bash - -set -eux - -echo "Author : giantbranch " -echo "" -echo "Github : https://github.com/giantbranch/pwn-env-init" -echo "" - -cd ~/ -# change sourse to ustc -echo "I suggest you modify the /etc/apt/sources.list file to speed up the download." -# echo "Press Enter to continue~" -# read -t 5 test -#sudo sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list -# change sourse —— deb-src -sudo sed -i 's/# deb-src/deb-src/' "/etc/apt/sources.list" -# change pip source -if [ ! -d ~/.pip ]; then - mkdir ~/.pip -fi -echo -e "[global]\nindex-url = https://pypi.douban.com/simple/\n[install]\ntrusted-host = pypi.douban.com" > ~/.pip/pip.conf -# support 32 bit -dpkg --add-architecture i386 -sudo apt-get update -# sudo apt-get -y install lib32z1 -sudo apt-get -y install libc6-i386 -# maybe git? -sudo apt-get -y install git gdb -# install pwndbg -git clone https://github.com/pwndbg/pwndbg -cd pwndbg -./setup.sh -# install peda -git clone https://github.com/longld/peda.git ~/peda -echo "source ~/peda/peda.py" >> ~/.gdbinit -# download the libc source to current directory(you can use gdb with this example command: directory ~/glibc-2.24/malloc/) -sudo apt-get source libc6-dev -# install pwntools -sudo apt-get -y install python python-pip -pip install more-itertools==5.0.0 -pip install pwntools -# install one_gadget -sudo apt-get -y install ruby -sudo gem install one_gadget -# download -git clone https://github.com/niklasb/libc-database.git ~/libc-database -echo "Do you want to download libc-database now(Y/n)?" -read input -if [[ $input = "n" ]] || [[ $input = "N" ]]; then - echo "you can cd ~/libc-database and run ./get to download the libc at anytime you want" -else - cd ~/libc-database && ./get -fi -echo "=========================================" -echo "=============Good, Enjoy it.=============" -echo "=========================================" diff --git a/pwn_init_py3.sh b/pwn_init_py3.sh deleted file mode 100644 index 4a23037..0000000 --- a/pwn_init_py3.sh +++ /dev/null @@ -1,56 +0,0 @@ -#!/bin/bash - -set -eux - -echo "Author : giantbranch " -echo -echo "Github : https://github.com/giantbranch/pwn-env-init" -echo - -cd ~/ -# change sourse to ustc -echo "I suggest you modify the /etc/apt/sources.list file to speed up the download." -echo "Press Enter to continue~" -read -t 5 test -#sudo sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list -# change sourse —— deb-src -sudo sed -i 's/# deb-src/deb-src/' "/etc/apt/sources.list" -# change pip source -if [ ! -d ~/.pip ]; then - mkdir ~/.pip -fi -echo -e "[global]\nindex-url = https://pypi.douban.com/simple/\n[install]\ntrusted-host = pypi.douban.com" > ~/.pip/pip.conf -# support 32 bit -dpkg --add-architecture i386 -sudo apt-get update -# sudo apt-get -y install lib32z1 -sudo apt-get -y install libc6-i386 -# maybe git? -sudo apt-get -y install git gdb -# install pwndbg -git clone https://github.com/pwndbg/pwndbg -cd pwndbg -./setup.sh -# install peda -git clone https://github.com/longld/peda.git ~/peda -echo "source ~/peda/peda.py" >> ~/.gdbinit -# download the libc source to current directory(you can use gdb with this example command: directory ~/glibc-2.24/malloc/) -sudo apt-get source libc6-dev -# install pwntools -sudo apt-get -y install python3 python3-pip -pip3 install pwntools -# install one_gadget -sudo apt-get -y install ruby -sudo gem install one_gadget -# download -git clone https://github.com/niklasb/libc-database.git ~/libc-database -echo "Do you want to download libc-database now(Y/n)?" -read input -if [[ $input = "n" ]] || [[ $input = "N" ]]; then - echo "you can cd ~/libc-database and run ./get to download the libc at anytime you want" -else - cd ~/libc-database && ./get -fi -echo "=========================================" -echo "=============Good, Enjoy it.=============" -echo "=========================================" From a6b0597ff6858443ff2c7e36fa8426548eb573c0 Mon Sep 17 00:00:00 2001 From: mymistoe <1105579357@qq.com> Date: Sat, 5 Apr 2025 22:51:42 +0800 Subject: [PATCH 3/9] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B8=85=E7=90=86?= =?UTF-8?q?=E4=B8=B4=E6=97=B6=E6=96=87=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO.md | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++ pwn_init.sh | 55 +++++++++++++++++++++++++++++++- 2 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 TODO.md diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..9a5fc94 --- /dev/null +++ b/TODO.md @@ -0,0 +1,92 @@ +# PWN环境配置脚本改进计划 + +## 1. 错误处理 +- [ ] 添加关键命令的错误检查 + ```bash + if [ $? -ne 0 ]; then + echo "安装失败,请检查错误信息" + exit 1 + fi + ``` +- [ ] 添加依赖包安装状态检查 + +## 2. 清理功能 +- [ ] 添加清理函数 + ```bash + cleanup() { + echo "清理临时文件..." + # 清理操作 + } + trap cleanup EXIT + ``` +- [ ] 添加安装失败时的回滚机制 +- [ ] 添加临时文件清理 + +## 3. 进度提示 +- [ ] 添加安装进度显示 + ```bash + echo "正在安装 pwntools..." + ``` +- [ ] 添加总体进度百分比 +- [ ] 添加预计剩余时间显示 +- [ ] 添加彩色输出支持 + +## 4. 系统检查 +- [ ] 添加必要工具的存在性检查 +- [ ] 添加系统架构检查 + +## 5. 权限管理 +- [ ] 添加 root 权限检查 + ```bash + if [ "$EUID" -ne 0 ]; then + echo "请使用 sudo 运行此脚本" + exit 1 + fi + ``` +- [ ] 添加用户权限检查 +- [ ] 添加目录权限检查 + +## 6. 功能增强 +- [ ] 添加配置文件支持 +- [ ] 添加命令行参数支持 +- [ ] 添加静默安装模式 +- [ ] 添加日志记录功能 +- [ ] 添加环境变量配置 +- [ ] 添加工具版本管理 + +## 7. 用户体验 +- [ ] 添加帮助信息显示 +- [ ] 添加安装选项说明 +- [ ] 添加常见问题解答 +- [ ] 添加安装完成后的使用说明 +- [ ] 添加交互式配置选项 + +## 8. 安全性 +- [ ] 添加敏感信息保护 +- [ ] 添加命令执行安全检查 +- [ ] 添加文件权限设置 +- [ ] 添加安全配置选项 + +## 9. 维护性 +- [ ] 添加版本号管理 +- [ ] 添加更新检查机制 +- [ ] 添加依赖关系管理 +- [ ] 添加代码注释完善 +- [ ] 添加模块化结构 + +## 10. 兼容性 +- [ ] 添加多发行版支持 +- [ ] 添加多架构支持 +- [ ] 添加多Python版本支持 +- [ ] 添加多Shell环境支持 + +## 优先级说明 +1. 高优先级:错误处理、系统检查、权限管理 +2. 中优先级:进度提示、功能增强、用户体验 +3. 低优先级:维护性、兼容性、安全性 + +## 注意事项 +- 保持脚本的简洁性和可维护性 +- 确保向后兼容性 +- 保持错误提示的清晰性 +- 确保安装过程的可追踪性 \ No newline at end of file diff --git a/pwn_init.sh b/pwn_init.sh index 34cd2d6..51fa152 100755 --- a/pwn_init.sh +++ b/pwn_init.sh @@ -5,6 +5,7 @@ set -eux # 错误处理函数 error_exit() { echo "错误: $1" >&2 + cleanup exit 1 } @@ -22,6 +23,51 @@ check_dependency() { fi } +# 临时文件列表 +TEMP_FILES=() +TEMP_DIRS=() + +# 清理函数 +cleanup() { + echo "正在清理临时文件..." + + # 清理临时文件 + for file in "${TEMP_FILES[@]}"; do + if [ -f "$file" ]; then + rm -f "$file" + echo "已删除临时文件: $file" + fi + done + + # 清理临时目录 + for dir in "${TEMP_DIRS[@]}"; do + if [ -d "$dir" ]; then + rm -rf "$dir" + echo "已删除临时目录: $dir" + fi + done + + # 如果安装失败,尝试回滚已安装的包 + if [ "$INSTALL_FAILED" = true ]; then + echo "正在回滚已安装的包..." + # 回滚Python包 + if [ "$python_version" = "2" ]; then + pip uninstall -y pwntools more-itertools + else + pip3 uninstall -y pwntools + fi + # 回滚系统包 + sudo apt-get remove -y libc6-i386 ruby + sudo gem uninstall one_gadget + fi +} + +# 设置清理陷阱 +trap cleanup EXIT + +# 安装状态标志 +INSTALL_FAILED=false + echo "Author : giantbranch " echo "" echo "Github : https://github.com/giantbranch/pwn-env-init" @@ -41,7 +87,11 @@ if [[ $python_version != "2" && $python_version != "3" ]]; then error_exit "无效的选择,请输入2或3" fi -cd ~/ +# 创建临时目录 +TEMP_DIR=$(mktemp -d) +TEMP_DIRS+=("$TEMP_DIR") +cd "$TEMP_DIR" + # change sourse to ustc echo "I suggest you modify the /etc/apt/sources.list file to speed up the download." # echo "Press Enter to continue~" @@ -133,3 +183,6 @@ fi echo "=========================================" echo "=============Good, Enjoy it.=============" echo "=========================================" + +# 安装成功,清除安装失败标志 +INSTALL_FAILED=false From e31e1c62436e8d9898cf90deae65e2b6a462f0b1 Mon Sep 17 00:00:00 2001 From: mymistoe <1105579357@qq.com> Date: Mon, 7 Apr 2025 00:16:25 +0800 Subject: [PATCH 4/9] =?UTF-8?q?=E4=BF=AE=E6=94=B9python=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pwn_init.sh | 333 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 289 insertions(+), 44 deletions(-) diff --git a/pwn_init.sh b/pwn_init.sh index 51fa152..bb68996 100755 --- a/pwn_init.sh +++ b/pwn_init.sh @@ -2,9 +2,213 @@ set -eux +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# 时间跟踪 +START_TIME=$(date +%s) +STEP_TIMES=() + +# 检测Python环境函数 +check_python_env() { + echo -e "${YELLOW}正在检测本地Python环境...${NC}" + + # 检测Python2 + if command -v python2 &> /dev/null; then + local py2_version=$(python2 --version 2>&1) + echo -e "${GREEN}检测到Python2环境: $py2_version${NC}" + else + echo -e "${YELLOW}未检测到Python2环境${NC}" + fi + + # 检测Python3 + if command -v python3 &> /dev/null; then + local py3_version=$(python3 --version 2>&1) + echo -e "${GREEN}检测到Python3环境: $py3_version${NC}" + else + echo -e "${YELLOW}未检测到Python3环境${NC}" + fi + + # 检测pip + if command -v pip &> /dev/null; then + local pip_version=$(pip --version 2>&1) + echo -e "${GREEN}检测到pip: $pip_version${NC}" + else + echo -e "${YELLOW}未检测到pip${NC}" + fi + + # 检测pip3 + if command -v pip3 &> /dev/null; then + local pip3_version=$(pip3 --version 2>&1) + echo -e "${GREEN}检测到pip3: $pip3_version${NC}" + else + echo -e "${YELLOW}未检测到pip3${NC}" + fi + + echo -e "${YELLOW}请选择要配置的Python版本 (2/3):${NC}" + read python_version + + if [[ $python_version != "2" && $python_version != "3" ]]; then + error_exit "无效的选择,请输入2或3" + fi + + # 检查选择的Python版本是否已安装 + if [ "$python_version" = "2" ] && ! command -v python2 &> /dev/null; then + echo -e "${YELLOW}警告: 您选择了Python2,但系统中未检测到Python2环境${NC}" + echo -e "${YELLOW}是否继续安装Python2环境?(y/N)${NC}" + read confirm + if [[ $confirm != "y" && $confirm != "Y" ]]; then + error_exit "安装已取消" + fi + elif [ "$python_version" = "3" ] && ! command -v python3 &> /dev/null; then + echo -e "${YELLOW}警告: 您选择了Python3,但系统中未检测到Python3环境${NC}" + echo -e "${YELLOW}是否继续安装Python3环境?(y/N)${NC}" + read confirm + if [[ $confirm != "y" && $confirm != "Y" ]]; then + error_exit "安装已取消" + fi + fi +} + +# 权限检查函数 +check_permissions() { + # 检查root权限 + if [ "$EUID" -ne 0 ]; then + echo -e "${RED}错误: 请使用sudo运行此脚本${NC}" + exit 1 + fi + + # 检查用户主目录权限 + if [ ! -w "$HOME" ]; then + echo -e "${RED}错误: 用户主目录没有写入权限${NC}" + exit 1 + fi + + # 检查必要的系统目录权限 + local system_dirs=("/usr/local/bin" "/usr/bin" "/usr/lib" "/usr/include") + for dir in "${system_dirs[@]}"; do + if [ ! -w "$dir" ]; then + echo -e "${YELLOW}警告: 目录 $dir 没有写入权限,某些功能可能受限${NC}" + fi + done + + # 检查Python包安装目录权限 + if [ "$python_version" = "2" ]; then + local python_dir=$(python -c "import site; print(site.getsitepackages()[0])" 2>/dev/null) + else + local python_dir=$(python3 -c "import site; print(site.getsitepackages()[0])" 2>/dev/null) + fi + + if [ -n "$python_dir" ] && [ ! -w "$python_dir" ]; then + echo -e "${YELLOW}警告: Python包目录 $python_dir 没有写入权限,可能需要使用sudo安装Python包${NC}" + fi + + # 检查Ruby gem目录权限 + local gem_dir=$(gem environment gemdir 2>/dev/null) + if [ -n "$gem_dir" ] && [ ! -w "$gem_dir" ]; then + echo -e "${YELLOW}警告: Ruby gem目录 $gem_dir 没有写入权限,可能需要使用sudo安装gem包${NC}" + fi +} + +# 设置目录权限函数 +set_directory_permissions() { + local dirs=("$HOME/peda" "$HOME/pwndbg" "$HOME/libc-database") + + for dir in "${dirs[@]}"; do + if [ -d "$dir" ]; then + chmod -R 755 "$dir" + echo -e "${GREEN}已设置目录权限: $dir${NC}" + fi + done +} + +# 系统架构检查函数 +check_architecture() { + local arch=$(uname -m) + echo -e "${YELLOW}检测系统架构: $arch${NC}" + + case $arch in + "x86_64") + echo -e "${GREEN}系统架构支持: 64位系统${NC}" + ;; + "i386"|"i486"|"i586"|"i686") + echo -e "${GREEN}系统架构支持: 32位系统${NC}" + ;; + "aarch64"|"arm64") + echo -e "${YELLOW}警告: 检测到ARM架构,某些功能可能不受支持${NC}" + ;; + *) + echo -e "${RED}错误: 不支持的架构: $arch${NC}" + exit 1 + ;; + esac +} + +# 必要工具检查函数 +check_required_tools() { + local tools=("curl" "wget" "make" "gcc" "g++" "python" "python3" "pip" "pip3" "ruby" "gem") + local missing_tools=() + + echo -e "${YELLOW}检查必要工具...${NC}" + + for tool in "${tools[@]}"; do + if ! command -v $tool &> /dev/null; then + missing_tools+=("$tool") + else + local version=$($tool --version 2>&1 | head -n 1) + echo -e "${GREEN}已安装: $tool${NC} - $version" + fi + done + + if [ ${#missing_tools[@]} -ne 0 ]; then + echo -e "${YELLOW}以下工具未安装,将在安装过程中自动安装:${NC}" + for tool in "${missing_tools[@]}"; do + echo -e "${YELLOW}- $tool${NC}" + done + fi +} + +# 进度显示函数 +show_progress() { + local current=$1 + local total=$2 + local message=$3 + local percent=$((current * 100 / total)) + local bar_length=50 + local filled=$((percent * bar_length / 100)) + local bar=$(printf "%${filled}s" | tr " " "=") + local empty=$(printf "%$((bar_length - filled))s" | tr " " " ") + + # 计算预计剩余时间 + local current_time=$(date +%s) + local elapsed=$((current_time - START_TIME)) + local avg_time=$((elapsed / current)) + local remaining=$((avg_time * (total - current))) + local remaining_min=$((remaining / 60)) + local remaining_sec=$((remaining % 60)) + + # 使用颜色输出 + printf "\r${BLUE}[%-${bar_length}s]${NC} ${GREEN}%3d%%${NC} ${YELLOW}%s${NC} ${RED}(预计剩余: %02d:%02d)${NC}" \ + "$bar$empty" "$percent" "$message" "$remaining_min" "$remaining_sec" +} + +# 更新进度函数 +update_progress() { + local step_start=$(date +%s) + ((CURRENT_STEP++)) + show_progress "$CURRENT_STEP" "$TOTAL_STEPS" "$1" + echo "" + local step_end=$(date +%s) + STEP_TIMES+=($((step_end - step_start))) +} + # 错误处理函数 error_exit() { - echo "错误: $1" >&2 + echo -e "${RED}错误: $1${NC}" >&2 cleanup exit 1 } @@ -29,13 +233,13 @@ TEMP_DIRS=() # 清理函数 cleanup() { - echo "正在清理临时文件..." + echo -e "${YELLOW}正在清理临时文件...${NC}" # 清理临时文件 for file in "${TEMP_FILES[@]}"; do if [ -f "$file" ]; then rm -f "$file" - echo "已删除临时文件: $file" + echo -e "${GREEN}已删除临时文件: $file${NC}" fi done @@ -43,13 +247,13 @@ cleanup() { for dir in "${TEMP_DIRS[@]}"; do if [ -d "$dir" ]; then rm -rf "$dir" - echo "已删除临时目录: $dir" + echo -e "${GREEN}已删除临时目录: $dir${NC}" fi done # 如果安装失败,尝试回滚已安装的包 if [ "$INSTALL_FAILED" = true ]; then - echo "正在回滚已安装的包..." + echo -e "${YELLOW}正在回滚已安装的包...${NC}" # 回滚Python包 if [ "$python_version" = "2" ]; then pip uninstall -y pwntools more-itertools @@ -68,45 +272,74 @@ trap cleanup EXIT # 安装状态标志 INSTALL_FAILED=false -echo "Author : giantbranch " +# 进度跟踪 +TOTAL_STEPS=15 # 总步骤数增加 +CURRENT_STEP=0 # 当前步骤 + +echo -e "${BLUE}Author : giantbranch ${NC}" echo "" -echo "Github : https://github.com/giantbranch/pwn-env-init" +echo -e "${BLUE}Github : https://github.com/giantbranch/pwn-env-init${NC}" echo "" +# 检查Python环境 +check_python_env +update_progress "Python环境检测完成" + +# 检查权限 +check_permissions +update_progress "权限检查完成" + +# 检查系统架构 +check_architecture +update_progress "系统架构检查完成" + +# 检查必要工具 +check_required_tools +update_progress "必要工具检查完成" + # 检查必要依赖 -echo "检查系统依赖..." +echo -e "${YELLOW}检查系统依赖...${NC}" check_dependency "apt-get" check_dependency "git" check_dependency "gdb" - -# 询问用户选择Python版本 -echo "请选择Python版本 (2/3):" -read python_version - -if [[ $python_version != "2" && $python_version != "3" ]]; then - error_exit "无效的选择,请输入2或3" -fi +update_progress "系统依赖检查完成" # 创建临时目录 TEMP_DIR=$(mktemp -d) TEMP_DIRS+=("$TEMP_DIR") cd "$TEMP_DIR" -# change sourse to ustc -echo "I suggest you modify the /etc/apt/sources.list file to speed up the download." -# echo "Press Enter to continue~" -# read -t 5 test -#sudo sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list -# change sourse —— deb-src -sudo sed -i 's/# deb-src/deb-src/' "/etc/apt/sources.list" -check_command "修改sources.list失败" +# 配置清华源 +echo -e "${YELLOW}正在配置清华源...${NC}" + +# 备份原有源 +echo -e "${YELLOW}正在备份原有源文件到 /etc/apt/sources.list.bak${NC}" +echo -e "${YELLOW}如果需要恢复原有源,请执行: sudo cp /etc/apt/sources.list.bak /etc/apt/sources.list${NC}" +sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak +check_command "备份原有源失败" -# change pip source +# 配置apt清华源 +sudo tee /etc/apt/sources.list << EOF +# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 +deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse +# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse +deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse +# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse +deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse +# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse +deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse +# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse +EOF +check_command "配置apt清华源失败" + +# 配置pip清华源 if [ ! -d ~/.pip ]; then mkdir ~/.pip fi -echo -e "[global]\nindex-url = https://pypi.douban.com/simple/\n[install]\ntrusted-host = pypi.douban.com" > ~/.pip/pip.conf -check_command "配置pip源失败" +echo -e "[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple/\n[install]\ntrusted-host = pypi.tuna.tsinghua.edu.cn" > ~/.pip/pip.conf +check_command "配置pip清华源失败" + +update_progress "配置清华源完成" # support 32 bit dpkg --add-architecture i386 @@ -115,37 +348,40 @@ check_command "添加32位架构支持失败" sudo apt-get update check_command "更新软件源失败" -# sudo apt-get -y install lib32z1 sudo apt-get -y install libc6-i386 check_command "安装libc6-i386失败" -# maybe git? sudo apt-get -y install git gdb check_command "安装git和gdb失败" +update_progress "安装系统依赖完成" # install pwndbg -echo "正在安装pwndbg..." +echo -e "${YELLOW}正在安装pwndbg...${NC}" git clone https://github.com/pwndbg/pwndbg check_command "克隆pwndbg失败" cd pwndbg ./setup.sh check_command "安装pwndbg失败" +cd .. +update_progress "安装pwndbg完成" # install peda -echo "正在安装peda..." +echo -e "${YELLOW}正在安装peda...${NC}" git clone https://github.com/longld/peda.git ~/peda check_command "克隆peda失败" echo "source ~/peda/peda.py" >> ~/.gdbinit check_command "配置peda失败" +update_progress "安装peda完成" -# download the libc source to current directory(you can use gdb with this example command: directory ~/glibc-2.24/malloc/) -echo "正在下载libc源码..." +# download the libc source +echo -e "${YELLOW}正在下载libc源码...${NC}" sudo apt-get source libc6-dev check_command "下载libc源码失败" +update_progress "下载libc源码完成" # 根据用户选择安装不同版本的Python环境 if [ "$python_version" = "2" ]; then - echo "正在安装Python2环境..." + echo -e "${YELLOW}正在安装Python2环境...${NC}" sudo apt-get -y install python python-pip check_command "安装Python2失败" pip install more-itertools==5.0.0 @@ -153,36 +389,45 @@ if [ "$python_version" = "2" ]; then pip install pwntools check_command "安装pwntools失败" else - echo "正在安装Python3环境..." + echo -e "${YELLOW}正在安装Python3环境...${NC}" sudo apt-get -y install python3 python3-pip check_command "安装Python3失败" pip3 install pwntools check_command "安装pwntools失败" fi +update_progress "安装Python环境完成" # install one_gadget -echo "正在安装one_gadget..." +echo -e "${YELLOW}正在安装one_gadget...${NC}" sudo apt-get -y install ruby check_command "安装ruby失败" sudo gem install one_gadget check_command "安装one_gadget失败" +update_progress "安装one_gadget完成" -# download -echo "正在安装libc-database..." +# download libc-database +echo -e "${YELLOW}正在安装libc-database...${NC}" git clone https://github.com/niklasb/libc-database.git ~/libc-database check_command "克隆libc-database失败" +update_progress "安装libc-database完成" -echo "Do you want to download libc-database now(Y/n)?" +echo -e "${YELLOW}Do you want to download libc-database now(Y/n)?${NC}" read input if [[ $input = "n" ]] || [[ $input = "N" ]]; then - echo "you can cd ~/libc-database and run ./get to download the libc at anytime you want" + echo -e "${YELLOW}you can cd ~/libc-database and run ./get to download the libc at anytime you want${NC}" else - cd ~/libc-database && ./get + cd ~/libc-database && ./get check_command "下载libc-database失败" + update_progress "下载libc-database完成" fi -echo "=========================================" -echo "=============Good, Enjoy it.=============" -echo "=========================================" + +# 设置目录权限 +set_directory_permissions +update_progress "设置目录权限完成" # 安装成功,清除安装失败标志 INSTALL_FAILED=false + +echo -e "${GREEN}=========================================${NC}" +echo -e "${GREEN}=============Good, Enjoy it.=============${NC}" +echo -e "${GREEN}=========================================${NC}" From 28a0fcdf5926c098e7a2673f24c6f4475bde31db Mon Sep 17 00:00:00 2001 From: mymistoe <1105579357@qq.com> Date: Mon, 7 Apr 2025 00:21:02 +0800 Subject: [PATCH 5/9] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9Ashell=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pwn_init.sh | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/pwn_init.sh b/pwn_init.sh index bb68996..3162db5 100755 --- a/pwn_init.sh +++ b/pwn_init.sh @@ -13,6 +13,76 @@ NC='\033[0m' # No Color START_TIME=$(date +%s) STEP_TIMES=() +# 检测shell环境函数 +check_shell_env() { + echo -e "${YELLOW}正在检测shell环境...${NC}" + + # 获取当前shell + local current_shell=$(basename "$SHELL") + echo -e "${GREEN}当前使用的shell: $current_shell${NC}" + + # 检查是否安装了其他常用shell + local shells=("bash" "zsh" "fish") + local installed_shells=() + + for shell in "${shells[@]}"; do + if command -v $shell &> /dev/null; then + local version=$($shell --version 2>&1 | head -n 1) + installed_shells+=("$shell") + echo -e "${GREEN}已安装: $shell${NC} - $version" + else + echo -e "${YELLOW}未安装: $shell${NC}" + fi + done + + # 检查shell配置文件 + case $current_shell in + "bash") + if [ -f ~/.bashrc ]; then + echo -e "${GREEN}检测到bash配置文件: ~/.bashrc${NC}" + else + echo -e "${YELLOW}未检测到bash配置文件: ~/.bashrc${NC}" + fi + ;; + "zsh") + if [ -f ~/.zshrc ]; then + echo -e "${GREEN}检测到zsh配置文件: ~/.zshrc${NC}" + else + echo -e "${YELLOW}未检测到zsh配置文件: ~/.zshrc${NC}" + fi + ;; + "fish") + if [ -d ~/.config/fish ]; then + echo -e "${GREEN}检测到fish配置目录: ~/.config/fish${NC}" + else + echo -e "${YELLOW}未检测到fish配置目录: ~/.config/fish${NC}" + fi + ;; + *) + echo -e "${YELLOW}警告: 检测到不常见的shell: $current_shell${NC}" + ;; + esac + + # 询问用户是否要安装其他shell + if [ ${#installed_shells[@]} -lt ${#shells[@]} ]; then + echo -e "${YELLOW}是否要安装其他shell环境?(y/N)${NC}" + read install_other_shells + if [[ $install_other_shells == "y" || $install_other_shells == "Y" ]]; then + for shell in "${shells[@]}"; do + if ! command -v $shell &> /dev/null; then + echo -e "${YELLOW}是否安装 $shell?(y/N)${NC}" + read install_shell + if [[ $install_shell == "y" || $install_shell == "Y" ]]; then + sudo apt-get install -y $shell + check_command "安装$shell失败" + echo -e "${GREEN}已安装: $shell${NC}" + fi + fi + done + fi + fi +} + # 检测Python环境函数 check_python_env() { echo -e "${YELLOW}正在检测本地Python环境...${NC}" @@ -273,7 +343,7 @@ trap cleanup EXIT INSTALL_FAILED=false # 进度跟踪 -TOTAL_STEPS=15 # 总步骤数增加 +TOTAL_STEPS=16 # 总步骤数增加 CURRENT_STEP=0 # 当前步骤 echo -e "${BLUE}Author : giantbranch ${NC}" @@ -281,6 +351,10 @@ echo "" echo -e "${BLUE}Github : https://github.com/giantbranch/pwn-env-init${NC}" echo "" +# 检查shell环境 +check_shell_env +update_progress "Shell环境检测完成" + # 检查Python环境 check_python_env update_progress "Python环境检测完成" From de7b87905a158079d2cb3f0a585f22a5b207f674 Mon Sep 17 00:00:00 2001 From: mymistoe <1105579357@qq.com> Date: Mon, 7 Apr 2025 00:29:01 +0800 Subject: [PATCH 6/9] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pwn_init.sh | 192 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 117 insertions(+), 75 deletions(-) diff --git a/pwn_init.sh b/pwn_init.sh index 3162db5..7f4dfe3 100755 --- a/pwn_init.sh +++ b/pwn_init.sh @@ -2,6 +2,14 @@ set -eux +# 全局变量定义 +python_version="" +INSTALL_FAILED=false +TEMP_FILES=() +TEMP_DIRS=() +TOTAL_STEPS=16 +CURRENT_STEP=0 + # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' @@ -13,6 +21,66 @@ NC='\033[0m' # No Color START_TIME=$(date +%s) STEP_TIMES=() +# 命令执行检查函数 +check_command() { + local status=$? + if [ $status -ne 0 ]; then + error_exit "命令执行失败: $1 (状态码: $status)" + fi +} + +# 错误处理函数 +error_exit() { + echo -e "${RED}错误: $1${NC}" >&2 + cleanup + exit 1 +} + +# 依赖包检查函数 +check_dependency() { + if ! command -v $1 &> /dev/null; then + error_exit "缺少必要依赖: $1" + fi +} + +# 清理函数 +cleanup() { + echo -e "${YELLOW}正在清理临时文件...${NC}" + + # 清理临时文件 + for file in "${TEMP_FILES[@]}"; do + if [ -f "$file" ]; then + rm -f "$file" + echo -e "${GREEN}已删除临时文件: $file${NC}" + fi + done + + # 清理临时目录 + for dir in "${TEMP_DIRS[@]}"; do + if [ -d "$dir" ]; then + rm -rf "$dir" + echo -e "${GREEN}已删除临时目录: $dir${NC}" + fi + done + + # 如果安装失败,尝试回滚已安装的包 + if [ "$INSTALL_FAILED" = true ]; then + echo -e "${YELLOW}正在回滚已安装的包...${NC}" + # 回滚Python包 + if [ "$python_version" = "2" ]; then + pip uninstall -y pwntools more-itertools || true + else + pip3 uninstall -y pwntools || true + fi + # 回滚系统包 + sudo apt-get remove -y libc6-i386 ruby || true + sudo gem uninstall one_gadget || true + fi +} + +# 设置清理陷阱 +trap cleanup EXIT + # 检测shell环境函数 check_shell_env() { echo -e "${YELLOW}正在检测shell环境...${NC}" @@ -81,6 +149,9 @@ check_shell_env() { done fi fi + + # 更新总步骤数 + update_total_steps } # 检测Python环境函数 @@ -142,6 +213,9 @@ check_python_env() { error_exit "安装已取消" fi fi + + # 更新总步骤数 + update_total_steps } # 权限检查函数 @@ -168,9 +242,9 @@ check_permissions() { # 检查Python包安装目录权限 if [ "$python_version" = "2" ]; then - local python_dir=$(python -c "import site; print(site.getsitepackages()[0])" 2>/dev/null) + local python_dir=$(python2 -c "import site; print(site.getsitepackages()[0])" 2>/dev/null || true) else - local python_dir=$(python3 -c "import site; print(site.getsitepackages()[0])" 2>/dev/null) + local python_dir=$(python3 -c "import site; print(site.getsitepackages()[0])" 2>/dev/null || true) fi if [ -n "$python_dir" ] && [ ! -w "$python_dir" ]; then @@ -178,7 +252,7 @@ check_permissions() { fi # 检查Ruby gem目录权限 - local gem_dir=$(gem environment gemdir 2>/dev/null) + local gem_dir=$(gem environment gemdir 2>/dev/null || true) if [ -n "$gem_dir" ] && [ ! -w "$gem_dir" ]; then echo -e "${YELLOW}警告: Ruby gem目录 $gem_dir 没有写入权限,可能需要使用sudo安装gem包${NC}" fi @@ -256,14 +330,22 @@ show_progress() { # 计算预计剩余时间 local current_time=$(date +%s) local elapsed=$((current_time - START_TIME)) - local avg_time=$((elapsed / current)) - local remaining=$((avg_time * (total - current))) - local remaining_min=$((remaining / 60)) - local remaining_sec=$((remaining % 60)) + local remaining="" + + # 避免除零错误 + if [ $current -gt 0 ]; then + local avg_time=$((elapsed / current)) + local remaining_time=$((avg_time * (total - current))) + local remaining_min=$((remaining_time / 60)) + local remaining_sec=$((remaining_time % 60)) + remaining="(预计剩余: ${remaining_min}:${remaining_sec})" + else + remaining="(正在初始化...)" + fi # 使用颜色输出 - printf "\r${BLUE}[%-${bar_length}s]${NC} ${GREEN}%3d%%${NC} ${YELLOW}%s${NC} ${RED}(预计剩余: %02d:%02d)${NC}" \ - "$bar$empty" "$percent" "$message" "$remaining_min" "$remaining_sec" + printf "\r${BLUE}[%-${bar_length}s]${NC} ${GREEN}%3d%%${NC} ${YELLOW}%s${NC} ${RED}%s${NC}" \ + "$bar$empty" "$percent" "$message" "$remaining" } # 更新进度函数 @@ -276,81 +358,40 @@ update_progress() { STEP_TIMES+=($((step_end - step_start))) } -# 错误处理函数 -error_exit() { - echo -e "${RED}错误: $1${NC}" >&2 - cleanup - exit 1 -} - -# 命令执行检查函数 -check_command() { - if [ $? -ne 0 ]; then - error_exit "命令执行失败: $1" - fi -} - -# 依赖包检查函数 -check_dependency() { - if ! command -v $1 &> /dev/null; then - error_exit "缺少必要依赖: $1" - fi -} - -# 临时文件列表 -TEMP_FILES=() -TEMP_DIRS=() - -# 清理函数 -cleanup() { - echo -e "${YELLOW}正在清理临时文件...${NC}" - - # 清理临时文件 - for file in "${TEMP_FILES[@]}"; do - if [ -f "$file" ]; then - rm -f "$file" - echo -e "${GREEN}已删除临时文件: $file${NC}" - fi - done +# 动态更新总步骤数 +update_total_steps() { + local additional_steps=0 - # 清理临时目录 - for dir in "${TEMP_DIRS[@]}"; do - if [ -d "$dir" ]; then - rm -rf "$dir" - echo -e "${GREEN}已删除临时目录: $dir${NC}" - fi - done + # 检查是否需要安装其他shell + if [ ${#installed_shells[@]} -lt ${#shells[@]} ]; then + for shell in "${shells[@]}"; do + if ! command -v $shell &> /dev/null; then + ((additional_steps++)) + fi + done + fi - # 如果安装失败,尝试回滚已安装的包 - if [ "$INSTALL_FAILED" = true ]; then - echo -e "${YELLOW}正在回滚已安装的包...${NC}" - # 回滚Python包 - if [ "$python_version" = "2" ]; then - pip uninstall -y pwntools more-itertools - else - pip3 uninstall -y pwntools - fi - # 回滚系统包 - sudo apt-get remove -y libc6-i386 ruby - sudo gem uninstall one_gadget + # 检查是否需要安装Python环境 + if [ "$python_version" = "2" ] && ! command -v python2 &> /dev/null; then + ((additional_steps++)) + elif [ "$python_version" = "3" ] && ! command -v python3 &> /dev/null; then + ((additional_steps++)) fi + + # 更新总步骤数 + TOTAL_STEPS=$((16 + additional_steps)) } -# 设置清理陷阱 -trap cleanup EXIT - -# 安装状态标志 -INSTALL_FAILED=false - -# 进度跟踪 -TOTAL_STEPS=16 # 总步骤数增加 -CURRENT_STEP=0 # 当前步骤 - +# 主程序开始 echo -e "${BLUE}Author : giantbranch ${NC}" echo "" echo -e "${BLUE}Github : https://github.com/giantbranch/pwn-env-init${NC}" echo "" +# 初始化进度显示 +show_progress 0 1 "正在初始化..." +echo "" + # 检查shell环境 check_shell_env update_progress "Shell环境检测完成" @@ -436,7 +477,7 @@ check_command "克隆pwndbg失败" cd pwndbg ./setup.sh check_command "安装pwndbg失败" -cd .. +cd "$TEMP_DIR" # 确保返回临时目录 update_progress "安装pwndbg完成" # install peda @@ -492,6 +533,7 @@ if [[ $input = "n" ]] || [[ $input = "N" ]]; then else cd ~/libc-database && ./get check_command "下载libc-database失败" + cd "$TEMP_DIR" # 确保返回临时目录 update_progress "下载libc-database完成" fi From 311d9466b4f622991325ab89c062310f9b295e0f Mon Sep 17 00:00:00 2001 From: mymistoe <1105579357@qq.com> Date: Mon, 7 Apr 2025 00:40:41 +0800 Subject: [PATCH 7/9] =?UTF-8?q?=20=E8=A6=81=E6=8F=90=E4=BA=A4=E7=9A=84?= =?UTF-8?q?=E5=8F=98=E6=9B=B4=EF=BC=9A=20=09=E5=88=A0=E9=99=A4=EF=BC=9A=20?= =?UTF-8?q?=20=20=20=20.whitesource=20=09=E5=88=A0=E9=99=A4=EF=BC=9A=20=20?= =?UTF-8?q?=20=20=20TODO.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .whitesource | 14 -------- TODO.md | 92 ---------------------------------------------------- 2 files changed, 106 deletions(-) delete mode 100644 .whitesource delete mode 100644 TODO.md diff --git a/.whitesource b/.whitesource deleted file mode 100644 index 9c7ae90..0000000 --- a/.whitesource +++ /dev/null @@ -1,14 +0,0 @@ -{ - "scanSettings": { - "baseBranches": [] - }, - "checkRunSettings": { - "vulnerableCheckRunConclusionLevel": "failure", - "displayMode": "diff", - "useMendCheckNames": true - }, - "issueSettings": { - "minSeverityLevel": "LOW", - "issueType": "DEPENDENCY" - } -} \ No newline at end of file diff --git a/TODO.md b/TODO.md deleted file mode 100644 index 9a5fc94..0000000 --- a/TODO.md +++ /dev/null @@ -1,92 +0,0 @@ -# PWN环境配置脚本改进计划 - -## 1. 错误处理 -- [ ] 添加关键命令的错误检查 - ```bash - if [ $? -ne 0 ]; then - echo "安装失败,请检查错误信息" - exit 1 - fi - ``` -- [ ] 添加依赖包安装状态检查 - -## 2. 清理功能 -- [ ] 添加清理函数 - ```bash - cleanup() { - echo "清理临时文件..." - # 清理操作 - } - trap cleanup EXIT - ``` -- [ ] 添加安装失败时的回滚机制 -- [ ] 添加临时文件清理 - -## 3. 进度提示 -- [ ] 添加安装进度显示 - ```bash - echo "正在安装 pwntools..." - ``` -- [ ] 添加总体进度百分比 -- [ ] 添加预计剩余时间显示 -- [ ] 添加彩色输出支持 - -## 4. 系统检查 -- [ ] 添加必要工具的存在性检查 -- [ ] 添加系统架构检查 - -## 5. 权限管理 -- [ ] 添加 root 权限检查 - ```bash - if [ "$EUID" -ne 0 ]; then - echo "请使用 sudo 运行此脚本" - exit 1 - fi - ``` -- [ ] 添加用户权限检查 -- [ ] 添加目录权限检查 - -## 6. 功能增强 -- [ ] 添加配置文件支持 -- [ ] 添加命令行参数支持 -- [ ] 添加静默安装模式 -- [ ] 添加日志记录功能 -- [ ] 添加环境变量配置 -- [ ] 添加工具版本管理 - -## 7. 用户体验 -- [ ] 添加帮助信息显示 -- [ ] 添加安装选项说明 -- [ ] 添加常见问题解答 -- [ ] 添加安装完成后的使用说明 -- [ ] 添加交互式配置选项 - -## 8. 安全性 -- [ ] 添加敏感信息保护 -- [ ] 添加命令执行安全检查 -- [ ] 添加文件权限设置 -- [ ] 添加安全配置选项 - -## 9. 维护性 -- [ ] 添加版本号管理 -- [ ] 添加更新检查机制 -- [ ] 添加依赖关系管理 -- [ ] 添加代码注释完善 -- [ ] 添加模块化结构 - -## 10. 兼容性 -- [ ] 添加多发行版支持 -- [ ] 添加多架构支持 -- [ ] 添加多Python版本支持 -- [ ] 添加多Shell环境支持 - -## 优先级说明 -1. 高优先级:错误处理、系统检查、权限管理 -2. 中优先级:进度提示、功能增强、用户体验 -3. 低优先级:维护性、兼容性、安全性 - -## 注意事项 -- 保持脚本的简洁性和可维护性 -- 确保向后兼容性 -- 保持错误提示的清晰性 -- 确保安装过程的可追踪性 \ No newline at end of file From b84be56614ec7ea74821644db94310463b2c9bf9 Mon Sep 17 00:00:00 2001 From: mymistoe <1105579357@qq.com> Date: Mon, 7 Apr 2025 03:23:42 +0800 Subject: [PATCH 8/9] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=89=88=E6=9C=AC=E7=B3=BB=E7=BB=9F=E4=B8=8B=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pwn_init.sh | 70 +++++++----------- test_script.sh | 195 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 222 insertions(+), 43 deletions(-) create mode 100644 test_script.sh diff --git a/pwn_init.sh b/pwn_init.sh index 7f4dfe3..e0c635a 100755 --- a/pwn_init.sh +++ b/pwn_init.sh @@ -7,8 +7,10 @@ python_version="" INSTALL_FAILED=false TEMP_FILES=() TEMP_DIRS=() -TOTAL_STEPS=16 +TOTAL_STEPS=15 CURRENT_STEP=0 +SHELLS=("bash" "zsh" "fish") +INSTALLED_SHELLS=() # 颜色定义 RED='\033[0;31m' @@ -23,9 +25,9 @@ STEP_TIMES=() # 命令执行检查函数 check_command() { - local status=$? - if [ $status -ne 0 ]; then - error_exit "命令执行失败: $1 (状态码: $status)" + local cmd_status=$? + if [ $cmd_status -ne 0 ]; then + error_exit "命令执行失败: $1 (状态码: $cmd_status)" fi } @@ -81,6 +83,14 @@ cleanup() { # 设置清理陷阱 trap cleanup EXIT +# 安全目录切换函数 +safe_cd() { + local target_dir="$1" + if ! cd "$target_dir"; then + error_exit "无法切换到目录: $target_dir" + fi +} + # 检测shell环境函数 check_shell_env() { echo -e "${YELLOW}正在检测shell环境...${NC}" @@ -90,13 +100,12 @@ check_shell_env() { echo -e "${GREEN}当前使用的shell: $current_shell${NC}" # 检查是否安装了其他常用shell - local shells=("bash" "zsh" "fish") - local installed_shells=() + INSTALLED_SHELLS=() - for shell in "${shells[@]}"; do + for shell in "${SHELLS[@]}"; do if command -v $shell &> /dev/null; then local version=$($shell --version 2>&1 | head -n 1) - installed_shells+=("$shell") + INSTALLED_SHELLS+=("$shell") echo -e "${GREEN}已安装: $shell${NC} - $version" else echo -e "${YELLOW}未安装: $shell${NC}" @@ -132,11 +141,11 @@ check_shell_env() { esac # 询问用户是否要安装其他shell - if [ ${#installed_shells[@]} -lt ${#shells[@]} ]; then + if [ ${#INSTALLED_SHELLS[@]} -lt ${#SHELLS[@]} ]; then echo -e "${YELLOW}是否要安装其他shell环境?(y/N)${NC}" read install_other_shells if [[ $install_other_shells == "y" || $install_other_shells == "Y" ]]; then - for shell in "${shells[@]}"; do + for shell in "${SHELLS[@]}"; do if ! command -v $shell &> /dev/null; then echo -e "${YELLOW}是否安装 $shell?(y/N)${NC}" read install_shell @@ -270,28 +279,6 @@ set_directory_permissions() { done } -# 系统架构检查函数 -check_architecture() { - local arch=$(uname -m) - echo -e "${YELLOW}检测系统架构: $arch${NC}" - - case $arch in - "x86_64") - echo -e "${GREEN}系统架构支持: 64位系统${NC}" - ;; - "i386"|"i486"|"i586"|"i686") - echo -e "${GREEN}系统架构支持: 32位系统${NC}" - ;; - "aarch64"|"arm64") - echo -e "${YELLOW}警告: 检测到ARM架构,某些功能可能不受支持${NC}" - ;; - *) - echo -e "${RED}错误: 不支持的架构: $arch${NC}" - exit 1 - ;; - esac -} - # 必要工具检查函数 check_required_tools() { local tools=("curl" "wget" "make" "gcc" "g++" "python" "python3" "pip" "pip3" "ruby" "gem") @@ -363,8 +350,8 @@ update_total_steps() { local additional_steps=0 # 检查是否需要安装其他shell - if [ ${#installed_shells[@]} -lt ${#shells[@]} ]; then - for shell in "${shells[@]}"; do + if [ ${#INSTALLED_SHELLS[@]} -lt ${#SHELLS[@]} ]; then + for shell in "${SHELLS[@]}"; do if ! command -v $shell &> /dev/null; then ((additional_steps++)) fi @@ -379,7 +366,7 @@ update_total_steps() { fi # 更新总步骤数 - TOTAL_STEPS=$((16 + additional_steps)) + TOTAL_STEPS=$((15 + additional_steps)) } # 主程序开始 @@ -404,10 +391,6 @@ update_progress "Python环境检测完成" check_permissions update_progress "权限检查完成" -# 检查系统架构 -check_architecture -update_progress "系统架构检查完成" - # 检查必要工具 check_required_tools update_progress "必要工具检查完成" @@ -474,10 +457,10 @@ update_progress "安装系统依赖完成" echo -e "${YELLOW}正在安装pwndbg...${NC}" git clone https://github.com/pwndbg/pwndbg check_command "克隆pwndbg失败" -cd pwndbg +safe_cd pwndbg ./setup.sh check_command "安装pwndbg失败" -cd "$TEMP_DIR" # 确保返回临时目录 +safe_cd "$TEMP_DIR" # 确保返回临时目录 update_progress "安装pwndbg完成" # install peda @@ -531,9 +514,10 @@ read input if [[ $input = "n" ]] || [[ $input = "N" ]]; then echo -e "${YELLOW}you can cd ~/libc-database and run ./get to download the libc at anytime you want${NC}" else - cd ~/libc-database && ./get + safe_cd ~/libc-database + ./get check_command "下载libc-database失败" - cd "$TEMP_DIR" # 确保返回临时目录 + safe_cd "$TEMP_DIR" # 确保返回临时目录 update_progress "下载libc-database完成" fi diff --git a/test_script.sh b/test_script.sh new file mode 100644 index 0000000..9e4bb0c --- /dev/null +++ b/test_script.sh @@ -0,0 +1,195 @@ +#!/bin/bash + +# 支持的Linux发行版 +DISTROS=( + # Ubuntu系列 + # "ubuntu:18.04" # 已测试 + # "ubuntu:20.04" # 已测试 + # "ubuntu:22.04" # 已测试 + # "ubuntu:23.10" # 已测试 + "ubuntu:24.04" + + # Debian系列 + "debian:9" + "debian:10" + "debian:11" + "debian:12" + + # Kali Linux + "kalilinux/kali-rolling:latest" + + # Arch Linux + "archlinux:latest" + + # Fedora + "fedora:38" + "fedora:39" + + # CentOS + "centos:7" + "centos:8" + + # Rocky Linux + "rockylinux:8" + "rockylinux:9" + + # AlmaLinux + "almalinux:8" + "almalinux:9" + + # OpenSUSE + "opensuse/leap:15.5" + "opensuse/tumbleweed:latest" +) + +# 错误处理函数 +handle_error() { + local distro=$1 + local step=$2 + local error=$3 + echo -e "\n${RED}错误: $distro 在 $step 步骤失败${NC}" + echo -e "${YELLOW}错误信息:${NC}" + echo "$error" + echo -e "\n${RED}测试终止${NC}" + exit 1 +} + +# 测试函数 +test_distro() { + local distro=$1 + echo -e "\n${BLUE}开始测试 $distro...${NC}" + + # 根据发行版选择不同的包管理器 + local pkg_manager="" + if [[ $distro == *"ubuntu"* ]] || [[ $distro == *"debian"* ]] || [[ $distro == *"kali"* ]]; then + pkg_manager="apt-get" + elif [[ $distro == *"fedora"* ]] || [[ $distro == *"centos"* ]] || [[ $distro == *"rocky"* ]] || [[ $distro == *"alma"* ]]; then + pkg_manager="dnf" + elif [[ $distro == *"arch"* ]]; then + pkg_manager="pacman" + elif [[ $distro == *"opensuse"* ]]; then + pkg_manager="zypper" + fi + + # 创建并运行容器 + local output=$(docker run -it --rm --init \ + -e DEBIAN_FRONTEND=noninteractive \ + -e TZ=Asia/Shanghai \ + -v $(pwd):/pwn-env \ + -w /pwn-env \ + $distro \ + bash -c " + # 设置时区 + ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime + + # 更新系统 + echo '${BLUE}正在更新系统...${NC}' + if ! $pkg_manager update -y; then + echo '系统更新失败' + exit 1 + fi + + # 安装基本工具 + echo '${BLUE}正在安装基本工具...${NC}' + if [[ $pkg_manager == \"apt-get\" ]]; then + if ! $pkg_manager install -y sudo git curl wget vim build-essential python3 python3-pip python3-dev tzdata; then + echo '基本工具安装失败' + exit 1 + fi + elif [[ $pkg_manager == \"dnf\" ]]; then + if ! $pkg_manager install -y sudo git curl wget vim gcc gcc-c++ make python3 python3-pip python3-devel tzdata; then + echo '基本工具安装失败' + exit 1 + fi + elif [[ $pkg_manager == \"pacman\" ]]; then + if ! $pkg_manager -Syu --noconfirm sudo git curl wget vim base-devel python python-pip tzdata; then + echo '基本工具安装失败' + exit 1 + fi + elif [[ $pkg_manager == \"zypper\" ]]; then + if ! $pkg_manager install -y sudo git curl wget vim gcc gcc-c++ make python3 python3-pip python3-devel timezone; then + echo '基本工具安装失败' + exit 1 + fi + fi + + # 安装shell环境 + echo '${BLUE}正在安装shell环境...${NC}' + if [[ $pkg_manager == \"apt-get\" ]]; then + if ! $pkg_manager install -y zsh fish; then + echo 'shell环境安装失败' + exit 1 + fi + elif [[ $pkg_manager == \"dnf\" ]]; then + if ! $pkg_manager install -y zsh fish; then + echo 'shell环境安装失败' + exit 1 + fi + elif [[ $pkg_manager == \"pacman\" ]]; then + if ! $pkg_manager -S --noconfirm zsh fish; then + echo 'shell环境安装失败' + exit 1 + fi + elif [[ $pkg_manager == \"zypper\" ]]; then + if ! $pkg_manager install -y zsh fish; then + echo 'shell环境安装失败' + exit 1 + fi + fi + + # 配置Python环境 + echo '${BLUE}正在配置Python环境...${NC}' + if ! pip3 install --upgrade pip; then + echo 'pip升级失败' + exit 1 + fi + if ! pip3 install pwntools; then + echo 'pwntools安装失败' + exit 1 + fi + + # 配置shell环境 + echo '${BLUE}正在配置shell环境...${NC}' + if command -v zsh &> /dev/null; then + if ! sh -c \"\$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)\" \"\" --unattended; then + echo 'oh-my-zsh安装失败' + exit 1 + fi + fi + + # 运行测试脚本 + echo '${BLUE}正在运行测试脚本...${NC}' + chmod +x pwn_init.sh + if ! ./pwn_init.sh; then + echo '测试脚本运行失败' + exit 1 + fi + " 2>&1) + + local exit_code=$? + if [ $exit_code -ne 0 ]; then + handle_error "$distro" "测试过程" "$output" + else + echo -e "${GREEN}$distro 测试通过${NC}" + fi +} + +# 颜色定义 +RED='\033[0;31m' +GREEN='\033[0;32m' +BLUE='\033[0;34m' +YELLOW='\033[1;33m' +NC='\033[0m' # No Color + +# 检查Docker是否安装 +if ! command -v docker &> /dev/null; then + echo -e "${RED}错误: 请先安装Docker${NC}" + exit 1 +fi + +# 主循环 +for distro in "${DISTROS[@]}"; do + test_distro "$distro" +done + +echo -e "\n${GREEN}所有测试完成${NC}" \ No newline at end of file From f25455f21ac311934edeec715bd90e542f71a1b2 Mon Sep 17 00:00:00 2001 From: mymistoe <1105579357@qq.com> Date: Mon, 7 Apr 2025 03:26:05 +0800 Subject: [PATCH 9/9] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + test_script.sh | 195 ------------------------------------------------- 2 files changed, 1 insertion(+), 195 deletions(-) create mode 100644 .gitignore delete mode 100644 test_script.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test_script.sh b/test_script.sh deleted file mode 100644 index 9e4bb0c..0000000 --- a/test_script.sh +++ /dev/null @@ -1,195 +0,0 @@ -#!/bin/bash - -# 支持的Linux发行版 -DISTROS=( - # Ubuntu系列 - # "ubuntu:18.04" # 已测试 - # "ubuntu:20.04" # 已测试 - # "ubuntu:22.04" # 已测试 - # "ubuntu:23.10" # 已测试 - "ubuntu:24.04" - - # Debian系列 - "debian:9" - "debian:10" - "debian:11" - "debian:12" - - # Kali Linux - "kalilinux/kali-rolling:latest" - - # Arch Linux - "archlinux:latest" - - # Fedora - "fedora:38" - "fedora:39" - - # CentOS - "centos:7" - "centos:8" - - # Rocky Linux - "rockylinux:8" - "rockylinux:9" - - # AlmaLinux - "almalinux:8" - "almalinux:9" - - # OpenSUSE - "opensuse/leap:15.5" - "opensuse/tumbleweed:latest" -) - -# 错误处理函数 -handle_error() { - local distro=$1 - local step=$2 - local error=$3 - echo -e "\n${RED}错误: $distro 在 $step 步骤失败${NC}" - echo -e "${YELLOW}错误信息:${NC}" - echo "$error" - echo -e "\n${RED}测试终止${NC}" - exit 1 -} - -# 测试函数 -test_distro() { - local distro=$1 - echo -e "\n${BLUE}开始测试 $distro...${NC}" - - # 根据发行版选择不同的包管理器 - local pkg_manager="" - if [[ $distro == *"ubuntu"* ]] || [[ $distro == *"debian"* ]] || [[ $distro == *"kali"* ]]; then - pkg_manager="apt-get" - elif [[ $distro == *"fedora"* ]] || [[ $distro == *"centos"* ]] || [[ $distro == *"rocky"* ]] || [[ $distro == *"alma"* ]]; then - pkg_manager="dnf" - elif [[ $distro == *"arch"* ]]; then - pkg_manager="pacman" - elif [[ $distro == *"opensuse"* ]]; then - pkg_manager="zypper" - fi - - # 创建并运行容器 - local output=$(docker run -it --rm --init \ - -e DEBIAN_FRONTEND=noninteractive \ - -e TZ=Asia/Shanghai \ - -v $(pwd):/pwn-env \ - -w /pwn-env \ - $distro \ - bash -c " - # 设置时区 - ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime - - # 更新系统 - echo '${BLUE}正在更新系统...${NC}' - if ! $pkg_manager update -y; then - echo '系统更新失败' - exit 1 - fi - - # 安装基本工具 - echo '${BLUE}正在安装基本工具...${NC}' - if [[ $pkg_manager == \"apt-get\" ]]; then - if ! $pkg_manager install -y sudo git curl wget vim build-essential python3 python3-pip python3-dev tzdata; then - echo '基本工具安装失败' - exit 1 - fi - elif [[ $pkg_manager == \"dnf\" ]]; then - if ! $pkg_manager install -y sudo git curl wget vim gcc gcc-c++ make python3 python3-pip python3-devel tzdata; then - echo '基本工具安装失败' - exit 1 - fi - elif [[ $pkg_manager == \"pacman\" ]]; then - if ! $pkg_manager -Syu --noconfirm sudo git curl wget vim base-devel python python-pip tzdata; then - echo '基本工具安装失败' - exit 1 - fi - elif [[ $pkg_manager == \"zypper\" ]]; then - if ! $pkg_manager install -y sudo git curl wget vim gcc gcc-c++ make python3 python3-pip python3-devel timezone; then - echo '基本工具安装失败' - exit 1 - fi - fi - - # 安装shell环境 - echo '${BLUE}正在安装shell环境...${NC}' - if [[ $pkg_manager == \"apt-get\" ]]; then - if ! $pkg_manager install -y zsh fish; then - echo 'shell环境安装失败' - exit 1 - fi - elif [[ $pkg_manager == \"dnf\" ]]; then - if ! $pkg_manager install -y zsh fish; then - echo 'shell环境安装失败' - exit 1 - fi - elif [[ $pkg_manager == \"pacman\" ]]; then - if ! $pkg_manager -S --noconfirm zsh fish; then - echo 'shell环境安装失败' - exit 1 - fi - elif [[ $pkg_manager == \"zypper\" ]]; then - if ! $pkg_manager install -y zsh fish; then - echo 'shell环境安装失败' - exit 1 - fi - fi - - # 配置Python环境 - echo '${BLUE}正在配置Python环境...${NC}' - if ! pip3 install --upgrade pip; then - echo 'pip升级失败' - exit 1 - fi - if ! pip3 install pwntools; then - echo 'pwntools安装失败' - exit 1 - fi - - # 配置shell环境 - echo '${BLUE}正在配置shell环境...${NC}' - if command -v zsh &> /dev/null; then - if ! sh -c \"\$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)\" \"\" --unattended; then - echo 'oh-my-zsh安装失败' - exit 1 - fi - fi - - # 运行测试脚本 - echo '${BLUE}正在运行测试脚本...${NC}' - chmod +x pwn_init.sh - if ! ./pwn_init.sh; then - echo '测试脚本运行失败' - exit 1 - fi - " 2>&1) - - local exit_code=$? - if [ $exit_code -ne 0 ]; then - handle_error "$distro" "测试过程" "$output" - else - echo -e "${GREEN}$distro 测试通过${NC}" - fi -} - -# 颜色定义 -RED='\033[0;31m' -GREEN='\033[0;32m' -BLUE='\033[0;34m' -YELLOW='\033[1;33m' -NC='\033[0m' # No Color - -# 检查Docker是否安装 -if ! command -v docker &> /dev/null; then - echo -e "${RED}错误: 请先安装Docker${NC}" - exit 1 -fi - -# 主循环 -for distro in "${DISTROS[@]}"; do - test_distro "$distro" -done - -echo -e "\n${GREEN}所有测试完成${NC}" \ No newline at end of file