( zamboriz | 2012. 03. 03., szo – 18:03 )

Átírtam használhatóbbra. A kód parancssori paramétert tölt ki (fájlból, sztenderd inputból, környezeti változóból) és kérésre el is tünteti.

teszt szkript:


echo args: "0:'$0'" "1:'$1'" "2:'$2'" "3:'$3'" "4:'$4'" "5:'$5'" "6:'$6'" "7:'$7'"  "8:'$8'"  "9:'$9'"
ps -f $$
echo "$(cat /proc/self/environ | xargs -0 -n 1 echo | grep TITOK)"

futtatás:


echo "titok.txt: '$(cat titok.txt)'"

LD_PRELOAD=./loadarg.so TITOK=asdf1234 ./test.sh  [LoadFromFile]titok.txt [LoadFromStdIn] [Hide][LoadFromEnv]TITOK [Hide]titok

eredmény:


$ ./_run.sh
titok.txt: 'fajlbol'

args: 0:'./test.sh' 1:'fajlbol' 2:'eztgepeltembe' 3:'asdf1234' 4:'titok'

... CMD
... /bin/bash ./test.sh [LoadFromFile]titok.txt [LoadFromStdIn] ************************ ***********

TITOK=********

A kód:


#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

static char buf[256];

static void init( int argc, char **argv, char **envp __attribute__((unused)) ){
  for( int i=0; i<argc; i++ ){
    int hide = 0;
    if( !strncasecmp( argv[i], "[Hide]", 6 ) ){
      hide = 1;
      char *orig = strdup( argv[i]+6 );
      for( char *c = argv[i]; *c; *c++ = '*' );
      argv[i] = orig;
    }
    if( !strncasecmp( argv[i], "[LoadFromStdIn]", 15 ) ){
      int len = read( 0, buf, 255 );
      if( 0 > len ) len = 1;
      buf[len-1] = 0;
      argv[i] = strdup( buf );
    }
    else if( !strncasecmp( argv[i], "[LoadFromFile]", 14 ) ){
      int fd = open( argv[i]+14, O_RDONLY );
      int len = read( fd, buf, 255 );
      close( fd );
      if( 0 > len ) len = 0;
      argv[i] = strdup(buf);
    }
    else if( !strncasecmp( argv[i], "[LoadFromEnv]", 13 ) ){
      char *e = getenv( argv[i]+13 )?:"";
      argv[i] = strdup( e );
      if( hide ){ for( char *t=e; *t; *t++='*' ); }
    }
  }
}

__attribute__((section(".init_array"))) typeof(init) *__init = init;

fordítás:


gcc -std=gnu99 -shared -fPIC -o loadarg.so  loadarg.c