Página 1 de 2

Buscar sticky bit en todos los archivos

NotaPublicado: 2011-01-21 20:54 @912
por situ
¿Alguien tiene un script para buscar sticky bit?

Sintáxis: [ Descargar ] [ Ocultar ]
Using bash Syntax Highlighting
  1. ~$ find / -perm -1000 -exec ls -la {} \;
  2.  
Coloreado en 0.004 segundos, usando GeSHi 1.0.8.4


Yo tengo en bash pero estaba buscando en Perl.

Saludos.

Re: Buscar sticky bit en todos los archivos

NotaPublicado: 2011-01-21 21:02 @918
por explorer
Para traducir órdenes del comando find a Perl, nada mejor que el comando find2perl (incluido con la distribución del módulo File::Find, así que es muy posible que lo tengas ya instalado).
Sintáxis: [ Descargar ] [ Ocultar ]
Using bash Syntax Highlighting
explorer@casa:~/Documentos/Desarrollo> find2perl / -perm -1000 -exec ls -la {} \;
#! /usr/bin/perl -w
    eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
        if 0; #$running_under_some_shell

use strict;
use File::Find ();

# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

sub wanted;
sub doexec ($@);


use Cwd ();
my $cwd = Cwd::cwd();


# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '/');
exit;


sub wanted {
    my ($dev,$ino,$mode,$nlink,$uid,$gid);

    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
    (($mode & 01000) == 01000) &&
    doexec(0, 'ls','-la','{}');
}


