Un bout de Perl avec la commande nm
Lors d’un récent entretien chez eNovance, ces derniers m’ont demandé de faire un script permettant de lister les fonctions actuellement en mémoire des librairies statiques présentes dans /usr/lib/.
Cet exercice était à faire en Perl, hors cela faisait un bout de temps que je n’avais pas pratiqué ce langage. Ils ont été plutôt sympas sur ce point et ils m’ont autorisé à le faire en Bash.
Pour le fun (oui pour le fun), j’ai tenté l’exercice en Perl. Les librairies statiques dans /usr/lib/ sont celles qui se terminent par .a. Une fois les librairies listées, la commande nm permet de lister les symboles (fonctions) en cours d’utilisation.
Exemple
$ nm /usr/lib/libasprintf.a
lib-asprintf.o: nm: lib-asprintf.o: no symbols autosprintf.o: 0000000000000000 t _GLOBAL__sub_I__ZN3gnu11autosprintfC2EPKcz 0000000000000000 T _ZN3gnu11autosprintfC1EPKcz 00000000000000b0 T _ZN3gnu11autosprintfC1ERKS0_ 0000000000000000 T _ZN3gnu11autosprintfC2EPKcz 00000000000000b0 T _ZN3gnu11autosprintfC2ERKS0_ 00000000000000d0 T _ZN3gnu11autosprintfD1Ev 00000000000000d0 T _ZN3gnu11autosprintfD2Ev 00000000000000e0 T _ZNK3gnu11autosprintfcvPcEv 0000000000000150 T _ZNK3gnu11autosprintfcvSsEv U _ZNSsC1EPKcRKSaIcE U _ZNSt8ios_base4InitC1Ev U _ZNSt8ios_base4InitD1Ev 0000000000000000 b _ZStL8__ioinit U _Znam U __cxa_atexit U __dso_handle U __vasprintf_chk U free U memcpy U strdup U strlen
Ici les fonctions utilisées sont celles ayant le flag T.
- T : le symbole est dans la zone mémoire .text (code)
Le script
#!/usr/bin/perl -w # ###################################################################### # # List all functions from static libraries currently used. # # Author : Gaetan Trellu (goldyfruit) <gaetan.trellu@incloudus.com> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # ########################################################################### use strict; use warnings; # Path to static libraries my $libPath = "/usr/lib"; # Open the libraries path and read the content opendir(DIR, $libPath) or die $!; # Keep only library who finished by .a my @libraries = grep { /\.a$/ } readdir(DIR); # Define arrays (associative and normal) my %array; my @tab; # Print the content at 70 chars from the left to the right for the first column # and 50 chars from the first column for the second column print "################################################################################\n"; printf("%-70s %-50s\n", "# Function currently used", "Library #"); print "################################################################################\n"; # For each librairy found foreach my $library (@libraries) { # Find all functions used (T flag) in static library # "nm" command is used to list the status of a library # Result is push in a standard array my @functionsArray = grep { /T/ } `nm $libPath/$library`; # Explode the function by using space and push the result in the @tab array foreach my $function (@functionsArray) { @tab = split(/ /, $function); # If the second field is equal to T then space, chariot # are escaped with the chomp() function # Result is push in the associative array %array if ($tab[1] eq "T") { chomp($tab[2]); $array{$tab[2]}=$library; } } } # Each result in the %array array is display foreach my $key (sort keys %array) { printf("%-70s %-50s\n", $key, $array{$key}); } print "\n"; # Close the libraries directory closedir(DIR); exit 0;
Lien
- http://www.thegeekstuff.com/2012/03/linux-nm-command/
- http://www.cgsecurity.org/Articles/1-MISC/Protections-1/index.html
Gaëtan Trellu (goldyfruit)
Derniers articles parGaëtan Trellu (goldyfruit) (voir tous)
- Qinling, let’s the journey begin! - 23 mai 2019
- systemd-networkd, l’âge de la maturité ? - 13 mars 2018
- Hyper-V, Nova, VxLAN et Open vSwitch, enfin une belle histoire ! - 31 décembre 2017
Fonctions utilisées d’une librairie statique par Gaëtan Trellu (goldyfruit) est sous Licence Creative Commons Internationale Attribution 4.0.