You are not logged in.

#1 2010-01-14 14:12:41

hatten
Arch Linux f@h Team Member
From: Sweden, Borlange
Registered: 2009-02-23
Posts: 736

Shell script that captures key input? (keylogger)

I want to record all my keystrokes, just to see the frequency of some keys. There is a bunch of keyloggers on google that captures keystrokes, but none that is good and is in the AUR. Isn't it possible to do just with a little shell script? I don't mind if i have to keep the terminal open, and there is no need for any daemon/advanced stuff. Could you record a /dev/something or similar?

Offline

#2 2010-01-14 15:21:33

tomd123
Developer
Registered: 2008-08-12
Posts: 565

Re: Shell script that captures key input? (keylogger)

so you tried lkl and pykeylogger? maybe a better solution would be to fix the problems that are in those packages rather from starting anew.

Offline

#3 2010-01-15 13:20:11

hBd
Member
From: Romania - Cluj Napoca
Registered: 2008-06-08
Posts: 241
Website

Re: Shell script that captures key input? (keylogger)

vim/nano  ? tongue (kidding)

Offline

#4 2010-01-15 15:01:29

hatten
Arch Linux f@h Team Member
From: Sweden, Borlange
Registered: 2009-02-23
Posts: 736

Re: Shell script that captures key input? (keylogger)

I read reviews of them, and they didn't seem overly good, and in one post it said something like "advanced users can do a simple script while newbies have to rely on programs like this" But I haven't found any simple scripts.

Maybe you could run

$ showkey

pipe the result to a file and then convert the keycodes to letters. Too bad that doesn't work in X.

Offline

#5 2010-01-16 12:51:57

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Shell script that captures key input? (keylogger)

You run into buffering problems, try this out:
sudo hexdump -e '48/1 "%x " "\n"' /dev/input/event1 | awk '( $29 == 1) { print "PRESS: " $13 } ( $29 == 0 ) { print "RELEASE: " $13 }'

It will collect a lot of events and print them all at once and it won't even redirect to a file.

So you want an all-in-one program like the ones in AUR. It should be pretty simple to write your own:

#! /usr/bin/python
file=open("/dev/input/event1")
keymap={
'\x2c': 'z',
'\x2d': 'x',
'\x2e': 'c',
'\xc8': 'UP',
'\xd0': 'DOWN',
'\xcb': 'LEFT',
'\xcd': 'RIGHT',
'\x10': 'q',
'\x11': 'w',
'\x12': 'e',
'\x01': 'ESC',
'\x2a': 'LSHIFT',
'\x1c': 'ENTER',
}
while True:
    event=file.read(48)
    if event[28] == '\x01':
        state="pressed:"
    elif event[28] == '\x00':
        state="released:"
    else:
        continue
    if event[12] in keymap:
        print state, keymap[event[12]]
file.close()

You could write an action game in curses like that!

Offline

#6 2010-01-16 14:17:44

hatten
Arch Linux f@h Team Member
From: Sweden, Borlange
Registered: 2009-02-23
Posts: 736

Re: Shell script that captures key input? (keylogger)

Thanks a lot, that's exactly what I were looking for. I'm sitting right now and tracking my number usage smile

Offline

#7 2010-01-16 14:52:51

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Shell script that captures key input? (keylogger)

Haha, check this out: (cursor=move, z=fire, q=quit)

#! /usr/bin/python
from curses import *
import threading

class ckeylog( threading.Thread ):
    def run(self):
        file=open("/dev/input/event1","rb")
        keymap={
        '\x2c': 'z',
        '\x2d': 'x',
        '\x2e': 'c',
        '\xc8': 'UP',
        '\xd0': 'DOWN',
        '\xcb': 'LEFT',
        '\xcd': 'RIGHT',
        '\x10': 'q',
        '\x11': 'w',
        '\x12': 'e',
        '\x01': 'ESC',
        '\x2a': 'LSHIFT',
        '\x1c': 'ENTER',
        }
        self.loop=True
        while self.loop:
            event=file.read(48)
            if event[28] == '\x01':
                state=True
            elif event[28] == '\x00':
                state=False
            else:
                continue
            if event[12] in keymap:
                self.map[keymap[event[12]]]=state
        file.close()
    def quit(self):
        self.loop=False

