Code afférent au projet Kouglof 2 de l'E2L
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

1189 linhas
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, com[100];
  619. int n2;
  620. pid_t pid;
  621. fauth = (char*)p;
  622. pid = getpid();
  623. read_history(FHISTO);
  624. if (REPR) comsh("reset",0);
  625. while (REQ) {
  626. if (kill(pid,SIGUSR1) < 0) { /* verif processus acquisition */
  627. ENDT=1;
  628. write(p1[1],"\n",1);
  629. }
  630. free(cmd);
  631. cmd = readline("O-> ");
  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. /* fin dialogue */
  823. pthread_exit(NULL);
  824. }
  825. void getIface(void)
  826. {
  827. FILE * fd;
  828. char *line = NULL, *s, *w;
  829. size_t ll = 0;
  830. int n;
  831. if ((fd = fopen("/proc/net/route","r")) == NULL) {
  832. perror("route"); return;
  833. }
  834. while ((n = getline(&line, &ll, fd)) > 0) {
  835. if ((s=strstr(line,"00000000"))==NULL) continue;
  836. w=line;
  837. while ((*w != ' ') && (*w != '\t')) w++;
  838. *w = '\0';
  839. w++;
  840. while ((*w == ' ') || (*w == '\t')) w++;
  841. if (s==w) { /* ok */
  842. IFACE = (char*)malloc(strlen(line)+1);
  843. strcpy(IFACE,line);
  844. break;
  845. }
  846. }
  847. free(line);
  848. fclose(fd);
  849. }
  850. #define Vie (ie >= 0)
  851. int main(int N, char * P[])
  852. {
  853. pthread_t thid;
  854. FILE * fp;
  855. char *analyse="tcpdump", *line = NULL, *cmd = NULL, *s1, *s2, *refU;
  856. char *fauth = "auth1.txt", *strR = "-R", *Pars, strPID[8], **NP;
  857. size_t ll = 0, lc = 0;
  858. ssize_t n,n2;
  859. int Inter=0, i, ie, np=0, opt;
  860. if ((NPROG = strrchr(P[0],(int)'/')) == NULL) NPROG=P[0];
  861. else NPROG++;
  862. sprintf(strPID,"%d",getpid());
  863. /* verif. options */
  864. while ((opt = getopt(N, P, "ilp:R:t")) != -1) {
  865. switch (opt) {
  866. case 'i':
  867. Inter = 1;
  868. break;
  869. case 'l':
  870. LogC = 1;
  871. break;
  872. case 't':
  873. Trace = TMIN+1;
  874. break;
  875. case 'p':
  876. fauth = optarg;
  877. break;
  878. case 'R':
  879. REPR=1;
  880. np = atoi(optarg);
  881. break;
  882. default: /* '?' */
  883. fprintf(stderr, "Utilisation: %s [options]\nAvec les options :\n", NPROG);
  884. fprintf(stderr, "\t-i : mode interactif,\n");
  885. fprintf(stderr, "\t-l : log des requetes,\n");
  886. fprintf(stderr, "\t-p fichier : nom du fichier parametres (%s par defaut),\n",fauth);
  887. fprintf(stderr, "\t-t : avec trace.\n");
  888. return 1;
  889. }
  890. }
  891. if ((REPR) && (np != getpid())) {
  892. fprintf(stderr,"Erreur reprise %d\n", np);
  893. return 1;
  894. }
  895. if (optind < N) {
  896. fprintf(stderr,"Parametre inconnu : %s\n", P[optind]);
  897. return 1;
  898. }
  899. getIface();
  900. if (REPR) {
  901. while (IFACE==NULL) { sleep(1); getIface(); }
  902. } else {
  903. if (IFACE == NULL) {
  904. fprintf(stderr,"Interface reseau absente !\n");
  905. return 9;
  906. }
  907. }
  908. printf("%s %s sur %s\n", NPROG, Version, IFACE);
  909. /* verif privilege root */
  910. if ((getuid() > 0) && (geteuid() > 0)) {
  911. fprintf(stderr,"A executer sous root !\n");
  912. return 2;
  913. }
  914. if (comsh(JCTLSYS,EX_SILENT) == 0) JCTL=1;
  915. if (T1) printf("T1: Fichier parametres = %s\n",fauth);
  916. signal(SIGUSR1,SIG_IGN);
  917. if (pipe(p1) < 0) {
  918. perror("pipe"); return 3;
  919. }
  920. openlog(NULL,LOG_PID,0);
  921. /* on lance le fils : */
  922. if ((pid = fork()) < 0) {
  923. perror("fork"); return 4;
  924. }
  925. if (pid == 0) {
  926. signal(SIGINT,SIG_IGN);
  927. close(0);
  928. close(p1[0]);
  929. dup2(p1[1],1); /* stdout dans p1 */
  930. dup2(p1[1],2); /* idem stderr */
  931. setsid();
  932. execlp(analyse,analyse,"-tn","-i",IFACE,"port","53",NULL);
  933. perror("execl");
  934. return 5;
  935. }
  936. if (Inter) signal(SIGINT,SIG_IGN);
  937. else signal(SIGINT,interup);
  938. if ((np=initIPT())!=0) {
  939. if (T1) printf("Erreur initIPT %d !!??\n",np);
  940. syslog(LOG_WARNING, "ERR: Erreur initIPT %d !!??\n",np);
  941. }
  942. /* lecture des listes */
  943. lectliste(fauth);
  944. if (T1) recaplistes();
  945. sleep(1); /* attend le fils en place */
  946. if (kill(pid,SIGUSR1) < 0) return 6;
  947. signal(SIGCHLD,interup);
  948. /*
  949. fcntl(p1[0], F_SETFL, O_NONBLOCK);
  950. flag0 = fcntl(0, F_GETFL, O_NONBLOCK);
  951. fcntl(0, F_SETFL, O_NONBLOCK);
  952. */
  953. /* on analyse la sortie de p1 */
  954. if ((fp = fdopen(p1[0],"r")) == NULL) {
  955. perror("fdopen"); return 7;
  956. }
  957. fcntl(p1[0], F_SETPIPE_SZ,1048576);
  958. if (T1) printf("Depart %s %s PIDF:%d !\n",NPROG, strPID,pid);
  959. if (T1) printf("Capacite pipe : %ld bytes\n", (long)fcntl(p1[0], F_GETPIPE_SZ));
  960. np=0;
  961. /* lancement du thread */
  962. if (Inter) {
  963. if (pthread_create(&thid,NULL,fct_com,(void*)fauth) != 0) {
  964. fprintf(stderr,"Erreur pthread_create !\n"); return 9;
  965. }
  966. }
  967. while (RUN) {
  968. tachePer1();
  969. if ((n = getline(&line, &ll, fp)) > 0) {
  970. if (ENDT) {
  971. printf("Erreur : plus de tache d'analyse !\n"); break;
  972. }
  973. if (RUN == 0) break;
  974. if ((n==1) && (*line=='\n')) continue;
  975. if (np==0) { np++;
  976. if (REPR) syslog(LOG_INFO,"Reprise de l'analyse !");
  977. else syslog(LOG_INFO,"Debut de l'analyse !");
  978. }
  979. /* analyse */
  980. if ((s1=strstr(line, " > ")) == NULL) continue;
  981. if (strncmp(s1-3,".53",3) == 0) { /* REPONSE */
  982. if ((s2=strstr(s1+3, ":")) == NULL) continue;
  983. NBin++;
  984. *s2 = '\0';
  985. s1 = s2 -1;
  986. while (*s1 != '.') s1--;
  987. np = atoi(s1+1);
  988. if ((ie = isElt(np)) == -1) { /* Elt OK ou ABSENT ! */
  989. ie = isElt(-np);
  990. s1 = s2+1;
  991. if ((s2=strstr(s1, " A ")) != NULL) { /* IPv4 */
  992. s2++;
  993. s1 = strrchr(s2,(int)' ');
  994. *s1 = '\0';
  995. if (LogC) syslog(LOG_INFO,"Rep. %d %s",np,s2);
  996. if Vie {
  997. markElt(ie,4); refU = Turl[ie];
  998. } else {
  999. if (T1) printf("Elt %d non trouve !!??\n",np);
  1000. syslog(LOG_WARNING,"ERR: Elt %d non trouve !!??\n",np);
  1001. refU = NULL;
  1002. }
  1003. if (!verifIPOk(s2, refU))
  1004. if Vie syslog(LOG_INFO,"Deblocage IP4 %s",refU);
  1005. } else {
  1006. if ((s2=strstr(s1, " AAAA ")) != NULL) { /* IPv6 */
  1007. s2++;
  1008. s1 = strrchr(s2,(int)' ');
  1009. *s1 = '\0';
  1010. if (LogC) syslog(LOG_INFO,"Rep. %d %s",np,s2);
  1011. if Vie markElt(ie,2);
  1012. dropIP6(s2);
  1013. } else {
  1014. if Vie markElt(ie,1);
  1015. }
  1016. }
  1017. continue;
  1018. }
  1019. s1 = s2+1;
  1020. if ((s2=strstr(s1, " A ")) == NULL) {
  1021. if ((s2=strstr(s1, " AAAA ")) == NULL) {
  1022. markElt(ie,1);
  1023. } else { /* traitement IPv6 */
  1024. s2++;
  1025. if (LogC) syslog(LOG_INFO,"Rep. %d %s",np,s2);
  1026. s1 = strrchr(s2,(int)' ');
  1027. *s1 = '\0';
  1028. if (markElt(ie,2)) dropIP6(s2);
  1029. }
  1030. continue;
  1031. }
  1032. /* IPv4 REJECT */
  1033. s2++;
  1034. s1 = strrchr(s2,(int)' ');
  1035. *s1 = '\0';
  1036. if (LogC) syslog(LOG_INFO,"Rep. %d %s",np,s2);
  1037. syslog(LOG_INFO,"%s DENY",Turl[ie]);
  1038. if (markElt(ie,4)) dropIP(s2);
  1039. } else { /* DEMANDE */
  1040. NBout++;
  1041. *s1 = '\0';
  1042. s2 = s1 +1;
  1043. while (*s1 != '.') s1--;
  1044. np = atoi(s1+1);
  1045. if ((s1=strstr(s2, " A? ")) == NULL) continue;
  1046. s1 += 4;
  1047. s2 = s1 +1;
  1048. while (*s2 != ' ') s2++;
  1049. *(s2-1) = '\0'; /* on supprime le '.' */
  1050. if (LogC) syslog(LOG_INFO,"Req. %d %s",np,s1);
  1051. if (strstr(s1, ".") == NULL) { /* il doit en rester 1 */
  1052. if (T1) printf("Ignore : %d %s !\n",np,s1);
  1053. syslog(LOG_WARNING,"ERR: Ignore %d %s !\n",np,s1);
  1054. continue;
  1055. }
  1056. if (!isDeny(s1)) { // V2 ! On enregistre le OK en NEGATIF
  1057. if (isElt(-np) < 0) {
  1058. addElt(-np,s1);
  1059. syslog(LOG_INFO,"%s ok",s1);
  1060. }
  1061. continue;
  1062. }
  1063. if (isElt(np) < 0) {
  1064. i=addElt(np,s1);
  1065. if (T3) printf("T3: addElt %d %d/%d\n",NbElt,i,NBT);
  1066. }
  1067. }
  1068. }
  1069. if (REQ) {
  1070. printf("Taper votre commande : H pour help !\n");
  1071. if ((n2 = getline(&cmd, &lc, stdin)) > 0) {
  1072. switch (*cmd) {
  1073. case 'C' :
  1074. REQ = 0;
  1075. break;
  1076. case 'L' :
  1077. listElt(cmd[1]);
  1078. printf(" %s Utilise %d elts/%d : %.2f%% (Max. %d)!\n",
  1079. ctime(&tim1),iT,NBT,(float)(iT*100)/(float)NBT,MaxElt);
  1080. break;
  1081. case 'S' :
  1082. RUN = 0;
  1083. REQ = 0;
  1084. break;
  1085. default :
  1086. printf("C\t: continuer\n");
  1087. printf("L\t: liste des elts\n");
  1088. printf("S\t: stopper\n");
  1089. break;
  1090. }
  1091. }
  1092. }
  1093. }
  1094. syslog(LOG_INFO,"Fin de l'analyse !");
  1095. free(line);
  1096. free(cmd);
  1097. kill(pid,SIGTERM);
  1098. close(p1[0]);
  1099. close(p1[1]);
  1100. closelog();
  1101. if (ENDT) { /* relance auto */
  1102. comsh("reset",0);
  1103. if (T1) printf("Relance auto %s dans %d sec. ...\n",strPID, DELAYR);
  1104. sleep(DELAYR); /* attend N s */
  1105. NP = (char**)malloc((sizeof(Pars))*(N+3));
  1106. for (i=0;i<N;i++) NP[i] = P[i];
  1107. NP[i++]=strR;
  1108. NP[i++]=strPID;
  1109. NP[i]=NULL;
  1110. if (WH) write_history(FHISTO);
  1111. execv(P[0],NP);
  1112. perror("execv");
  1113. }
  1114. printf("Fin du programme!\n");
  1115. return 0;
  1116. }