Skip to content

Instantly share code, notes, and snippets.

@maxux
Last active November 29, 2021 08:01
Show Gist options
  • Save maxux/ad1fc2f57654581bab339363ed7e6db2 to your computer and use it in GitHub Desktop.
Save maxux/ad1fc2f57654581bab339363ed7e6db2 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <linux/reboot.h>
#include <sys/reboot.h>
#include <sys/syscall.h>
#include <linux/kexec.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#define KERNEL_BASEURL "https://bootstrap.gig.tech/kernel/"
#define KERNEL_FILE "zero-os-HEAD-0-core-1cfdbc6ff9.efi"
void diep(char *str) {
perror(str);
exit(EXIT_FAILURE);
}
int main(void) {
int kfd;
char cmdline[2048];
printf("[+] downloading kernel\n");
chdir("/tmp/");
system("wget " KERNEL_BASEURL KERNEL_FILE);
printf("[+] reading current kernel cmdline\n");
if((kfd = open("/proc/cmdline", O_RDONLY)) < 0)
diep("/proc/cmdline");
if(read(kfd, cmdline, sizeof(cmdline)) < 0)
diep("read");
close(kfd);
printf("[+] opening new kernel file\n");
if((kfd = open("/tmp/" KERNEL_FILE, O_RDONLY)) < 0)
diep("kernel open");
printf("[+] loading new kernel into memory\n");
if(syscall(SYS_kexec_file_load, kfd, 0, strlen(cmdline) + 1, cmdline, KEXEC_FILE_NO_INITRAMFS) < 0)
diep("kexec_file_load");
// here we should kill SIGTERM -1
printf("[+] flushing buffers\n");
sync();
printf("[+] booting the new kernel\n");
reboot(LINUX_REBOOT_CMD_KEXEC);
// never reached anyway
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment