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
| #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <pwd.h> #include <grp.h> #include <time.h>
typedef struct { uid_t ruid, euid, suid; gid_t rgid, egid, sgid; time_t timestamp; pid_t pid; } identity_snapshot_t;
int capture_identity_snapshot(identity_snapshot_t *snapshot) { snapshot->pid = getpid(); snapshot->timestamp = time(NULL); if (getresuid(&snapshot->ruid, &snapshot->euid, &snapshot->suid) == -1 || getresgid(&snapshot->rgid, &snapshot->egid, &snapshot->sgid) == -1) { return -1; } return 0; }
void print_identity_snapshot(const identity_snapshot_t *snapshot) { char time_str[32]; strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S", localtime(&snapshot->timestamp)); printf("时间: %s\n", time_str); printf("进程: %d\n", snapshot->pid); printf("用户 ID: %d/%d/%d (真实/有效/保存)\n", snapshot->ruid, snapshot->euid, snapshot->suid); printf("组 ID: %d/%d/%d (真实/有效/保存)\n", snapshot->rgid, snapshot->egid, snapshot->sgid); struct passwd *pwd = getpwuid(snapshot->ruid); if (pwd) printf("真实用户: %s\n", pwd->pw_name); pwd = getpwuid(snapshot->euid); if (pwd) printf("有效用户: %s\n", pwd->pw_name); struct group *grp = getgrgid(snapshot->rgid); if (grp) printf("真实组: %s\n", grp->gr_name); grp = getgrgid(snapshot->egid); if (grp) printf("有效组: %s\n", grp->gr_name); }
void security_audit() { identity_snapshot_t snapshot; printf("=== 安全审计报告 ===\n"); if (capture_identity_snapshot(&snapshot) == -1) { printf("获取身份信息失败\n"); return; } print_identity_snapshot(&snapshot); printf("\n安全检查结果:\n"); if (snapshot.euid == 0) { printf("⚠ 警告: 进程以 root 权限运行\n"); } if (snapshot.egid == 0) { printf("⚠ 警告: 进程具有 root 组权限\n"); } if (snapshot.ruid != snapshot.euid) { printf("ℹ 信息: 用户权限已被切换\n"); } if (snapshot.rgid != snapshot.egid) { printf("ℹ 信息: 组权限已被切换\n"); } if (snapshot.suid == 0 && snapshot.euid != 0) { printf("ℹ 信息: 保存了 root 用户权限,可用于恢复\n"); } if (snapshot.sgid == 0 && snapshot.egid != 0) { printf("ℹ 信息: 保存了 root 组权限,可用于恢复\n"); } if ((snapshot.ruid != snapshot.euid || snapshot.rgid != snapshot.egid) && (snapshot.suid == snapshot.ruid && snapshot.sgid == snapshot.rgid)) { printf("✓ 安全: 可以完全恢复到原始身份\n"); } }
void monitor_privilege_changes() { printf("\n=== 权限变化监控 ===\n"); identity_snapshot_t initial, current; if (capture_identity_snapshot(&initial) == -1) { printf("无法获取初始身份信息\n"); return; } printf("监控 5 秒钟内的权限变化...\n"); for (int i = 0; i < 5; i++) { sleep(1); if (capture_identity_snapshot(¤t) == 0) { if (current.ruid != initial.ruid || current.euid != initial.euid || current.suid != initial.suid || current.rgid != initial.rgid || current.egid != initial.egid || current.sgid != initial.sgid) { printf("检测到权限变化:\n"); printf("之前: UID(%d/%d/%d) GID(%d/%d/%d)\n", initial.ruid, initial.euid, initial.suid, initial.rgid, initial.egid, initial.sgid); printf("现在: UID(%d/%d/%d) GID(%d/%d/%d)\n", current.ruid, current.euid, current.suid, current.rgid, current.egid, current.sgid); initial = current; } } } printf("监控结束\n"); }
int main() { security_audit(); monitor_privilege_changes(); return 0; }
|