[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Passworte mit keygen.pl



Hi.


* Martin Kurz <martin@kurz.net> [041128 17:37]:
[...]
> Falls es sehr kompliziert ist, hier ein Script oder
> sonst etwas zu ?ndern,  w?re ich f?r einen weiteren
> Tipp auch sehr dankbar.
Hier (vgl. Anhang)ist ein Perl-Script:
-> http://www.prenzel.de/perl/scripts/keygen.pl


Peter
#!/usr/bin/perl
#!/usr/local/bin/perl -w
#This is a key/password generator tool.
#CVS --> $Revision: 1.7 $ <steffen@dett.de>

$CHARSET_EXTENTION='';	#Alphanum plus that chars
			#chars A-Za-z0-9 plus EXTENSION will be used for keys
$PW_COUNT=0;		#number of passwords, 0 means manual ( "more [y] ?" )
$NO_KEY_TYPEING=0;	#keyboard hits for enthropy (skip flag)
			#no interaction needed if set (auto mode)
$QUIET_MODE=0;		#silent operation flag (mininum output if set)

sub parse_command_line()
{#parses command line and sets global control vars
	my $param;
PARAM:	foreach $param (@ARGV) {
		if ($param =~ m/^(-h|--help|-?)$/i) {
			#help switch
			param_help();	#output possible switch
			exit 0;	
		}
		if ($param =~ m/^-a$/i) {
			#auto mode
			$NO_KEY_TYPEING = 1;
			next PARAM;
		}
		if ($param =~ m/^-q$/i) {
			#quiet mode (minimum output)
			$QUIET_MODE = 1;
			next PARAM;
		}
		if ($param =~ m/^(\d+)$/) {
			if ($PW_COUNT != 0) {
				print "Overriding counter ($PW_COUNT) with new"
				. " value ($1).\n";
			}
			$PW_COUNT=$1;
			next PARAM;
		}
		print "Unknown command line option: \"$param\"!\n";
		print "  (invoke \"$0 --help\" for command summary)\n"; 
	}
}

sub param_help()
{#output of possible command line parameters/switches
	print "Key generator tool. Generates random passwords.\n";
    print "GPL applies. Absolutly no warranty. [c] <steffen\@dett.de>\n";
	print "Command line parameters:\n";
	print "		<number>	count of passwords to be generated\n";
	print "		-a		automatic operation "
		. "(no keyboard hits)\n";
	print "		-q		quiet mode "
		. "(silent operation if -a too)\n";
}

sub keyboard_seed()
{
	return 0 if ($NO_KEY_TYPEING);		#do nothing if set

	#C would be better... ;-)
	my $time_sum=0;		#"rotated" value for keystroke timings
	my $key_sum=0;		#"rotated" value for keycodes
	my $rin='';		#select() bit vector 
	vec($rin,fileno(STDIN),1) = 1; 	#construct STDIN select vector
	$r=$rin; 			#select-vector
	system "stty", '-icanon', '-echo', 'eol', "\001";
	#set line discipline: non canonical mode no echo
	select(STDOUT); $|=1;	#set STDOUT unbuffered
	print	"I have to collect some enthropy. ",
		"I'll analyze you keyboard hits.\n";
	print "Please hit 32 keys now: [", " " x 32, "]\n";
	print "                    ---> ";
	for ($n=0; $n<32; $n++) {	#32 keys
		my $one_time=0;		#time between two keystrokes
		my $c='';		#1 byte input buffer
		while (($rin=$r,$nfound = select($rin, undef, undef, 0.01)) == 0) {
			#wait until data on STDIN
			$one_time++;	#a time tick without data
			$one_time %= 2**8; #we take 4 bit only...
		}
		sysread(STDIN, $c, 1);	#read char from STDIN
		print "*";		#just a mark
		#add some bits to sum:
		my $b4 = ((4 ** $n)%(2**16));  #max 2 bytes (factor)
		my $b6 = ((6 ** $n)%(2**16));  #max 2 bytes (factor)
		$time_sum = $time_sum + $one_time * $b4; #4 bits (a "shift" in)
		$key_sum = $key_sum + ( ord($c)-ord('0')) * $b6;#6 bits
	}
	print "\nENOUGH, Thank you!\n\n";
	sleep 2;
	#now we have to flush input (for such users that type 33 chars :-)
	while (($rin=$r,$nfound = select($rin, undef, undef, 0)) != 0) {
		sysread(STDIN, $rin, 1); #trash input
	}
	#restore line discipline mode
	system "stty", 'icanon', 'echo', 'eol', '^@'; # ASCII null
	select(STDOUT); $|=0;	#set line buffered mode
	my $seed = $time_sum ^ $key_sum ;	#XOR timer and key longs
	printf "  (Keyboard seed value: 0x%X [T:0x%X K:0x%X])\n", 
		$seed, $time_sum, $key_sum unless($QUIET_MODE);
	return $seed;	#pass seed to caller
}

sub rnd_init()
{
	#calling srand with entrophy...
	#we "hash" some "ps" process info and collect enthropy from user
	open(OLDERR, ">&STDERR") or die "STDERR->OLDERR $!\n";
	open(STDERR, ">/dev/null") or die "STDERR->/dev/null $!\n";
	close(STDERR);
	#"redirect" STDERR; it seems that IT DOES NOT WORK CORRECTLY!!
	#$seed1 = (unpack "%L*", `find /proc -print | xargs cat gzip`);
	$seed1 = keyboard_seed();
	$seed2 = (time ^ $$ ^ unpack "%L*", `ps axww | gzip`);
	$seed = $seed1 ^ $seed2;
#next line DOES NOT WORK!!! 
	open(STDERR, ">&OLDERR") or print OLDERR "OLDERR->STDERR $!\n";
	close (OLDERR);
	printf "  --> 32 bits enthropy: 0x%X <--\n", $seed
		unless ($QUIET_MODE);
	srand($seed);	#set calculated seed
}

sub random_char($)
{
	#returns a char from allow set of pw chars
	my $CHARSET = shift;
	return (substr($CHARSET,rand(length($CHARSET)),1));
}

sub build_charset()
{
	#return A-Z, a-z and 0-9
	#C-Style
	my $CHARSET="";
	my $n;
	for ($n=ord('a'); $n <= ord('z'); $n++) {
		$CHARSET.=chr($n);
	}
	for ($n=ord('A'); $n <= ord('Z'); $n++) {
		$CHARSET.=chr($n);
	}
	for ($n=ord('0'); $n <= ord('9'); $n++) {
		$CHARSET.=chr($n);
	}
	return $CHARSET . $CHARSET_EXTENTION;
}

sub build_charset_perl()
{
	#return A-Z, a-z and 0-9
	#Perl-Style
	#PERL MAGIC :-)
	my $CHARSET="";
	my $n;
	for ($n='a'; $n ne "z"; $n++) {
		$CHARSET.=$n;
	}
	$CHARSET.=$n;
	for ($n='A'; $n ne "Z"; $n++) {
		$CHARSET.=$n;
	}
	$CHARSET.=$n;
	for ($n='0'; $n ne "9"; $n++) {
		$CHARSET.=$n;
	}
	$CHARSET.=$n;
	return $CHARSET . $CHARSET_EXTENTION;
}

sub print_pw($)
{
	#generate and print the password
	my $CHARSET=shift;
	my $n;
	print 'Generated password: "' unless ($QUIET_MODE);
	for ($n=0; $n<8; $n++) {
		print random_char($CHARSET);
	}
	print '"'  unless ($QUIET_MODE);
}

parse_command_line();	#set vars according to the switches

print "\n" unless ($QUIET_MODE);
rnd_init;		#build a seed (enthropy)
print "\n" unless ($QUIET_MODE);

my $CHARSET=build_charset();

if ($PW_COUNT == 0) {
	#manual mode
	while("true") {
		#loop until user stops
		print_pw($CHARSET);
		print ' ... more [y]? ';
		$INPUT = lc(<STDIN>);
		chomp($INPUT);
		last if (($INPUT ne "") and ($INPUT ne "y"));
	}
} else {
	#automode
	my $n;
	for ($n=0; $n<$PW_COUNT; $n++) {
		print_pw($CHARSET);
		print "\n";
	}
}

print "\n" unless ($QUIET_MODE);


Reply to: