bloc_note
Ceci est une ancienne révision du document !
Bloc Note rapide
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> // Liste des scripts autorisés const char *authorized_scripts[] = { "/path/to/authorized_script1.sh", "/path/to/authorized_script2.sh", NULL // Marqueur de fin de liste }; // Fonction pour vérifier si un script est autorisé int is_script_authorized(const char *script_path) { for (int i = 0; authorized_scripts[i] != NULL; i++) { if (strcmp(script_path, authorized_scripts[i]) == 0) { return 1; // Trouvé dans la liste } } return 0; // Non autorisé } void check_parent_script() { pid_t ppid = getppid(); // Obtenir le PPID (Parent Process ID) char path[256]; char cmdline[1024] = {0}; FILE *file; // Construire le chemin vers /proc/[PPID]/cmdline snprintf(path, sizeof(path), "/proc/%d/cmdline", ppid); // Ouvrir le fichier cmdline pour lire la commande du parent file = fopen(path, "r"); if (!file) { perror("Error: Unable to open parent process info."); exit(1); } // Lire le contenu de cmdline if (fgets(cmdline, sizeof(cmdline), file) != NULL) { fclose(file); // Extraire le chemin du script (premier argument après le shell) char *script_path = strchr(cmdline, '\0') + 1; if (*script_path != '\0') { // Valider le script if (!is_script_authorized(script_path)) { fprintf(stderr, "Error: Unauthorized script '%s' tried to launch this program.\n", script_path); exit(1); } } else { fprintf(stderr, "Error: This program must be launched by a shell script.\n"); exit(1); } } else { fclose(file); perror("Error: Unable to read parent process command line."); exit(1); } } int main(int argc, char *argv[]) { // Vérifier le script parent check_parent_script(); printf("Program successfully launched by an authorized script.\n"); // Logique principale du programme ici return 0; }
bloc_note.1734503349.txt.gz · Dernière modification : 2024/12/18 07:29 de admin