http://qs321.pair.com?node_id=178588

SMA has asked for the wisdom of the Perl Monks concerning the following question:

I need to write a keylogger which will be running as a Linux daemon for our System Programming Course assignment. I can do it with C ( with a kernel module ), but this assignment must be done with Perl. Can you help me how can it be possible to get all keypress events with Perl both on console and X ? I cannot figure out how to handle these events, what modules can be useful, etc? If you can show me the right way to do it ,or give any idea, i think i could write it pretty fast. Thanks in advance... SMA

Replies are listed 'Best First'.
Re: perl keylogger on linux
by hacker (Priest) on Jul 01, 2002 at 21:41 UTC
    Since this is for your System Programming course, I won't give you the exact answer here, but I can definately give you enough to help
    "..teach a man to fish.."

    cjf, myself, and others discussed this briefly on the CB soon after it was posted, and I gave up a few ideas on how to solve this. First a few key questions have to be asked:

    1. Will you have access to create character devices on the target machine?
    2. Will you be able to rebuild a kernel or reboot the kernel on the target machine?
    3. Can you run a daemon on the user machine, global to all processes?
    4. Are you limited to using pure perl? Or can you use C also?
    With this, you could begin to construct several plans of attack:
    • Write a kernel module (does not require a reboot, but requires sufficient access to load it into userspace) which will log all calls through the kernel keyboard driver to a file via normal kernel printk() mechanisms.
    • Create a character device in /dev that you can peek from and write to a file (requires root access to create devices)
    • Set up a userspace daemon that polls for input, a 'shim' between keyboard driver and input application.
    • Work with Inline::C and integrate it with your perl code
    Some other modules you may be able to use at your disposal are:
    • Net::Daemon to create your daemon process in userspace.
    • IPC::open3 to allow you to open a process for for reading/writing/error reporting
    • Standard pipe() function for opening and dealing with your system calls..
    • The source to xev, the X Event Interface could give you some ideas also
      Hi,
      - I will have root access to target computers. My project aims to watch a large computer lab during exam hours only, since exams are made by a web interface, grabbing keys in X windows is more important.
      - I have already written a kernel module in C and am able to grab all keys regardless of console or X. But this assignment must be done in pure Perl.
      Also I have tried TK yesterday, but it only grabs key events from a focused window, global grabbing blocks all inputs so it doesn't suit my needs. Today I'll be trying Qt and X Event Interface. I hope listening X events will solve my problem.
      Thank you very much for help :-)
Re: perl keylogger on linux
by perigeeV (Hermit) on Jul 01, 2002 at 18:05 UTC

    Term::ReadKey, Term::ReadLine, and Term::VT102 on CPAN will be a help. You can roll your own with getc and print statements if you want.

    I recommend reading up on terminal emulation and stty.

    xterm windows are just a /dev/pty* file that can be read and written. Type tty at a prompt. It will tell you the terminal.

    Read perlfaq8.


Re: perl keylogger on linux
by Trimbach (Curate) on Jul 01, 2002 at 15:53 UTC
    Seeing as how you're the one taking the Systems Programming Course it seems to me that you should be the one doing your homework. We're awfully helpful to people around here, but you'll find an enormous reluctance to do your work for you, especially when (it appears) you've done exactly zero of the work so far.

    Try to solve the problem yourself. If you have a (very specific) question about something, please feel free to post it.

    Gary Blackburn
    Trained Killer

      While it's true that no one here will do someone's homework for them, I don't think this particular post deserves to be brushed off so hastily.

      SMA said, "If you can show me the right way to do it ,or give any idea, i think i could write it pretty fast." This doesn't sound to me like someone asking to have their project written for them. I see nothing unreasonable about someone asking for guidance with their homework. Telling someone to RTFM is often appropriate when people are asking about easily-found technical details, but manuals rarely help when you don't have a starting point. I see nothing wrong with telling folks to go look at module X, or read merlyn's article on Y, even if it is for someone's homework.

      Just my $0.02.


      _______________
      D a m n D i r t y A p e
      Home Node | Email
        I don't buy it. CPAN is not exactly a secret, especially to someone who has, according to his own admission "used Perl for years." If you're in a class part of your education isn't just writing, it's researching which means putting some level of personal effort into finding out where the answer may be without cutting to the chase and asking those that already know. It's certainly convenient for the student, though ultimately not as educational as doing the grunt work yourself.

        It's kinda amusing seeing the reputation on my original node swing wildly up and down... I deliberately posted first so that I could say in a nice way what lots of other Monks would have said much less politely. The poster is new and doesn't need to be abused... I was trying to save him some abuse and I get beat up on instead.

        Ah well... to each his own. :-D

        Gary Blackburn
        Trained Killer

      Okay, this thread has inspired me to start a new project.

      The project will be a keylogger written entirely in Perl. It needs to run on Linux and read keystrokes entered on the command line and in X. I've done searches both here and on CPAN for 'keylogger' and 'term' but didn't find anything relevant.

      Any suggestions as to how I'd get started on this project? Thanks in advance for the replies :).

        if u want to write a simple key logger in windows this would be how to do it
        use feature ':5.10'; while(10) { $input = <>; #this creats an infinite loop last if $input eq "exit/n"; #exit loop when user presses exit then ent +er open(FILE, ">>log.txt"); print FILE $input; } close FILE;
      Sorry but I didn't want anyone do it for me. I just needed a few ideas on "how could it be done on Linux with Perl" I have posted this question because I'm not an experienced Linux user, but I have been using Perl for CGI for years. All i needed was some advice like "You can try .... for X , and .... for console". Thanks again :-)
        I guess that Term::Readkey would be where you want to look. But im really not that sure.

        Yves / DeMerphq
        ---
        Writing a good benchmark isnt as easy as it might look.