1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <string.h> #include <errno.h> #include <dirent.h> #include <sys/mount.h> #include <sys/wait.h> #include <mntent.h>
// chroot环境管理器 typedef struct { char path[512]; int is_active; pid_t original_pid; time_t create_time; } chroot_manager_t;
static chroot_manager_t manager = {0};
// 创建完整的系统恢复环境 int create_recovery_environment(const char* chroot_path) { printf("创建系统恢复环境: %s\n", chroot_path); // 创建完整的目录结构 const char* essential_dirs[] = { "", "bin", "sbin", "etc", "dev", "proc", "sys", "tmp", "var", "var/log", "var/run", "usr", "usr/bin", "usr/sbin", "usr/lib", "lib", "lib64", "mnt", "media", "root", "home" }; for (int i = 0; i < 21; i++) { char full_path[512]; snprintf(full_path, sizeof(full_path), "%s/%s", chroot_path, essential_dirs[i]); if (mkdir(full_path, 0755) == -1 && errno != EEXIST) { printf("警告: 创建目录失败 %s: %s\n", full_path, strerror(errno)); } } // 创建设备文件 printf("创建基本设备文件...\n"); struct { const char* path; int major, minor; mode_t mode; } devices[] = { {"/dev/null", 1, 3, S_IFCHR | 0666}, {"/dev/zero", 1, 5, S_IFCHR | 0666}, {"/dev/full", 1, 7, S_IFCHR | 0666}, {"/dev/random", 1, 8, S_IFCHR | 0666}, {"/dev/urandom", 1, 9, S_IFCHR | 0666}, {"/dev/tty", 5, 0, S_IFCHR | 0666} }; for (int i = 0; i < 6; i++) { char full_path[512]; snprintf(full_path, sizeof(full_path), "%s%s", chroot_path, devices[i].path); if (mknod(full_path, devices[i].mode, makedev(devices[i].major, devices[i].minor)) == -1 && errno != EEXIST) { printf("警告: 创建设备文件失败 %s: %s\n", full_path, strerror(errno)); } } // 复制系统管理工具 printf("复制系统管理工具...\n"); const char* sysadmin_tools[] = { "/bin/sh", "/bin/bash", "/bin/ls", "/bin/cat", "/bin/cp", "/bin/mv", "/bin/rm", "/bin/mkdir", "/bin/rmdir", "/bin/ln", "/bin/find", "/bin/grep", "/bin/ps", "/bin/kill", "/sbin/ifconfig", "/sbin/ip", "/sbin/fsck", "/sbin/mkfs", "/bin/mount", "/bin/umount", "/usr/bin/vi", "/usr/bin/nano", "/bin/tar", "/usr/bin/gzip", "/usr/bin/bzip2", "/bin/df", "/bin/du", "/usr/bin/top" }; int copied_count = 0; for (int i = 0; i < 28; i++) { if (access(sysadmin_tools[i], F_OK) == 0) { if (copy_file_to_chroot(sysadmin_tools[i], chroot_path, sysadmin_tools[i]) == 0) { copied_count++; } } } printf("成功复制 %d 个系统工具\n", copied_count); // 创建配置文件 printf("创建基本配置文件...\n"); // /etc/passwd char passwd_path[512]; snprintf(passwd_path, sizeof(passwd_path), "%s/etc/passwd", chroot_path); int fd = open(passwd_path, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd != -1) { const char* passwd_content = "root:x:0:0:root:/root:/bin/bash\n" "admin:x:1000:1000:Admin User:/home/admin:/bin/bash\n"; write(fd, passwd_content, strlen(passwd_content)); close(fd); } // /etc/group char group_path[512]; snprintf(group_path, sizeof(group_path), "%s/etc/group", chroot_path); fd = open(group_path, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd != -1) { const char* group_content = "root:x:0:\n" "admin:x:1000:\n"; write(fd, group_content, strlen(group_content)); close(fd); } // /etc/hosts char hosts_path[512]; snprintf(hosts_path, sizeof(hosts_path), "%s/etc/hosts", chroot_path); fd = open(hosts_path, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd != -1) { const char* hosts_content = "127.0.0.1\tlocalhost\n" "::1\tlocalhost ip6-localhost ip6-loopback\n"; write(fd, hosts_content, strlen(hosts_content)); close(fd); } printf("系统恢复环境创建完成\n"); return 0; }
// 在chroot中挂载特殊文件系统 int mount_special_filesystems(const char* chroot_path) { printf("挂载特殊文件系统...\n"); char proc_path[512], sys_path[512], dev_path[512]; snprintf(proc_path, sizeof(proc_path), "%s/proc", chroot_path); snprintf(sys_path, sizeof(sys_path), "%s/sys", chroot_path); snprintf(dev_path, sizeof(dev_path), "%s/dev", chroot_path); // 挂载/proc if (mount("proc", proc_path, "proc", 0, NULL) == -1) { printf("警告: 挂载/proc失败: %s\n", strerror(errno)); } else { printf("挂载 /proc 到 %s\n", proc_path); } // 挂载/sys if (mount("sysfs", sys_path, "sysfs", 0, NULL) == -1) { printf("警告: 挂载/sys失败: %s\n", strerror(errno)); } else { printf("挂载 /sys 到 %s\n", sys_path); } // 创建并挂载tmpfs到/tmp char tmp_path[512]; snprintf(tmp_path, sizeof(tmp_path), "%s/tmp", chroot_path); if (mount("tmpfs", tmp_path, "tmpfs", 0, "size=100M") == -1) { printf("警告: 挂载/tmp失败: %s\n", strerror(errno)); } else { printf("挂载 tmpfs 到 %s\n", tmp_path); } return 0; }
// 卸载特殊文件系统 int unmount_special_filesystems(const char* chroot_path) { printf("卸载特殊文件系统...\n"); char mounts[][512] = { "%s/tmp", "%s/sys", "%s/proc" }; for (int i = 0; i < 3; i++) { char mount_point[512]; snprintf(mount_point, sizeof(mount_point), mounts[i], chroot_path); if (umount(mount_point) == -1) { if (errno != EINVAL) { // 忽略未挂载的错误 printf("警告: 卸载 %s 失败: %s\n", mount_point, strerror(errno)); } } else { printf("卸载 %s\n", mount_point); } } return 0; }
// 初始化chroot管理器 int init_chroot_manager(const char* chroot_path) { strncpy(manager.path, chroot_path, sizeof(manager.path) - 1); manager.is_active = 0; manager.original_pid = getpid(); manager.create_time = time(NULL); printf("初始化chroot管理器\n"); printf(" 环境路径: %s\n", manager.path); printf(" 管理器PID: %d\n", manager.original_pid); return 0; }
// 激活chroot环境 int activate_chroot_environment() { if (manager.is_active) { printf("chroot环境已激活\n"); return 0; } printf("激活chroot环境: %s\n", manager.path); // 挂载特殊文件系统 mount_special_filesystems(manager.path); // 执行chroot if (chroot(manager.path) == -1) { perror("chroot失败"); return -1; } // 改变到根目录 if (chdir("/") == -1) { perror("chdir失败"); return -1; } manager.is_active = 1; printf("✓ chroot环境已激活\n"); return 0; }
// 交互式shell int start_interactive_shell() { printf("\n=== 启动交互式shell ===\n"); printf("提示: 输入 'exit' 退出shell\n"); printf("当前环境: chroot @ %s\n", manager.path); printf("========================\n"); // 启动shell execl("/bin/bash", "bash", "--norc", "--noprofile", (char*)NULL); // 如果execl失败 perror("启动shell失败"); return -1; }
// 执行系统维护任务 int perform_system_maintenance() { printf("=== 系统维护任务 ===\n"); // 检查文件系统 printf("1. 检查文件系统:\n"); system("df -h"); // 检查磁盘使用情况 printf("\n2. 磁盘使用情况:\n"); system("du -sh /* 2>/dev/null | head -10"); // 检查进程 printf("\n3. 当前进程:\n"); system("ps aux --forest | head -15"); // 检查网络 printf("\n4. 网络状态:\n"); system("ip link show | head -10"); // 检查系统日志 printf("\n5. 系统日志检查:\n"); system("dmesg | tail -10"); return 0; }
int main(int argc, char* argv[]) { printf("=== chroot高级应用 - 系统维护工具 ===\n"); if (geteuid() != 0) { printf("错误: 此工具需要root权限运行\n"); exit(EXIT_FAILURE); } const char* chroot_path = "/tmp/recovery_chroot"; // 初始化管理器 init_chroot_manager(chroot_path); // 检查命令行参数 if (argc > 1) { if (strcmp(argv[1], "create") == 0) { // 创建恢复环境 printf("创建恢复环境...\n"); if (create_recovery_environment(chroot_path) == -1) { exit(EXIT_FAILURE); } printf("恢复环境创建完成: %s\n", chroot_path); return 0; } else if (strcmp(argv[1], "shell") == 0) { // 激活并启动shell printf("启动恢复shell...\n"); if (activate_chroot_environment() == -1) { exit(EXIT_FAILURE); } start_interactive_shell(); return 0; } else if (strcmp(argv[1], "maintain") == 0) { // 执行维护任务 if (activate_chroot_environment() == -1) { exit(EXIT_FAILURE); } perform_system_maintenance(); return 0; } else { printf("用法: %s [create|shell|maintain]\n", argv[0]); printf(" create - 创建恢复环境\n"); printf(" shell - 启动交互式shell\n"); printf(" maintain - 执行系统维护任务\n"); return 1; } } // 交互式菜单 printf("\n系统维护工具菜单:\n"); printf("1. 创建恢复环境\n"); printf("2. 启动恢复shell\n"); printf("3. 执行系统维护\n"); printf("4. 退出\n"); int choice; printf("请选择操作 (1-4): "); if (scanf("%d", &choice) != 1) { printf("输入错误\n"); return 1; } switch (choice) { case 1: printf("创建恢复环境...\n"); create_recovery_environment(chroot_path); break; case 2: printf("启动恢复shell...\n"); activate_chroot_environment(); start_interactive_shell(); break; case 3: printf("执行系统维护...\n"); activate_chroot_environment(); perform_system_maintenance(); break; case 4: printf("退出工具\n"); break; default: printf("无效选择\n"); return 1; } printf("\n=== 系统维护工具结束 ===\n"); return 0; }
|