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
| user@honeypot:/opt# cat jail_v1
FILE *logfile; char current_dir[MAX_PATH] = "";
// 记录操作日志 void log_activity(const char *input, const char *output) { if (!logfile) return; time_t now = time(NULL); struct tm *t = localtime(&now); fprintf(logfile, "[%04d-%02d-%02d %02d:%02d:%02d] IN: %s\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, input); if (output && strlen(output) > 0) { fprintf(logfile, "OUT: %s\n\n", output); } fflush(logfile); }
// 检查命令是否被允许 int is_command_allowed(const char *command) { const char *allowed[] = { "ls", "cd", "cat", "pwd", "ps", "top", "free", "df", "ifconfig", "ip", "whoami", "uname", "echo", "id", "history", "help", "clear", "exit", "logout", NULL }; for (int i = 0; allowed[i]; i++) { if (strcmp(command, allowed[i]) == 0) { return 1; } } return 0; }
// 检查命令是否试图修改文件系统 int is_file_modification_command(const char *command) { const char *modifiers[] = { ">", ">>", "<", "|", "&", ";", "rm", "mv", "cp", "touch", "mkdir", "chmod", "chown", "nano", "vi", "vim", ">", ">>", "tee", "dd", "tar", "gzip", "zip", "unzip", "sed", "awk", "find", "git", "svn", "wget", "curl", "scp", "rsync", NULL }; for (int i = 0; modifiers[i]; i++) { if (strstr(command, modifiers[i])) { return 1; } } return 0; }
// 过滤输出中的xxxx敏感信息 void filter_xxxx_output(char *output) { char *patterns[] = { "xxxx", "xxxxx", "xxxxx", "xxxxxxxxx", "xxxxxxxx", "/xxxx", "xxxxxxxxxxx", "xxxxxxxxxxxx", NULL }; for (int i = 0; patterns[i]; i++) { char *pos = output; while ((pos = strstr(pos, patterns[i]))) { memset(pos, 'x', strlen(patterns[i])); pos += strlen(patterns[i]); } } }
// 执行命令并获取输出 void execute_command(const char *input, char *output) { // 检查命令是否被允许 char command_copy[MAX_CMD_LEN]; strncpy(command_copy, input, MAX_CMD_LEN); char *first_token = strtok(command_copy, " "); if (!first_token || !is_command_allowed(first_token)) { snprintf(output, MAX_OUTPUT, "-bash: %s: command not found", input); return; } // 检查文件修改操作 if (is_file_modification_command(input)) { snprintf(output, MAX_OUTPUT, "-bash: %s: Permission denied", input); return; } // 创建管道 int pipefd[2]; if (pipe(pipefd) == -1) { snprintf(output, MAX_OUTPUT, "pipe error: %s", strerror(errno)); return; } pid_t pid = fork(); if (pid < 0) { snprintf(output, MAX_OUTPUT, "fork error: %s", strerror(errno)); close(pipefd[0]); close(pipefd[1]); return; } if (pid == 0) { // 子进程 close(pipefd[0]); // 关闭读端 // 重定向标准输出和错误输出到管道 dup2(pipefd[1], STDOUT_FILENO); dup2(pipefd[1], STDERR_FILENO); close(pipefd[1]); // 解析命令参数 char *args[64]; int i = 0; char *token = strtok((char *)input, " "); while (token != NULL && i < 63) { args[i++] = token; token = strtok(NULL, " "); } args[i] = NULL; // 执行命令 execvp(args[0], args); // 如果execvp失败 fprintf(stderr, "execvp failed: %s", strerror(errno)); exit(EXIT_FAILURE); } else { // 父进程 close(pipefd[1]); // 关闭写端 // 读取命令输出 output[0] = '\0'; char buffer[256]; ssize_t count; while ((count = read(pipefd[0], buffer, sizeof(buffer) - 1)) > 0) { buffer[count] = '\0'; if (strlen(output) + count < MAX_OUTPUT - 1) { strcat(output, buffer); } else { strncat(output, buffer, MAX_OUTPUT - strlen(output) - 1); break; } } close(pipefd[0]); // 等待子进程结束 waitpid(pid, NULL, 0); // 过滤xxxx敏感信息 filter_xxxx_output(output); } }
// 初始化日志文件 int init_logging() { // 检查日志文件是否存在 if (access(LOG_PATH, F_OK) != 0) { return 0; } // 确保日志目录存在 mkdir("/var/www", 0777); mkdir("/var/www/html", 0777); // 打开日志文件 logfile = fopen(LOG_PATH, "a"); if (logfile == NULL) { return -1; } // 设置文件权限 chmod(LOG_PATH, 0644); return 1; }
// 启动真实的 shell void launch_real_shell() { printf("Warning: Log file not found, launching real shell environment\n"); printf("System maintenance mode activated\n"); // 执行真实的 shell execl("/bin/sh", "sh", NULL); exit(0); }
int main() { char input[MAX_CMD_LEN]; char output[MAX_OUTPUT]; // 初始化当前目录 if (getcwd(current_dir, sizeof(current_dir)) == NULL) { strcpy(current_dir, "/"); } // 初始化日志 - 检查文件是否存在 int log_status = init_logging(); // 如果日志文件不存在,启动真实 shell if (log_status == 0) { launch_real_shell(); return 0; } else if (log_status == -1) { fprintf(stderr, "Critical error: Failed to initialize logging system\n"); return 1; } // 清屏 printf("\033[H\033[J"); // 显示登录横幅 printf("Honeypot Terminal v2.0 - Restricted Environment\n"); printf("Last login: %s from 192.168.1.123\n", ctime(&(time_t){time(NULL) - 3600})); // 主循环 while (1) { // 打印提示符 printf("\033[1;32muser@honeypot\033[0m:\033[1;34m%s\033[0m# ", strcmp(current_dir, "/xxxx") == 0 ? "~" : current_dir); fflush(stdout); // 读取用户输入 if (fgets(input, sizeof(input), stdin) == NULL) { break; } // 移除换行符 input[strcspn(input, "\n")] = '\0'; // 跳过空输入 if (strlen(input) == 0) { continue; } // 记录输入 log_activity(input, NULL); // 特殊处理cd命令 if (strncmp(input, "cd", 2) == 0) { char *path = strchr(input, ' '); if (path) { path++; if (chdir(path) != 0) { snprintf(output, MAX_OUTPUT, "bash: cd: %s: %s", path, strerror(errno)); } else { getcwd(current_dir, sizeof(current_dir)); output[0] = '\0'; } } else { chdir("/"); getcwd(current_dir, sizeof(current_dir)); output[0] = '\0'; } } // 特殊处理exit/logout else if (strcmp(input, "exit") == 0 || strcmp(input, "logout") == 0) { strcpy(output, "logout"); printf("%s\n", output); log_activity(input, output); break; } // 特殊处理clear else if (strcmp(input, "clear") == 0) { printf("\033[H\033[J"); output[0] = '\0'; } // 处理其他命令 else { execute_command(input, output); printf("%s\n", output); } // 记录输出 log_activity(input, output); }
|