Skip to content

Instantly share code, notes, and snippets.

@alirezaarzehgar
Created October 23, 2022 11:47
Show Gist options
  • Select an option

  • Save alirezaarzehgar/d6d0ca01eb0948b5ada3fe189b53c08a to your computer and use it in GitHub Desktop.

Select an option

Save alirezaarzehgar/d6d0ca01eb0948b5ada3fe189b53c08a to your computer and use it in GitHub Desktop.
su command example for learning setuid & setgid in c
CC = cc
SRC = $(wildcard *.c)
OBJ = $(SRC:.c=.o)
TARGET = su
all: $(TARGET)
$(TARGET): $(OBJ)
$(CC) $(OBJ) -o $(TARGET)
%.o: %.c
$(CC) -c $< -o $@
install: $(TARGET)
chown root:root $(TARGET)
chmod 4755 $(TARGET)
clean:
rm -rf $(TARGET) $(OBJ)
#include <unistd.h>
#include <pwd.h>
int main(int argc, char const *argv[])
{
struct passwd *pw = getpwnam("root");
setgid(0);
setuid(0);
execlp(pw->pw_shell, pw->pw_shell, NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment