diff --git a/.vscode/settings.json b/.vscode/settings.json index 2e29463..e8731cb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,6 +8,7 @@ "*.wxs": "javascript", "*.json": "jsonc", "string.h": "c", - "shm.h": "c" + "shm.h": "c", + "unistd.h": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index bbb60c6..da84008 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC = gcc -CFLAGS = -shared -fPIC -Wall -Wextra -Werror +CFLAGS = -shared -fPIC -Wall -Wextra -Werror -O2 -fno-strict-aliasing -fPIC -fno-omit-frame-pointer -fno-stack-protector -Wl,-z,relro,-z,now LDFLAGS = -ldl -ljson-c TARGET = intercept.so SRC = execve_intercept.c @@ -15,4 +15,8 @@ $(TARGET): $(SRC) $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) clean: - rm -f $(TARGET) \ No newline at end of file + rm -f $(TARGET) + +debug: + $(MAKE) DEBUG=1 + \ No newline at end of file diff --git a/intercept.so b/intercept.so index 7dc80e5..f9bebb3 100755 Binary files a/intercept.so and b/intercept.so differ diff --git a/tests/a.out b/tests/a.out deleted file mode 100755 index 727cc87..0000000 Binary files a/tests/a.out and /dev/null differ diff --git a/tests/output.txt b/tests/output.txt deleted file mode 100644 index e69de29..0000000 diff --git a/tests/shm_reader.c b/tests/shm_reader.c new file mode 100644 index 0000000..fe254f3 --- /dev/null +++ b/tests/shm_reader.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define SHM_KEY 12345 +#define MAX_RULES 100 +#define MAX_ARGS 10 + +typedef struct { + char cmd[256]; + char type[32]; + char msg[1024]; + char args[MAX_ARGS][256]; + int arg_count; +} Rule; + +typedef struct { + bool enabled; + Rule rules[MAX_RULES]; + int rule_count; +} ConfigData; + +void print_config(const ConfigData *cfg) { + printf("=== Config ===\n"); + printf("Enabled: %s\n", cfg->enabled ? "true" : "false"); + printf("Rule count: %d\n", cfg->rule_count); + for (int i = 0; i < cfg->rule_count && i < MAX_RULES; i++) { + const Rule *r = &cfg->rules[i]; + printf("Rule %d:\n", i + 1); + printf(" CMD : %s\n", r->cmd); + printf(" Type: %s\n", r->type); + printf(" Msg : %s\n", r->msg); + printf(" Args(%d):\n", r->arg_count); + for (int j = 0; j < r->arg_count && j < MAX_ARGS; j++) { + printf(" - %s\n", r->args[j]); + } + } + printf("=====================\n\n"); +} + +int main() { + int shmid = shmget(SHM_KEY, sizeof(ConfigData), 0666); + if (shmid < 0) { + perror("shmget failed"); + return 1; + } + + ConfigData *shared_cfg = (ConfigData *)shmat(shmid, NULL, SHM_RDONLY); + if (shared_cfg == (void *)-1) { + perror("shmat failed"); + return 1; + } + + ConfigData last_cfg = {0}; + + struct timespec ts; + ts.tv_sec = 0; + ts.tv_nsec = 100 * 1000000; // 100ms + + while (1) { + if (memcmp(&last_cfg, shared_cfg, sizeof(ConfigData)) != 0) { + printf(">>> Config changed:\n"); + print_config(shared_cfg); + memcpy(&last_cfg, shared_cfg, sizeof(ConfigData)); + } + nanosleep(&ts, NULL); // 每 100 毫秒检查一次 + } + + shmdt(shared_cfg); + return 0; +}