"keyboard injection" under Linux
What to do if you want to pass some data into STDIN of some process,
and it doesn't accepts it?! One of the most (un)famous example is ssh
;
who never tried to do a "echo password | ssh host"
?!
So, you just want to inject characters as there were someone
typing them. Remember: TIOCSTI ioctl() call is your
friend
Just google it to see what
it does. And now, some actually working code:
#!/usr/bin/perl -w
use strict;
use Fcntl;
use constant TIOCSTI => 0x5412;
unless (@ARGV >= 1) {
print "usage: $0 [vt] <input>\n";
exit;
}
my $vt = shift @ARGV;
my $buf = join '', <>;
&writevt ($vt, $buf) || die "can't write to $vt: $!\n";
exit;
sub writevt {
my ($vt, $buf) = @_;
sysopen (VT, $vt, O_RDONLY) || return 0;
for (my $i = 0; $i < length $buf; $i++) {
ioctl (VT, TIOCSTI, substr ($buf, $i, 1)) || return 0;
}
close (VT) || return 0;
return 1;
}
And this is how it works (Linux-specific!!!):
[stas@home stas]$ echo id | ./catvt.pl /dev/pts/0
id
[stas@home stas]$ id
uid=?????(stas) gid=?????(stas) groups=?????(stas)
Kinda awesome
stas » August 15, 2007 » 21:20
9492 reads
|