Code afférent au projet Kouglof 2 de l'E2L
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

1194 lignes
32 KiB

  1. /*******************************************************************
  2. Copyright (C) 2011-2024 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or any
  6. later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>
  13. *******************************************************************/
  14. /*
  15. ############################################################
  16. # Projet Kouglof 2 de l'Ecole du Logiciel Libre d'Ivry : #
  17. ############################################################
  18. octave.c : outil pour scanner l'interface reseau afin d'analyser les sites
  19. auxquels les applications veulent se connecter.
  20. A utiliser avec le fichier auth1.txt pour stopper les connexions non voulues
  21. Tous les details sur le site :
  22. https://e2li.org -> menu : Projet Prosecco.
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include <signal.h>
  29. #include <fcntl.h>
  30. #include <readline/readline.h>
  31. #include <readline/history.h>
  32. #include <sys/wait.h>
  33. #include <string.h>
  34. #include <syslog.h>
  35. #include <time.h>
  36. #include <arpa/inet.h>
  37. #include <pthread.h>
  38. #define Version "1.01"
  39. #define F_GETPIPE_SZ 1032
  40. #define F_SETPIPE_SZ 1031
  41. static int RUN=1, REQ=0, ENDT=0, REPR=0, JCTL=0, LogC=0, WH=1, p1[2],Trace=0;
  42. static pid_t pid;
  43. static char * NPROG, *IFACE=NULL;
  44. #define DELAYR 20 /* delai avant relance auto */
  45. void interup (int S)
  46. {
  47. if (S==SIGINT) {
  48. write(p1[1],"\n",1);
  49. REQ=1; return;
  50. }
  51. if (S==SIGCHLD) {
  52. if (waitpid(pid,NULL,WNOHANG) == pid) {
  53. ENDT=1;
  54. write(p1[1],"\n",1);
  55. }
  56. return;
  57. }
  58. fprintf(stderr,"Reçu signal %d !!??\n",S);
  59. }
  60. /* ### les niveaux de trace */
  61. #define TMIN 0
  62. #define TMAX 3
  63. #define T1 Trace > 0
  64. #define T2 Trace > 1
  65. #define T3 Trace > 2
  66. /* #### les fonctions adresses IPv4 */
  67. int isIPv4(char *a)
  68. {
  69. struct in_addr S;
  70. int r;
  71. if ((r = inet_pton(AF_INET,a, (void *)&S)) <= 0) return 0;
  72. return 1;
  73. }
  74. int isIPv6(char *a)
  75. {
  76. struct in6_addr S;
  77. int r;
  78. if ((r = inet_pton(AF_INET6,a, (void *)&S)) <= 0) return 0;
  79. return 1;
  80. }
  81. int isCidr(char*r)
  82. {
  83. char buf[20], *sn;
  84. int n;
  85. if (strlen(r)>18) return 0;
  86. strcpy(buf,r);
  87. if ((sn=strstr(buf,"/")) == NULL) return 0;
  88. *sn = '\0';
  89. if (!isIPv4(buf)) return 0;
  90. n = atoi(sn+1);
  91. if (n>32) return 0;
  92. return n;
  93. }
  94. int isCidr6(char*r)
  95. {
  96. char buf[52], *sn;
  97. int n;
  98. if (strlen(r)>50) return 0;
  99. strcpy(buf,r);
  100. if ((sn=strstr(buf,"/")) == NULL) return 0;
  101. *sn = '\0';
  102. if (!isIPv6(buf)) return 0;
  103. n = atoi(sn+1);
  104. if (n>128) return 0;
  105. return n;
  106. }
  107. uint64_t NbAddCidrs = 0;
  108. int isSousRes(char*r, char *a)
  109. {
  110. struct in_addr Sa, Sr, Sm;
  111. int n, m1,m2,m3,m4, m;
  112. char buf[20],smasq[16],*sn;
  113. if (!isCidr(r)) return 0;
  114. if (!isIPv4(a)) return 0;
  115. strcpy(buf,r);
  116. if ((sn=strstr(buf,"/")) == NULL) return 0;
  117. *sn = '\0';
  118. n = atoi(sn+1);
  119. /* calcul du masq */
  120. m=n;
  121. if (m>7) { m1=255; m-=8;
  122. } else { m1=0;
  123. while (m>=0) m1 |= 0x80 >> --m;
  124. }
  125. if (m>7) { m2=255; m-=8;
  126. } else { m2=0;
  127. while (m>=0) m2 |= 0x80 >> --m;
  128. }
  129. if (m>7) { m3=255; m-=8;
  130. } else { m3=0;
  131. while (m>=0) m3 |= 0x80 >> --m;
  132. }
  133. if (m>7) { m4=255; m-=8;
  134. } else { m4=0;
  135. while (m>=0) m4 |= 0x80 >> --m;
  136. }
  137. sprintf(smasq,"%d.%d.%d.%d",m1,m2,m3,m4);
  138. inet_pton(AF_INET,smasq, (void *)&Sm);
  139. inet_pton(AF_INET,a, (void *)&Sa);
  140. inet_pton(AF_INET,buf, (void *)&Sr);
  141. if ((Sr.s_addr & Sm.s_addr) == (Sa.s_addr & Sm.s_addr)) return 1;
  142. return 0;
  143. }
  144. /* #### gestion dynamique des CIDR */
  145. #define NBC 100 /* nb de CIDR */
  146. char* Tcidr[NBC];
  147. int iC=0;
  148. int bloqueIP(char*);
  149. int addCidr(char * c)
  150. {
  151. int i=iC;
  152. if (i==NBC) return i;
  153. Tcidr[i] = (char*)malloc(strlen(c)+1);
  154. strcpy(Tcidr[i],c);
  155. iC++;
  156. bloqueIP(c);
  157. return i;
  158. }
  159. void delCidr(char * c)
  160. {
  161. int i;
  162. for (i=0; i<iC; i++)
  163. if (strcmp(c,Tcidr[i]) == 0) {
  164. iC--;
  165. if (iC == i) return;
  166. if (iC > 0) Tcidr[i] = Tcidr[iC];
  167. return;
  168. }
  169. }
  170. int isAddrInCidr(char * a, int M)
  171. {
  172. int i;
  173. for(i=0;i<iC;i++) {
  174. if (isSousRes(Tcidr[i],a)) {
  175. if (M) syslog(LOG_INFO,"CIDR %s contient %s !",Tcidr[i],a);
  176. return 1;
  177. }
  178. }
  179. return 0;
  180. }
  181. void validCidr(void)
  182. {
  183. int i,j;
  184. char a[20],*p, *g, *w;
  185. for(i=0;i<iC;i++)
  186. for (j=i+1;j<iC;j++) {
  187. if (isCidr(Tcidr[i]) < isCidr(Tcidr[j])) {
  188. p=Tcidr[i]; g=Tcidr[j];
  189. } else {
  190. p=Tcidr[j]; g=Tcidr[i];
  191. }
  192. strcpy(a,g);
  193. w = strstr(a,"/");
  194. *w = '\0';
  195. w++;
  196. if (isSousRes(p,a)) {
  197. if (T1) printf("T1: %s contient %s (%s)\n",p,a,w);
  198. delCidr(g);
  199. }
  200. }
  201. }
  202. void listCidr(void)
  203. {
  204. int i;
  205. printf("CIDR : %d elts representent %lld adresses.\n",iC,(long long)NbAddCidrs);
  206. for (i=0; i<iC; i++) printf("\t%s\n", Tcidr[i]);
  207. }
  208. /* ### gestion des listes */
  209. #define NBAll 500
  210. #define NBDen 300
  211. char * Allow[NBAll];
  212. char * Deny[NBDen];
  213. int iAll=0, iDen=0;
  214. int isDeny(char*u)
  215. {
  216. char *su;
  217. int i, tu, t;
  218. for (i=0;i<iDen;i++) {
  219. tu = strlen(u);
  220. t = strlen(Deny[i]);
  221. if (tu < t) continue;
  222. su = u + tu - t;;
  223. if (strcmp(su,Deny[i]) == 0) {
  224. if (su==u) return 1;
  225. if (*(su-1)=='.') return 1;
  226. }
  227. }
  228. for (i=0;i<iAll;i++) {
  229. tu = strlen(u);
  230. t = strlen(Allow[i]);
  231. if (tu < t) continue;
  232. su = u + tu - t;
  233. if (strcmp(su,Allow[i]) == 0) {
  234. if (*(Allow[i]) == '.') return 0;
  235. if (su==u) return 0;
  236. if (*(su-1)=='.') return 0;
  237. }
  238. }
  239. return 1; /* deny par defaut */
  240. }
  241. void listeAllow(void)
  242. {
  243. int i;
  244. printf("Allow : %d\n",iAll);
  245. for (i=0;i<iAll;i++) printf("\t%s\n",Allow[i]);
  246. }
  247. void listeDeny(void)
  248. {
  249. int i;
  250. printf("Deny : %d\n",iDen);
  251. for (i=0;i<iDen;i++) printf("\t%s\n",Deny[i]);
  252. }
  253. void dejaLa(char * e)
  254. {
  255. printf("%s est deja dans la liste !\n",e);
  256. }
  257. int dejaAllow(char *e)
  258. {
  259. int i;
  260. for (i=0;i<iAll;i++) {
  261. if (strlen(e) != strlen(Allow[i])) continue;
  262. if (strcmp(e,Allow[i])==0) {
  263. dejaLa(e); return 1;
  264. }
  265. }
  266. return 0;
  267. }
  268. int dejaDeny(char *e)
  269. {
  270. int i;
  271. for (i=0;i<iDen;i++) {
  272. if (strlen(e) != strlen(Deny[i])) continue;
  273. if (strcmp(e,Deny[i])==0) {
  274. dejaLa(e); return 1;
  275. }
  276. }
  277. return 0;
  278. }
  279. void recaplistes(void)
  280. {
  281. listeDeny();
  282. listeAllow();
  283. }
  284. int litligne(char * line)
  285. {
  286. char *w, **S;
  287. void * M;
  288. int t,v;
  289. if (*line == '#') return 1;
  290. if ((w=strstr(line, "\n")) != NULL) *w = '\0';
  291. w=line;
  292. if (*w == '-') w++;
  293. t=strlen(w);
  294. if (t==0) return 1;
  295. if ((v=isCidr(w)) > 0) { /* test si CIDR */
  296. addCidr(w);
  297. NbAddCidrs += (int)(1<<v);
  298. return 1;
  299. }
  300. if ((v=isCidr6(w)) > 0) { /* test si CIDR6 */
  301. printf("%s : CIDR IPv6 non pris en compte pour l'instant !\n",w);
  302. return 1;
  303. }
  304. if (*line == '-') {
  305. if (iDen == NBDen) return 0;
  306. if (dejaDeny(w)) return 0;
  307. S = &Deny[iDen];
  308. iDen++;
  309. } else {
  310. if (iAll == NBAll) return 0;
  311. if (dejaAllow(w)) return 0;
  312. S = &Allow[iAll];
  313. iAll++;
  314. }
  315. if ((M = malloc(t+1)) == NULL) {
  316. perror("malloc"); return 0;
  317. }
  318. *S=(char*)M;
  319. strcpy(*S,w);
  320. return 1;
  321. }
  322. void lectliste(char *f)
  323. {
  324. FILE * fd;
  325. char *line = NULL;
  326. size_t ll = 0;
  327. int n;
  328. if ((fd = fopen(f,"r")) == NULL) {
  329. perror(f); return;
  330. }
  331. while ((n = getline(&line, &ll, fd)) > 0) {
  332. if (!litligne(line)) {
  333. if (T1) printf("T1: Erreur param. = %s\n",line);
  334. }
  335. }
  336. free(line);
  337. fclose(fd);
  338. validCidr();
  339. if (T1) listCidr();
  340. }
  341. /* ### gestion dynamique des elts */
  342. #define NBT 1000 /* nb d'elts */
  343. int Tno[NBT];
  344. int Trv[NBT];
  345. char* Turl[NBT];
  346. int iT=0, NbElt=0, MaxElt=0;
  347. int addElt(int n, char * u)
  348. {
  349. int i=iT;
  350. if (i == NBT) return i;
  351. Tno[i]=n;
  352. Trv[i]=0;
  353. Turl[i] = (char*)malloc(strlen(u)+1);
  354. strcpy(Turl[i],u);
  355. iT++;
  356. NbElt++;
  357. if (NbElt > MaxElt) MaxElt=NbElt;
  358. return i;
  359. }
  360. int isElt(int n)
  361. {
  362. int i;
  363. for (i=0; i<iT; i++) if (n==Tno[i]) return i;
  364. return -1;
  365. }
  366. void delIElt(int i)
  367. {
  368. if (i>=iT) return;
  369. if (T3) printf("T3: Del %d : %s \n",Tno[i],Turl[i]);
  370. iT--;
  371. if (iT == i) return;
  372. if (iT > 0) {
  373. Tno[i] = Tno[iT];
  374. Turl[i] = Turl[iT];
  375. Trv[i] = Trv[iT];
  376. }
  377. return;
  378. }
  379. void delElt(int n)
  380. {
  381. int i;
  382. for (i=0; i<iT; i++)
  383. if (n==Tno[i]) {
  384. delIElt(i);
  385. return;
  386. }
  387. }
  388. int markElt(int i, int v)
  389. {
  390. if (Trv[i] & v) return 0;
  391. Trv[i] |= v;
  392. return 1;
  393. }
  394. void listElt(char c)
  395. {
  396. int i,n=0;
  397. switch (c) {
  398. case '-':
  399. for (i=0; i<iT; i++)
  400. if (Tno[i]<0) { printf("%d : %s (%d)\n",Tno[i], Turl[i], Trv[i]);
  401. n++;
  402. }
  403. break;
  404. case '+':
  405. for (i=0; i<iT; i++)
  406. if (Tno[i]>0) { printf("%d : %s (%d)\n",Tno[i], Turl[i], Trv[i]);
  407. n++;
  408. }
  409. break;
  410. default:
  411. for (i=0; i<iT; i++) {
  412. printf("%d : %s (%d)\n",Tno[i], Turl[i], Trv[i]);
  413. n++;
  414. }
  415. break;
  416. }
  417. printf(" %d elements trouves.\n",n);
  418. }
  419. #define EX_NOOUT 1
  420. #define EX_NOERR 2
  421. #define EX_SILENT EX_NOOUT|EX_NOERR
  422. int comsh(char *com,int mode)
  423. {
  424. pid_t pid;
  425. int ret;
  426. if ((pid = fork()) < 0) {
  427. perror("fork2"); return 99;
  428. }
  429. if (T3) printf("$ %s\n",com);
  430. if (pid == 0) {
  431. if (mode & EX_NOOUT) close(1);
  432. if (mode & EX_NOERR) close(2);
  433. signal(SIGINT,SIG_IGN);
  434. execl("/bin/sh", "sh", "-c", com, (char *) 0);
  435. perror("execl2"); return 98;
  436. }
  437. waitpid(pid,&ret,0);
  438. return WEXITSTATUS(ret);
  439. }
  440. int exeCom(char * comm) /* on se reserve le droit de modifier */
  441. {
  442. char b[120];
  443. sprintf(b,"%s >/dev/null 2>&1",comm);
  444. return comsh(b,EX_SILENT);
  445. }
  446. /* ### fct de MAJ iptables */
  447. static char * IPT = "iptables";
  448. static char * IP6T = "ip6tables";
  449. static char * MYCH = "valide4";
  450. static char * OUTP = "OUTPUT";
  451. static char * MNO = "REJECT";
  452. static char * MOK = "ACCEPT";
  453. int initIPT(void)
  454. {
  455. int i=0;
  456. char b[90];
  457. if (REPR) return 0;
  458. sprintf(b,"%s -F",IPT);
  459. i += exeCom(b);
  460. sprintf(b,"%s -F",IP6T);
  461. i += exeCom(b);
  462. sprintf(b,"%s -L %s -n",IPT,MYCH);
  463. if (exeCom(b)) {
  464. sprintf(b,"%s -N %s",IPT,MYCH);
  465. i += exeCom(b);
  466. }
  467. sprintf(b,"%s -A %s -j %s",IPT,OUTP,MYCH);
  468. i += exeCom(b);
  469. return i;
  470. }
  471. int isPresentIP(char * comm, char * ip, char * chain)
  472. {
  473. char buf[100];
  474. sprintf(buf,"%s -L %s -n|grep %s",comm,chain,ip);
  475. if (exeCom(buf) == 0) return 1;
  476. return 0;
  477. }
  478. int retireChain(char * comm, char * ip, char * chain, char * jump)
  479. {
  480. char buf[100];
  481. sprintf(buf,"%s -D %s -d %s -j %s",comm,chain, ip, jump);
  482. return exeCom(buf);
  483. }
  484. int ajouteChain(char * comm, char * ip, char * chain, char * jump)
  485. {
  486. char buf[100];
  487. sprintf(buf,"%s -A %s -d %s -j %s",comm,chain, ip, jump);
  488. return exeCom(buf);
  489. }
  490. int bloqueIP(char* ip)
  491. {
  492. if (isAddrInCidr(ip,0)) return 0;
  493. if (isPresentIP(IPT,ip,OUTP)) return 0;
  494. return ajouteChain(IPT,ip,OUTP,MNO);
  495. }
  496. int debloqueIP(char* ip, char * url)
  497. {
  498. if (url != NULL) syslog(LOG_INFO,"%s=%s ACCEPT",url,ip);
  499. return ajouteChain(IPT,ip,MYCH,MOK);
  500. }
  501. int rebloqueIP(char* ip)
  502. {
  503. return retireChain(IPT,ip,MYCH,MOK);
  504. }
  505. void dropIP(char * l)
  506. {
  507. char *s,*d=l;
  508. while ((s=strstr(d, "A ")) != NULL) {
  509. s+=2;
  510. if ((d=strstr(s+2, ",")) == NULL) break;
  511. *d = '\0';
  512. d++;
  513. bloqueIP(s);
  514. }
  515. bloqueIP(s);
  516. }
  517. int verifIPOk(char * l, char * url)
  518. {
  519. char *s,*d=l;
  520. while ((s=strstr(d, "A ")) != NULL) {
  521. s+=2;
  522. if ((d=strstr(s+2, ",")) == NULL) break;
  523. *d = '\0';
  524. d++;
  525. if (isPresentIP(IPT,s,MYCH)) continue;
  526. if (isAddrInCidr(s,1)) debloqueIP(s,url);
  527. }
  528. if (isPresentIP(IPT,s,MYCH)) return 1;
  529. if (isAddrInCidr(s,1)) return(debloqueIP(s,url));
  530. return 1;
  531. }
  532. int dropIP6(char * l)
  533. {
  534. char *s,*d=l;
  535. while ((s=strstr(d, "A ")) != NULL) {
  536. s+=2;
  537. if ((d=strstr(s+2, ",")) == NULL) break;
  538. *d = '\0';
  539. d++;
  540. if (isPresentIP(IP6T,s,OUTP)) continue;
  541. ajouteChain(IP6T,s,OUTP,MNO);
  542. }
  543. if (isPresentIP(IP6T,s,OUTP)) return 1;
  544. ajouteChain(IP6T,s,OUTP,MNO);
  545. return 1;
  546. }
  547. /* tache de commande et periodiques */
  548. #define t0 (time_t)0
  549. time_t tim1=t0;
  550. void tachePer1(void) /* vide les elts toutes les 30 secondes */
  551. {
  552. static time_t tim0=t0, tw;
  553. int i, v;
  554. tw = time(NULL);
  555. if ((tw - tim0) < 30) {
  556. if (T3) printf ("T3: tache1 passe %s",ctime(&tw));
  557. return;
  558. }
  559. if (T3) printf ("T3: tache1 exec %s",ctime(&tw));
  560. tim1 = time(NULL);
  561. v = (tim1 - tim0) / 30;
  562. if (tim0 != t0) {
  563. for (i=iT-1; i>=0; i--) {
  564. if ((Trv[i]&0x6) == 6) delIElt(i); // IPv4 + IPV6
  565. else { Trv[i] += 8*v;
  566. if (Trv[i] > 80) delIElt(i); // On laisse 5 min.
  567. }
  568. }
  569. }
  570. tim0 = time(NULL);
  571. return;
  572. }
  573. void ajoutParam(char * ficp, char * param)
  574. {
  575. FILE * fw;
  576. fw = fopen(ficp,"a");
  577. fwrite(param,strlen(param),1,fw);
  578. fwrite("\n",1,1,fw);
  579. fclose(fw);
  580. }
  581. static int NBin=0, NBout=0;
  582. void prInOut(void)
  583. {
  584. printf(" %d messages DNS: %d requetes, %d reponses.\n",NBout+NBin,NBout,NBin);
  585. }
  586. int printQ(char * q)
  587. {
  588. char *rep=NULL;
  589. size_t lr = 0;
  590. int n;
  591. while (1) {
  592. printf("Voulez-vous %s ?\n Taper O (OUI) ou N (NON) :\n",q);
  593. if ((n = getline(&rep, &lr, stdin)) != 2) continue;
  594. if (*rep == 'O') return 1;
  595. if (*rep == 'N') return 0;
  596. }
  597. }
  598. void pr_encours(void)
  599. {
  600. printf(" ...\r"); fflush(stdout);
  601. }
  602. #define SUNIC "|sort|uniq"
  603. #define JCTLSYS "journalctl --system"
  604. #define JCTLSYSG JCTLSYS"|grep "
  605. #define CHLOG "/var/log/user.log"
  606. #define CHLOGREP "/var/log/user.log|grep "
  607. #define CUT6 "|cut -d' ' -f6"
  608. #define CUTM45 "|cut -d' ' -f1-3,6-"
  609. #define CUT7S "|cut -d' ' -f7-"
  610. #define FHISTO ".octave_history"
  611. #define AWK5 "|awk '{ print $5}'"
  612. #define AWK4 "|awk '{ print $4}'"
  613. #define DREJ "^REJECT "
  614. #define DACC "^ACCEPT "
  615. void * fct_com(void * p)
  616. {
  617. int REQ=1;
  618. char *cmd = NULL, *fauth, pr[30], com[200];
  619. int n2;
  620. pid_t pid;
  621. fauth = (char*)p;
  622. pid = getpid();
  623. read_history(FHISTO);
  624. while (REQ) {
  625. if (kill(pid,SIGUSR1) < 0) { /* verif processus acquisition */
  626. ENDT=1;
  627. write(p1[1],"\n",1);
  628. }
  629. free(cmd);
  630. sprintf(pr,"\e[01;34m%s-> \e[00m",NPROG);
  631. cmd = readline(pr);
  632. if ((n2 = strlen(cmd)) > 0) {
  633. write(p1[1],"\n",1);
  634. add_history(cmd);
  635. switch (*cmd) {
  636. case '+' :
  637. if (*(cmd+1) != '\0') {
  638. if (litligne(cmd+1)) { /* ajout au fichier fauth */
  639. if (debloqueIP(cmd+1,NULL)) printf("Element non valable !\n");
  640. else {
  641. if (printQ("ajouter au fichier parametres"))
  642. ajoutParam(fauth,cmd+1);
  643. listeAllow();
  644. }
  645. } else printf("Erreur ajout param. !\n");
  646. } else listeAllow();
  647. break;
  648. case '-' :
  649. if (*(cmd+1) != '\0') {
  650. if (litligne(cmd)) { /* ajout au fichier fauth */
  651. if (rebloqueIP(cmd+1)) printf("Element non valable !\n");
  652. else {
  653. if (printQ("ajouter au fichier parametres"))
  654. ajoutParam(fauth,cmd);
  655. listeDeny();
  656. }
  657. } else printf("Erreur ajout param. !\n");
  658. } else listeDeny();
  659. break;
  660. case 'l' :
  661. listElt(cmd[1]);
  662. printf(" %s Utilise %d elts/%d : %.2f%% (Max. %d)!\n",ctime(&tim1),iT,
  663. NBT, (float)(iT*100)/(float)NBT, MaxElt);
  664. prInOut();
  665. break;
  666. case 't' :
  667. if (*(cmd+1) != '\0') {
  668. if ((cmd[1] == '+') || (cmd[1] == '-')) {
  669. if ((cmd[1] == '+') && (Trace < TMAX)) Trace++;
  670. else {
  671. if ((cmd[1] == '-') && (Trace > TMIN)) Trace--;
  672. else printf("Erreur: niveau dans [%d, %d].\n",TMIN,TMAX);
  673. }
  674. } else printf("Erreur: Utiliser t+ ou t- !\n");
  675. }
  676. printf(" Trace niveau %d\n",Trace);
  677. break;
  678. case 'a' :
  679. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  680. if (JCTL) sprintf(com,"%s'%s\\[%d\\]%s'%s%s",JCTLSYSG,NPROG,pid,
  681. ".* ok", CUT6,SUNIC);
  682. else sprintf(com,"grep '%s\\[%d\\]%s' %s%s%s",NPROG,pid,
  683. ".* ok", CHLOG,CUT6,SUNIC);
  684. comsh(com,0);
  685. break;
  686. case 'i' :
  687. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  688. if (JCTL) sprintf(com,"%s'%s\\[%d\\]%s'%s%s",JCTLSYSG,NPROG,pid,
  689. ".* DENY", CUT6,SUNIC);
  690. else sprintf(com,"grep '%s\\[%d\\]%s' %s%s%s",NPROG,pid,
  691. ".* DENY", CHLOG,CUT6,SUNIC);
  692. comsh(com,0);
  693. break;
  694. case 'e' :
  695. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  696. if (JCTL) sprintf(com,"%s'%s\\[%d\\]%s'%s%s",JCTLSYSG,NPROG,pid,
  697. ".* ACCEPT", CUT6,SUNIC);
  698. else sprintf(com,"grep '%s\\[%d\\]%s' %s%s%s",NPROG,pid,
  699. ".* ACCEPT", CHLOG,CUT6,SUNIC);
  700. comsh(com,0);
  701. break;
  702. case 'E' :
  703. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  704. if (JCTL) sprintf(com,"%s'%s\\[%d\\]%s'%s",JCTLSYSG,NPROG,pid,
  705. ".*ERR: ", CUTM45);
  706. else sprintf(com,"grep '%s\\[%d\\]%s' %s%s",NPROG,pid,
  707. ".*ERR: ", CHLOG,CUTM45);
  708. comsh(com,0);
  709. break;
  710. case 'L' :
  711. if (*(cmd+1) == '\0') {
  712. if (JCTL) sprintf(com,"%s'%s\\[%d\\]'|grep %s%s",JCTLSYSG,NPROG,
  713. pid, "-v 'Re[pq]. '",CUTM45);
  714. else sprintf(com,"grep '%s\\[%d\\]' %s%s%s",NPROG,pid,
  715. CHLOGREP,"-v 'Re[pq]. '",CUTM45);
  716. } else {
  717. if (JCTL) sprintf(com,"%s'%s\\[%d\\]'|grep %s%s|grep '%s'",JCTLSYSG
  718. ,NPROG,pid,"-v 'Re[pq]. '",CUTM45,cmd+1);
  719. else sprintf(com,"grep '%s\\[%d\\]' %s%s%s|grep '%s'",NPROG,pid,
  720. CHLOGREP,"-v 'Re[pq]. '",CUTM45,cmd+1);
  721. }
  722. comsh(com,0);
  723. break;
  724. case 'T' :
  725. if (*(cmd+1) != '\0') { /* avec parametre */
  726. if ((*(cmd+1) == '+') && (*(cmd+2) != '\0')) { /* script + param */
  727. sprintf(com,"./t1.sh %d %s >.Trav%d",pid,cmd+2,pid);
  728. comsh(com,0);
  729. sprintf(com,"cat .Trav%d",pid);
  730. } else {
  731. if (JCTL)
  732. sprintf(com,"%s'%s\\[%d\\].*%s'|grep%s%s",JCTLSYSG,NPROG,pid,
  733. cmd+1," 'Re[pq]. '",CUTM45);
  734. else
  735. sprintf(com,"grep '%s\\[%d\\].*%s' %s%s%s",NPROG,pid,cmd+1,
  736. CHLOGREP," 'Re[pq]. '",CUTM45);
  737. }
  738. } else {
  739. if (JCTL)sprintf(com,"%s'%s\\[%d\\]'|grep %s%s",JCTLSYSG,NPROG,pid,
  740. " 'Re[pq]. '",CUTM45);
  741. else sprintf(com,"grep '%s\\[%d\\]' %s%s%s",NPROG,pid,
  742. CHLOGREP," 'Re[pq]. '",CUTM45);
  743. }
  744. comsh(com,0);
  745. prInOut();
  746. break;
  747. case '>' :
  748. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  749. if (JCTL) sprintf(com,"%s'%s\\[%d\\].*%s%s%s",JCTLSYSG,NPROG,pid,
  750. " Req. '",CUT7S,SUNIC);
  751. else sprintf(com,"grep '%s\\[%d\\]' %s%s%s%s",NPROG,pid,
  752. CHLOGREP," 'Req. '",CUT7S,SUNIC);
  753. comsh(com,0);
  754. prInOut();
  755. break;
  756. case '<' :
  757. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  758. if (JCTL) sprintf(com,"%s'%s\\[%d\\].*%s%s%s",JCTLSYSG,NPROG,pid,
  759. " Rep. '",CUT7S,SUNIC);
  760. else sprintf(com,"grep '%s\\[%d\\]' %s%s%s%s",NPROG,pid,
  761. CHLOGREP," 'Rep. '",CUT7S,SUNIC);
  762. comsh(com,0);
  763. prInOut();
  764. break;
  765. case 'r' :
  766. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  767. sprintf(com,"%s -L %s|grep %s%s%s",IPT,OUTP,DREJ,AWK5,SUNIC);
  768. pr_encours();
  769. comsh(com,0);
  770. break;
  771. case 'R' :
  772. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  773. sprintf(com,"%s -L|grep %s%s%s",IP6T,DREJ,AWK4,SUNIC);
  774. pr_encours();
  775. comsh(com,0);
  776. break;
  777. case 'S' :
  778. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  779. RUN = 0;
  780. REQ = 0;
  781. write(p1[1],"\n",1);
  782. break;
  783. case 'v' :
  784. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  785. sprintf(com,"%s -L %s|grep %s%s%s",IPT,MYCH,DACC,AWK5,SUNIC);
  786. pr_encours();
  787. comsh(com,0);
  788. break;
  789. case ' ' :
  790. if (*(cmd+1) != '\0') comsh(cmd+1,0);
  791. break;
  792. case '?' :
  793. if (*(cmd+1) != '\0') printf("ignore %s\n",cmd+1);
  794. printf("Version %s\n",Version);
  795. prInOut();
  796. default :
  797. printf("?\t\t: Version et menu.\n");
  798. printf("+url\t\t: Allow (autoriser une Url)\n");
  799. printf("-url\t\t: Deny (interdire une Url)\n");
  800. printf("a\t\t: Autorisations suivant analyse\n");
  801. printf("i\t\t: Interdictions suivant analyse\n");
  802. printf("e\t\t: Exceptions suivant analyse\n");
  803. printf("E\t\t: Liste des erreurs\n");
  804. printf("l[+|-]\t\t: Liste des elements dynamiques\n");
  805. printf("L[filtre]\t: Logs du systeme avec filtre de type regex\n");
  806. printf("r\t\t: Rejets actifs IPv4 (dure plusieurs sec.)\n");
  807. printf("R\t\t: Rejets actifs IPv6 (dure plusieurs sec.)\n");
  808. printf("v\t\t: Validations actives IPv4 (dure plusieurs sec.)\n");
  809. printf("t+|-\t\t: Niveau de trace : 0 (off) => 3\n");
  810. if (LogC) {
  811. printf("T[+][mot]\t: Traces des demandes/reponses contenant mot.\n\t\t Avec + fait les liaisons entre requetes et reponses.\n");
  812. printf(">\t\t: Traces des demandes triees\n");
  813. printf("<\t\t: Traces des reponses triees\n");
  814. }
  815. printf("S\t\t: Stopper\n");
  816. break;
  817. }
  818. }
  819. }
  820. WH=write_history(FHISTO);
  821. free(cmd);
  822. sprintf(com,"rm -f .Trav%d",pid);
  823. comsh(com,0);
  824. /* fin dialogue */
  825. pthread_exit(NULL);
  826. }
  827. void getIface(void)
  828. {
  829. FILE * fd;
  830. char *line = NULL, *s, *w;
  831. size_t ll = 0;
  832. int n;
  833. if ((fd = fopen("/proc/net/route","r")) == NULL) {
  834. perror("route"); return;
  835. }
  836. while ((n = getline(&line, &ll, fd)) > 0) {
  837. if ((s=strstr(line,"00000000"))==NULL) continue;
  838. w=line;
  839. while ((*w != ' ') && (*w != '\t')) w++;
  840. *w = '\0';
  841. w++;
  842. while ((*w == ' ') || (*w == '\t')) w++;
  843. if (s==w) { /* ok */
  844. IFACE = (char*)malloc(strlen(line)+1);
  845. strcpy(IFACE,line);
  846. break;
  847. }
  848. }
  849. free(line);
  850. fclose(fd);
  851. }
  852. #define Vie (ie >= 0)
  853. int main(int N, char * P[])
  854. {
  855. pthread_t thid;
  856. FILE * fp;
  857. char *analyse="tcpdump", *line = NULL, *cmd = NULL, *s1, *s2, *refU;
  858. char *fauth = "auth1.txt", *strR = "-R", *Pars, strPID[8], **NP;
  859. size_t ll = 0, lc = 0;
  860. ssize_t n,n2;
  861. int Inter=0, i, ie, np=0, opt;
  862. if ((NPROG = strrchr(P[0],(int)'/')) == NULL) NPROG=P[0];
  863. else NPROG++;
  864. sprintf(strPID,"%d",getpid());
  865. /* verif. options */
  866. while ((opt = getopt(N, P, "ilp:R:t")) != -1) {
  867. switch (opt) {
  868. case 'i':
  869. Inter = 1;
  870. break;
  871. case 'l':
  872. LogC = 1;
  873. break;
  874. case 't':
  875. Trace = TMIN+1;
  876. break;
  877. case 'p':
  878. fauth = optarg;
  879. break;
  880. case 'R':
  881. REPR=1;
  882. np = atoi(optarg);
  883. break;
  884. default: /* '?' */
  885. fprintf(stderr, "Utilisation: %s [options]\nAvec les options :\n", NPROG);
  886. fprintf(stderr, "\t-i : mode interactif,\n");
  887. fprintf(stderr, "\t-l : log des requetes,\n");
  888. fprintf(stderr, "\t-p fichier : nom du fichier parametres (%s par defaut),\n",fauth);
  889. fprintf(stderr, "\t-t : avec trace.\n");
  890. return 1;
  891. }
  892. }
  893. if ((REPR) && (np != getpid())) {
  894. fprintf(stderr,"Erreur reprise %d\n", np);
  895. return 1;
  896. }
  897. if (optind < N) {
  898. fprintf(stderr,"Parametre inconnu : %s\n", P[optind]);
  899. return 1;
  900. }
  901. getIface();
  902. if (REPR) {
  903. while (IFACE==NULL) { sleep(1); getIface(); }
  904. } else {
  905. if (IFACE == NULL) {
  906. fprintf(stderr,"Interface reseau absente !\n");
  907. return 9;
  908. }
  909. }
  910. printf("%s %s sur %s\n", NPROG, Version, IFACE);
  911. /* verif privilege root */
  912. if ((getuid() > 0) && (geteuid() > 0)) {
  913. fprintf(stderr,"A executer sous root !\n");
  914. return 2;
  915. }
  916. if (comsh(JCTLSYS,EX_SILENT) == 0) JCTL=1;
  917. if (T1) printf("T1: Fichier parametres = %s\n",fauth);
  918. signal(SIGUSR1,SIG_IGN);
  919. if (pipe(p1) < 0) {
  920. perror("pipe"); return 3;
  921. }
  922. openlog(NULL,LOG_PID,0);
  923. /* on lance le fils : */
  924. if ((pid = fork()) < 0) {
  925. perror("fork"); return 4;
  926. }
  927. if (pid == 0) {
  928. signal(SIGINT,SIG_IGN);
  929. close(0);
  930. close(p1[0]);
  931. dup2(p1[1],1); /* stdout dans p1 */
  932. dup2(p1[1],2); /* idem stderr */
  933. setsid();
  934. execlp(analyse,analyse,"-tn","-i",IFACE,"port","53",NULL);
  935. perror("execl");
  936. return 5;
  937. }
  938. if (Inter) signal(SIGINT,SIG_IGN);
  939. else signal(SIGINT,interup);
  940. if ((np=initIPT())!=0) {
  941. if (T1) printf("Erreur initIPT %d !!??\n",np);
  942. syslog(LOG_WARNING, "ERR: Erreur initIPT %d !!??\n",np);
  943. }
  944. /* lecture des listes */
  945. lectliste(fauth);
  946. if (T1) recaplistes();
  947. sleep(1); /* attend le fils en place */
  948. if (kill(pid,SIGUSR1) < 0) return 6;
  949. signal(SIGCHLD,interup);
  950. /*
  951. fcntl(p1[0], F_SETFL, O_NONBLOCK);
  952. flag0 = fcntl(0, F_GETFL, O_NONBLOCK);
  953. fcntl(0, F_SETFL, O_NONBLOCK);
  954. */
  955. /* on analyse la sortie de p1 */
  956. if ((fp = fdopen(p1[0],"r")) == NULL) {
  957. perror("fdopen"); return 7;
  958. }
  959. fcntl(p1[0], F_SETPIPE_SZ,1048576);
  960. if (T1) printf("Depart %s %s PIDF:%d !\n",NPROG, strPID,pid);
  961. if (T1) printf("Capacite pipe : %ld bytes\n", (long)fcntl(p1[0], F_GETPIPE_SZ));
  962. np=0;
  963. /* lancement du thread */
  964. if (Inter) {
  965. if (pthread_create(&thid,NULL,fct_com,(void*)fauth) != 0) {
  966. fprintf(stderr,"Erreur pthread_create !\n"); return 9;
  967. }
  968. }
  969. while (RUN) {
  970. tachePer1();
  971. if ((n = getline(&line, &ll, fp)) > 0) {
  972. if (ENDT) {
  973. printf("Erreur : plus de tache d'analyse !\n"); break;
  974. }
  975. if (RUN == 0) break;
  976. if ((n==1) && (*line=='\n')) continue;
  977. if (np==0) { np++;
  978. if (REPR) syslog(LOG_INFO,"Reprise de l'analyse !");
  979. else syslog(LOG_INFO,"Debut de l'analyse !");
  980. }
  981. /* analyse */
  982. if ((s1=strstr(line, " > ")) == NULL) continue;
  983. if (strncmp(s1-3,".53",3) == 0) { /* REPONSE */
  984. if ((s2=strstr(s1+3, ":")) == NULL) continue;
  985. NBin++;
  986. *s2 = '\0';
  987. s1 = s2 -1;
  988. while (*s1 != '.') s1--;
  989. np = atoi(s1+1);
  990. if ((ie = isElt(np)) == -1) { /* Elt OK ou ABSENT ! */
  991. ie = isElt(-np);
  992. s1 = s2+1;
  993. if ((s2=strstr(s1, " A ")) != NULL) { /* IPv4 */
  994. s2++;
  995. s1 = strrchr(s2,(int)' ');
  996. *s1 = '\0';
  997. if (LogC) syslog(LOG_INFO,"Rep. %d %s",np,s2);
  998. if Vie {
  999. markElt(ie,4); refU = Turl[ie];
  1000. } else {
  1001. if (T1) printf("Elt %d non trouve !!??\n",np);
  1002. syslog(LOG_WARNING,"ERR: Elt %d non trouve !!??\n",np);
  1003. refU = NULL;
  1004. }
  1005. if (!verifIPOk(s2, refU))
  1006. if Vie syslog(LOG_INFO,"Deblocage IP4 %s",refU);
  1007. } else {
  1008. if ((s2=strstr(s1, " AAAA ")) != NULL) { /* IPv6 */
  1009. s2++;
  1010. s1 = strrchr(s2,(int)' ');
  1011. *s1 = '\0';
  1012. if (LogC) syslog(LOG_INFO,"Rep. %d %s",np,s2);
  1013. if Vie markElt(ie,2);
  1014. dropIP6(s2);
  1015. } else {
  1016. if Vie markElt(ie,1);
  1017. }
  1018. }
  1019. continue;
  1020. }
  1021. s1 = s2+1;
  1022. if ((s2=strstr(s1, " A ")) == NULL) {
  1023. if ((s2=strstr(s1, " AAAA ")) == NULL) {
  1024. markElt(ie,1);
  1025. } else { /* traitement IPv6 */
  1026. s2++;
  1027. if (LogC) syslog(LOG_INFO,"Rep. %d %s",np,s2);
  1028. s1 = strrchr(s2,(int)' ');
  1029. *s1 = '\0';
  1030. if (markElt(ie,2)) dropIP6(s2);
  1031. }
  1032. continue;
  1033. }
  1034. /* IPv4 REJECT */
  1035. s2++;
  1036. s1 = strrchr(s2,(int)' ');
  1037. *s1 = '\0';
  1038. if (LogC) syslog(LOG_INFO,"Rep. %d %s",np,s2);
  1039. syslog(LOG_INFO,"%s DENY",Turl[ie]);
  1040. if (markElt(ie,4)) dropIP(s2);
  1041. } else { /* DEMANDE */
  1042. NBout++;
  1043. *s1 = '\0';
  1044. s2 = s1 +1;
  1045. while (*s1 != '.') s1--;
  1046. np = atoi(s1+1);
  1047. if ((s1=strstr(s2, " A? ")) == NULL) continue;
  1048. s1 += 4;
  1049. s2 = s1 +1;
  1050. while (*s2 != ' ') s2++;
  1051. *(s2-1) = '\0'; /* on supprime le '.' */
  1052. if (LogC) syslog(LOG_INFO,"Req. %d %s",np,s1);
  1053. if (strstr(s1, ".") == NULL) { /* il doit en rester 1 */
  1054. if (T1) printf("Ignore : %d %s !\n",np,s1);
  1055. syslog(LOG_WARNING,"ERR: Ignore %d %s !\n",np,s1);
  1056. continue;
  1057. }
  1058. if (!isDeny(s1)) { // V2 ! On enregistre le OK en NEGATIF
  1059. if (isElt(-np) < 0) {
  1060. addElt(-np,s1);
  1061. syslog(LOG_INFO,"%s ok",s1);
  1062. }
  1063. continue;
  1064. }
  1065. if (isElt(np) < 0) {
  1066. i=addElt(np,s1);
  1067. if (T3) printf("T3: addElt %d %d/%d\n",NbElt,i,NBT);
  1068. }
  1069. }
  1070. }
  1071. if (REQ) {
  1072. printf("Taper votre commande : H pour help !\n");
  1073. if ((n2 = getline(&cmd, &lc, stdin)) > 0) {
  1074. switch (*cmd) {
  1075. case 'C' :
  1076. REQ = 0;
  1077. break;
  1078. case 'L' :
  1079. listElt(cmd[1]);
  1080. printf(" %s Utilise %d elts/%d : %.2f%% (Max. %d)!\n",
  1081. ctime(&tim1),iT,NBT,(float)(iT*100)/(float)NBT,MaxElt);
  1082. break;
  1083. case 'S' :
  1084. RUN = 0;
  1085. REQ = 0;
  1086. break;
  1087. default :
  1088. printf("C\t: continuer\n");
  1089. printf("L\t: liste des elts\n");
  1090. printf("S\t: stopper\n");
  1091. break;
  1092. }
  1093. }
  1094. }
  1095. }
  1096. syslog(LOG_INFO,"Fin de l'analyse !");
  1097. free(line);
  1098. free(cmd);
  1099. kill(pid,SIGTERM);
  1100. close(p1[0]);
  1101. close(p1[1]);
  1102. closelog();
  1103. if (ENDT) { /* relance auto */
  1104. if (T1) printf("Relance auto %s dans %d sec. ...\n",strPID, DELAYR);
  1105. sleep(DELAYR); /* attend N s */
  1106. NP = (char**)malloc((sizeof(Pars))*(N+3));
  1107. ie=0;
  1108. for (i=0;i<N;i++) { NP[i] = P[i]; if (strcmp(P[i],strR) == 0) ie=1; }
  1109. if (ie == 0) {
  1110. NP[i++]=strR;
  1111. NP[i++]=strPID;
  1112. }
  1113. NP[i]=NULL;
  1114. if (WH) write_history(FHISTO);
  1115. comsh("reset",0);
  1116. execv(P[0],NP);
  1117. perror("execv");
  1118. }
  1119. printf("Fin du programme!\n");
  1120. return 0;
  1121. }