"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 Smiling
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 Laughing out loud

Share/Save/Bookmark

stas's picture
stas » August 15, 2007 » 21:20

Post new comment

*
*
The content of this field is kept private and will not be shown publicly.


*

  • Allowed HTML tags: <a> <i> <b> <u> <img> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <hr>
  • Lines and paragraphs break automatically.
  • Textual smileys will be replaced with graphical ones.