keylog=ckeylog()
keylog.map={}
keylog.start()
def main(s):
    curs_set(0)
    ship=[
"| /\ |",
"+/!!\+",
"[]--[]",
]
    shipx=0
    shipy=0
    bullet="*    *"
    bulletx=0
    bullety=0
    fire=False
    while True:
        (my,mx)=s.getmaxyx()
        if 'q' in keylog.map:
            if keylog.map['q']: keylog.quit(); return
        if 'UP' in keylog.map:
            if keylog.map['UP']: shipy=shipy-1
        if 'DOWN' in keylog.map:
            if keylog.map['DOWN']: shipy=shipy+1
        if 'RIGHT' in keylog.map:
            if keylog.map['RIGHT']: shipx=shipx+1
        if 'LEFT' in keylog.map:
            if keylog.map['LEFT']: shipx=shipx-1
        if 'z' in keylog.map:
            if keylog.map['z']: (fire,bullety,bulletx)=(True,shipy,shipx)
        if shipy<0: shipy=0
        if shipy>my-len(ship): shipy=my-len(ship)
        if shipx<0: shipx=0
        if shipx>mx-len(ship[0]): shipx=mx-len(ship[0])
        if fire and bullety == 0: fire=False
        if fire: bullety=bullety-1
        s.erase()
        s.addstr(shipy,shipx,ship[0])
        s.addstr(shipy+1,shipx,ship[1])
        try:
            s.addstr(shipy+2,shipx,ship[2])
        except error: pass
        if fire: s.addstr(bullety,bulletx,bullet)
        s.refresh()
        napms(10)
    


wrapper(main)

Offline

#8 2010-01-20 08:00:11

hatten
Arch Linux f@h Team Member
From: Sweden, Borlange
Registered: 2009-02-23
Posts: 736

Re: Shell script that captures key input? (keylogger)

haha, too bad it lags a little, and need sudo to run. I succeeded in crashing it, here's the output if you want =p

Traceback (most recent call last):
  File "./game", line 86, in <module>
    wrapper(main)
  File "/usr/lib/python2.6/curses/wrapper.py", line 44, in wrapper
    return func(stdscr, *args, **kwds)
  File "./game", line 80, in main
    if fire: s.addstr(bullety,bulletx,bullet)
_curses.error: addstr() returned ERR

I happens when you hold the fire button and go out of the screen up or to the left.

It also freezes randomly =p

Offline

#9 2010-01-20 15:13:48

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Shell script that captures key input? (keylogger)

Hah yeah, those bugs should be easy to fix, but the 10 ms sleep probably isn't a good game routine. I'm going to leave it at that though...

Offline

#10 2010-08-24 01:41:57

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Shell script that captures key input? (keylogger)

Maybe it's because I have a USB keyboard, but when I tried this again it wasn't working anymore.

The length of a key event is now 1.5x bigger (it took me a while to find out - or maybe I was wrong all along)

Data collection:
sudo hexdump -e '72/1 "%x " "\n"' /dev/input/event0

Here is an example script that doesn't use a keyboard map, but just checks to see if there are any keys currently depressed, and if so turn on a LED (I have a new laptop and I am using this right now, however I could only find one single LED to use, and it's tiny, shared by hard disk usage and exactly where you would put your left wrist - search for LEDs continues, because this seems quite fun to use)

#! /usr/bin/python
import sys,signal

#config
LED=open("/sys/class/leds/hp::hddprotect/brightness",'w')
KBD=open("/dev/input/event0",'rb')

def clean(un,used):
        ledwrite("0")
        sys.exit(0)
signal.signal(signal.SIGTERM, clean)
signal.signal(signal.SIGINT, clean)
signal.signal(signal.SIGHUP, clean)
signal.signal(signal.SIGQUIT, clean)

def ledwrite(state):
        LED.write(state)
        LED.flush()

count=0
while True:
        event=KBD.read(72)
        if event[44] == '\x01':
                if count == 0:
                        ledwrite("1")
                count+=1
        elif event[44] == '\x00':
                count-=1
                if count == 0:
                        ledwrite("0")
        if count < 0:
                count = 0
                ledwrite("0")

Offline

#11 2010-08-26 07:27:04

hatten
Arch Linux f@h Team Member
From: Sweden, Borlange
Registered: 2009-02-23
Posts: 736

Re: Shell script that captures key input? (keylogger)

What is the reason for that? Isn't it easier to just look at your fingers?

Offline

Board footer

Powered by FluxBB