sub doexec ($@) {
    my $ok = shift;
    my @command = @_; # copy so we don't try to s/// aliases to constants
    for my $word (@command)
        { $word =~ s#{}#$name#g }
    if ($ok) {
        my $old = select(STDOUT);
        $| = 1;
        print "@command";
        select($old);
        return 0 unless <STDIN> =~ /^y/;
    }
    chdir $cwd; #sigh
    system @command;
    chdir $File::Find::dir;
    return !$?;
}
 
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4

Aunque analizando el código, se puede extraer la información que realmente queremos.

Re: Buscar sticky bit en todos los archivos

NotaPublicado: 2011-01-21 21:16 @928
por situ
Intenté agregar el siguiente script dentro de la utilidad que hablamos en el post: existencia-de-archivo-y-su-propietario-en-linux-t5685.html

Pero no me funciona, ahora si lo ejecuto en un script nuevo, si anda =(

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. use Term::ANSIColor qw(:constants);
  2. use strict;
  3. use File::Find ();
  4. use vars qw/*name *dir *prune/;
  5. *name   = *File::Find::name;
  6. *dir    = *File::Find::dir;
  7. *prune  = *File::Find::prune;
  8. sub wanted;
  9. # Traverse desired filesystems
  10. File::Find::find({wanted => \&wanted}, '/');
  11. sub wanted {
  12.     my ($dev,$ino,$mode,$nlink,$uid,$gid);
  13.     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) and
  14.     !($File::Find::prune |= ($dev != $File::Find::topdev)) and
  15.     -d _ and
  16.     (
  17.         (($mode & 01000) == 01000)
  18.     ) and
  19. print BOLD, WHITE, "Informacion:",RESET . " Se detecto sticky bit en $name\n" and      
  20. }
  21. print BOLD, CYAN, "\t\t\t\t\t\t[Pulse la tecla ENTER para continuar]", RESET;
  22. <>;
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4

Re: Buscar sticky bit en todos los archivos

NotaPublicado: 2011-01-21 21:21 @931
por explorer
Tienes que fijarte en los errores que te devuelve Perl...

Sintáxis: [ Descargar ] [ Ocultar ]
Using bash Syntax Highlighting
explorer@casa:~/Documentos/Desarrollo> perl -c code_24258.pl
syntax error at code_24258.pl line 20, near "}"
code_24258.pl had compilation errors.
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

Re: Buscar sticky bit en todos los archivos

NotaPublicado: 2011-01-21 21:35 @941
por situ
Igualmente como te decia antes solo me anda el tema es cuando lo agrego al script anterior.

Codigo:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl -w
  2. no warnings;
  3. use File::Find ();
  4. use File::stat;
  5. # Modulos
  6. use Term::ANSIColor qw(:constants);
  7.  
  8. ## Verificacion de permisos en archivos y directorios
  9. print " -- Verificar permisos en archivos y directorios\n";
  10. # verifico si el owner es root
  11. my @archivos = qw(
  12. /etc/crontab
  13. /etc/exports
  14. /etc/fstab
  15. /etc/group
  16. /etc/hosts
  17. /etc/hosts.allow
  18. /etc/hosts.deny
  19. /etc/inetd.conf
  20. /etc/inittab
  21. /etc/ld.so.conf
  22. /etc/modules
  23. /etc/motd
  24. /etc/mtab
  25. /etc/passwd
  26. /etc/profile
  27. /etc/securetty
  28. /etc/syslog.conf
  29. /bin/
  30. /boot/
  31. /etc/
  32. /etc/cron.daily/
  33. /etc/cron.hourly/
  34. /etc/cron.weekly/
  35. /home/
  36. /mnt/
  37. /lib/
  38. /root/
  39. /sbin/
  40. /tmp/
  41. /usr/
  42. /usr/bin/
  43. /var/
  44. );
  45. for my $archivo (@archivos) {
  46.     my $estatus = stat($archivo);
  47.  
  48.     if (! defined $estatus) {
  49.         print BOLD, RED, 'No existe', RESET, " El archivo $archivo no existe en el sistema o no puede ser accedido\n";
  50.     }
  51.     else {
  52.         if (! -r $archivo) {
  53.             print BOLD, RED, 'No leible', RESET, " El archivo $archivo no puede ser leido\n";
  54.         }
  55.  
  56.         if ( $estatus->uid == 0 ) {
  57.             #print BOLD, GREEN, "Positivo:", RESET, " El archivo $archivo tiene como propietario a root\n";
  58.         }
  59.         else {
  60.             my ($nombre,$password,$uid,$gid,$quota,$comentario,$gcos,$dir,$shell,$expire) = getpwuid($estatus->uid);
  61.             print BOLD, RED,   "Negativo:", RESET, " El archivo $archivo tiene como propietario a $nombre ($uid)\n";
  62.         }
  63.  
  64.         my $permisos = sprintf "%04o", $estatus->mode & 07777;
  65.  
  66.         if ( $permisos eq '0644' ) {
  67.  
  68.             #print BOLD, GREEN, "Positivo:", RESET, " El archivo $archivo tiene como permiso 0644\n";
  69.  
  70.         }
  71.  
  72.         else {
  73.  
  74.             print BOLD, RED, "Negativo:", RESET, " El archivo $archivo tiene como permiso $permisos\n";
  75.  
  76.         }
  77.     }
  78. }
  79.  
  80. ## Verificacion de Sticky Bit
  81.  
  82. print " -- Verificacion de Sticky Bit\n";
  83. use Term::ANSIColor qw(:constants);
  84. use File::Find ();
  85. use vars qw/*name *dir *prune/;
  86. *name   = *File::Find::name;
  87. *dir    = *File::Find::dir;
  88. *prune  = *File::Find::prune;
  89. sub wanted;
  90. # Traverse desired filesystems
  91. File::Find::find({wanted => \&wanted}, '/');
  92. sub wanted {
  93.     my ($dev,$ino,$mode,$nlink,$uid,$gid);
  94.     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) and
  95.     !($File::Find::prune |= ($dev != $File::Find::topdev)) and
  96.     -d _ and
  97.     (
  98.         (($mode & 01000) == 01000)
  99.     ) and
  100. print BOLD, WHITE, "Informacion:",RESET . " Se detecto sticky bit en $name\n";}
  101.  
  102.  
Coloreado en 0.003 segundos, usando GeSHi 1.0.8.4


=(

Re: Buscar sticky bit en todos los archivos

NotaPublicado: 2011-01-21 21:58 @957
por explorer
Si haces un find2perl / -perm -1000 te dará una subrutina de búsqueda más sencilla con la que poder probar.

Re: Buscar sticky bit en todos los archivos

NotaPublicado: 2011-01-21 22:18 @970
por situ
Se queda "pensando" no sé si es así o algo está mal en el código :S

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl -w
  2. no warnings;
  3. use File::Find ();
  4. use File::stat;
  5. # Modulos
  6. use Term::ANSIColor qw(:constants);
  7.  
  8. ## Verificacion de permisos en archivos y directorios
  9. print " -- Verificar permisos en archivos y directorios\n";
  10. # verifico si el owner es root
  11. my @archivos = qw(
  12. /etc/crontab
  13. /etc/exports
  14. /etc/fstab
  15. /etc/group
  16. /etc/hosts
  17. /etc/hosts.allow
  18. /etc/hosts.deny
  19. /etc/inetd.conf
  20. /etc/inittab
  21. /etc/ld.so.conf
  22. /etc/modules
  23. /etc/motd
  24. /etc/mtab
  25. /etc/passwd
  26. /etc/profile
  27. /etc/securetty
  28. /etc/syslog.conf
  29. /bin/
  30. /boot/
  31. /etc/
  32. /etc/cron.daily/
  33. /etc/cron.hourly/
  34. /etc/cron.weekly/
  35. /home/
  36. /mnt/
  37. /lib/
  38. /root/
  39. /sbin/
  40. /tmp/
  41. /usr/
  42. /usr/bin/
  43. /var/
  44. );
  45. for my $archivo (@archivos) {
  46.     my $estatus = stat($archivo);
  47.  
  48.     if (! defined $estatus) {
  49.         print BOLD, RED, 'No existe', RESET, " El archivo $archivo no existe en el sistema o no puede ser accedido\n";
  50.     }
  51.     else {
  52.         if (! -r $archivo) {
  53.             print BOLD, RED, 'No leible', RESET, " El archivo $archivo no puede ser leido\n";
  54.         }
  55.  
  56.         if ( $estatus->uid == 0 ) {
  57.             #print BOLD, GREEN, "Positivo:", RESET, " El archivo $archivo tiene como propietario a root\n";
  58.         }
  59.         else {
  60.             my ($nombre,$password,$uid,$gid,$quota,$comentario,$gcos,$dir,$shell,$expire) = getpwuid($estatus->uid);
  61.             print BOLD, RED,   "Negativo:", RESET, " El archivo $archivo tiene como propietario a $nombre ($uid)\n";
  62.         }
  63.  
  64.         my $permisos = sprintf "%04o", $estatus->mode & 07777;
  65.  
  66.         if ( $permisos eq '0644' ) {
  67.  
  68.             #print BOLD, GREEN, "Positivo:", RESET, " El archivo $archivo tiene como permiso 0644\n";
  69.  
  70.         }
  71.  
  72.         else {
  73.  
  74.             print BOLD, RED, "Negativo:", RESET, " El archivo $archivo tiene como permiso $permisos\n";
  75.  
  76.         }
  77.     }
  78. }
  79.  
  80. ## Verificacion de Sticky Bit
  81.  
  82. print " -- Verificacion de Sticky Bit\n";
  83.     eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  84.         if 0; #$running_under_some_shell
  85.  
  86. use strict;
  87. use File::Find ();
  88.  
  89. # Set the variable $File::Find::dont_use_nlink if you're using AFS,
  90. # since AFS cheats.
  91.  
  92. # for the convenience of &wanted calls, including -eval statements:
  93. use vars qw/*name *dir *prune/;
  94. *name   = *File::Find::name;
  95. *dir    = *File::Find::dir;
  96. *prune  = *File::Find::prune;
  97.  
  98. sub wanted;
  99.  
  100.  
  101.  
  102. # Traverse desired filesystems
  103. File::Find::find({wanted => \&wanted}, '/');
  104. exit;
  105.  
  106.  
  107. sub wanted {
  108.     my ($dev,$ino,$mode,$nlink,$uid,$gid);
  109.  
  110.     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
  111.     (($mode & 01000) == 01000)
  112.     && print("$name\n");
  113. }
  114.  
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4

Re: Buscar sticky bit en todos los archivos

NotaPublicado: 2011-01-22 10:49 @492
por explorer
En mi ordenador, tu programa tarda en leerse todo el sistema de ficheros unos 5 minutos. Y al final, no encontró ningún fichero con el sticky bit :)

Re: Buscar sticky bit en todos los archivos

NotaPublicado: 2011-01-22 11:48 @533
por situ
explorer escribiste:En mi ordenador, tu programa tarda en leerse todo el sistema de ficheros unos 5 minutos. Y al final, no encontró ningún fichero con el sticky bit :)


Si ejecuto este:
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl -w
  2. no warnings;
  3. use File::Find ();
  4. use File::stat;
  5. # Modulos
  6. use Term::ANSIColor qw(:constants);
  7.  
  8. ## Verificacion de permisos en archivos y directorios
  9. print " -- Verificar permisos en archivos y directorios\n";
  10. # verifico si el owner es root
  11. my @archivos = qw(
  12. /etc/crontab
  13. /etc/exports
  14. /etc/fstab
  15. /etc/group
  16. /etc/hosts
  17. /etc/hosts.allow
  18. /etc/hosts.deny
  19. /etc/inetd.conf
  20. /etc/inittab
  21. /etc/ld.so.conf
  22. /etc/modules
  23. /etc/motd
  24. /etc/mtab
  25. /etc/passwd
  26. /etc/profile
  27. /etc/securetty
  28. /etc/syslog.conf
  29. /bin/
  30. /boot/
  31. /etc/
  32. /etc/cron.daily/
  33. /etc/cron.hourly/
  34. /etc/cron.weekly/
  35. /home/
  36. /mnt/
  37. /lib/
  38. /root/
  39. /sbin/
  40. /tmp/
  41. /usr/
  42. /usr/bin/
  43. /var/
  44. );
  45. for my $archivo (@archivos) {
  46.     my $estatus = stat($archivo);
  47.  
  48.     if (! defined $estatus) {
  49.         print BOLD, RED, 'No existe', RESET, " El archivo $archivo no existe en el sistema o no puede ser accedido\n";
  50.     }
  51.     else {
  52.         if (! -r $archivo) {
  53.             print BOLD, RED, 'No leible', RESET, " El archivo $archivo no puede ser leido\n";
  54.         }
  55.  
  56.         if ( $estatus->uid == 0 ) {
  57.             #print BOLD, GREEN, "Positivo:", RESET, " El archivo $archivo tiene como propietario a root\n";
  58.         }
  59.         else {
  60.             my ($nombre,$password,$uid,$gid,$quota,$comentario,$gcos,$dir,$shell,$expire) = getpwuid($estatus->uid);
  61.             print BOLD, RED,   "Negativo:", RESET, " El archivo $archivo tiene como propietario a $nombre ($uid)\n";
  62.         }
  63.  
  64.         my $permisos = sprintf "%04o", $estatus->mode & 07777;
  65.  
  66.         if ( $permisos eq '0644' ) {
  67.  
  68.             #print BOLD, GREEN, "Positivo:", RESET, " El archivo $archivo tiene como permiso 0644\n";
  69.  
  70.         }
  71.  
  72.         else {
  73.  
  74.             print BOLD, RED, "Negativo:", RESET, " El archivo $archivo tiene como permiso $permisos\n";
  75.  
  76.         }
  77.     }
  78. }
  79.  
  80. ## Verificacion de Sticky Bit
  81. print "sticky bit";
  82. use Term::ANSIColor qw(:constants);
  83. use strict;
  84. use File::Find ();
  85. use vars qw/*name *dir *prune/;
  86. *name   = *File::Find::name;
  87. *dir    = *File::Find::dir;
  88. *prune  = *File::Find::prune;
  89. sub wanted;
  90. # Traverse desired filesystems
  91. File::Find::find({wanted => \&wanted}, '/');
  92. sub wanted {
  93.     my ($dev,$ino,$mode,$nlink,$uid,$gid);
  94.     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) and
  95.     !($File::Find::prune |= ($dev != $File::Find::topdev)) and
  96.     -d _ and
  97.     (
  98.         (($mode & 01000) == 01000)
  99.     ) and
  100. print BOLD, WHITE, "Informacion:",RESET . " Se detecto sticky bit en $name\n"
  101. }
  102.  
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4


No ejecuta la parte de sticky bit.

Pero si ejecuto solamente la parte de sticky bit me anda perfecto.

Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. use Term::ANSIColor qw(:constants);
  2. use strict;
  3. use File::Find ();
  4. use vars qw/*name *dir *prune/;
  5. *name   = *File::Find::name;
  6. *dir    = *File::Find::dir;
  7. *prune  = *File::Find::prune;
  8. sub wanted;
  9. # Traverse desired filesystems
  10. File::Find::find({wanted => \&wanted}, '/');
  11. sub wanted {
  12.     my ($dev,$ino,$mode,$nlink,$uid,$gid);
  13.     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) and
  14.     !($File::Find::prune |= ($dev != $File::Find::topdev)) and
  15.     -d _ and
  16.     (
  17.         (($mode & 01000) == 01000)
  18.     ) and
  19. print BOLD, WHITE, "Informacion:",RESET . " Se detecto sticky bit en $name\n"
  20. }
  21.  
Coloreado en 0.001 segundos, usando GeSHi 1.0.8.4

Re: Buscar sticky bit en todos los archivos

NotaPublicado: 2011-01-22 12:13 @551
por explorer
Hay varios problemas, y uno de ellos es el comentado por wanako y por mi en el hilo Existencia de archivo y su propietario en Linux: el uso del módulo File::stat enmascara el uso de las primitivas stat() y lstat(), por lo que la línea 94 no es correcta. Debería ser CORE::lstat($_).

Además, como se indica en el mismo hilo, el uso de '_' con File::stat también es problemático, así que la línea 96 también dará problemas. Es mejor ponerla como -d $_.

Haciendo un poco de limpieza del código, queda así (y a mi me funciona):
Sintáxis: [ Descargar ] [ Ocultar ]
Using perl Syntax Highlighting
  1. #!/usr/bin/perl
  2. use strict;  
  3. use warnings;  
  4. use diagnostics;
  5.  
  6. use File::stat;  
  7. use File::Find ();
  8. use Term::ANSIColor qw(:constants);
  9.  
  10. ## Verificacion de permisos en archivos y directorios
  11. print " -- Verificar permisos en archivos y directorios\n";
  12.  
  13. my @archivos = qw(
  14.     /bin/
  15.     /boot/
  16.     /etc/
  17.     /etc/cron.daily/
  18.     /etc/cron.hourly/
  19.     /etc/crontab
  20.     /etc/cron.weekly/
  21.     /etc/exports  
  22.     /etc/fstab    
  23.     /etc/group
  24.     /etc/hosts  
  25.     /etc/hosts.allow
  26.     /etc/hosts.deny
  27.     /etc/inetd.conf
  28.     /etc/inittab  
  29.     /etc/ld.so.conf
  30.     /etc/modules
  31.     /etc/motd
  32.     /etc/mtab
  33.     /etc/passwd
  34.     /etc/profile
  35.     /etc/securetty  
  36.     /etc/syslog.conf
  37.     /home/
  38.     /lib/
  39.     /mnt/    
  40.     /root/
  41.     /sbin/
  42.     /tmp/
  43.     /usr/
  44.     /usr/bin/
  45.     /var/
  46. );
  47.  
  48. for my $archivo (@archivos) {
  49.     my $estatus = stat($archivo);
  50.  
  51.     if (! defined $estatus) {
  52.         print BOLD, RED, 'No existe', RESET, " El archivo $archivo no existe en el sistema o no puede ser accedido\n";
  53.     }
  54.     else {
  55.         if (! -r $archivo) {
  56.             print BOLD, RED, 'No leible', RESET, " El archivo $archivo no puede ser leido\n";
  57.         }
  58.  
  59.         if ( $estatus->uid == 0 ) {
  60.             #print BOLD, GREEN, "Positivo:", RESET, " El archivo $archivo tiene como propietario a root\n";
  61.         }
  62.         else {
  63.             my ($nombre,$password,$uid,$gid,$quota,$comentario,$gcos,$dir,$shell,$expire) = getpwuid($estatus->uid);
  64.             print BOLD, RED,   "Negativo:", RESET, " El archivo $archivo tiene como propietario a $nombre ($uid)\n";
  65.         }
  66.  
  67.         my $permisos = sprintf "%04o", $estatus->mode & 07777;
  68.  
  69.         if ( $permisos eq '0644' ) {
  70.  
  71.             #print BOLD, GREEN, "Positivo:", RESET, " El archivo $archivo tiene como permiso 0644\n";
  72.         }
  73.  
  74.         else {
  75.  
  76.             print BOLD, RED, "Negativo:", RESET, " El archivo $archivo tiene como permiso $permisos\n";
  77.         }
  78.     }
  79. }
  80.  
  81. ## Verificacion de Sticky Bit
  82. print "sticky bit\n";
  83.  
  84. use vars qw/*name *dir *prune/;
  85.  
  86. *name   = *File::Find::name;
  87. *dir    = *File::Find::dir;
  88. *prune  = *File::Find::prune;  
  89.  
  90. # Traverse desired filesystems
  91. File::Find::find({wanted => \&wanted}, '/');
  92.  
  93. sub wanted {
  94.     my ($dev,$ino,$mode,$nlink,$uid,$gid);
  95.  
  96.         (($dev,$ino,$mode,$nlink,$uid,$gid) = CORE::lstat($_))
  97.     and !($prune |= ($dev != $File::Find::topdev))
  98.     and -d $_
  99.     and (($mode & 01000) == 01000)
  100.     and print BOLD . WHITE . "Informacion:" . RESET . " Se detecto sticky bit en $name\n"
  101.     ;
  102. }
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4

Ahora me ha detectado tres directorios con sticky bit, en el /tmp :)