keylogger icon indicating copy to clipboard operation
keylogger copied to clipboard

why does it need root privilages?

Open wlwatkins opened this issue 4 years ago • 1 comments

Everything is in the title. I know this prevents the making of a malicious keylogger, but I'm genuinely asking out of curiosity

wlwatkins avatar Aug 08 '21 12:08 wlwatkins

hey @willmendil . In linux, everything is a file so as the input devices (keyboard, mouse etc..) If you look at the /dev/input on any linux distro you will see the files that represents events from which you can read from. To read from the file, your user needs to be in correct group. For example, on ubuntu there are groups (plugdev and input). So, to run without root, you need to add yourself (a user) to that group and you can run this without root permission.

Why was implemented to run as root? I really dont know why(it was a long time ago when I started this) and probably it would be a good change to detect the running user to see if he is in a allowed group or root.

MarinX avatar Aug 12 '21 16:08 MarinX

Hi @MarinX I am Unable to Push code (PR) Here I am attaching code for root permission please give me permission so I can contribute continue ...

Thanks In Advance (INDIA)

keylogger.go

// requestRootPermission attempts to request root permissions using sudo.

func requestRootPermission() error {
	cmd := exec.Command("sudo", os.Args[0])
	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	return cmd.Run()
}

// checkRoot checks if the program is running with root permissions and requests them if needed. // It returns true if root permissions are obtained or already present, false otherwise.

func (k *KeyLogger) IsRoot() bool {
	// Check if root permissions are needed
	if syscall.Getuid() != 0 && syscall.Geteuid() != 0 {
		fmt.Println("Requesting root permissions...")
		err := requestRootPermission()
		if err != nil {
			return false // Unable to obtain root permissions
		}
		fmt.Println("Running with root permissions!")
		return true // Root permissions obtained successfully
	}
	return true // Root permissions are already available
}

anujkumarthakur avatar Aug 23 '23 17:08 anujkumarthakur

@anujkumarthakur thank you for the comment and explanation. Requesting root permission, especially from a library is not a good practice. The user of the library should decide what to do if he cannot read a keyboard event (maybe it's not related to root permissions). If you wrap this into a CLI app - then yes, it makes sense to request the root if all other options fail.

A better PR would be to return the error message or error code so the user can decide in his own app to do the next action (eg request root permission by his own implementation)

MarinX avatar Aug 31 '23 18:08 MarinX