Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
doronz88 committed Jan 24, 2022
0 parents commit a42a5bc
Show file tree
Hide file tree
Showing 8 changed files with 1,243 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Object files
*.o
*.ko
*.obj
*.elf

# Precompiled Headers
*.gch
*.pch

# Libraries
*.lib
*.a
*.la
*.lo

# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib

# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
c/client
c/server

# Debug files
*.dSYM/

# macOS shit
.DS_Store

# ide files
.vscode

675 changes: 675 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# zShell

## Description

Inspired by [inSecure-SHell](https://github.com/fffaraz/inSecure-SHell), this shell's goal is to be the tinyest and simplest remote shell.

## Building

```shell
cd c
make all
```

## Running

To execute the server:

```shell
./server [-p port] [-s shell]
```

Connecting via:

```shell
./connect HOST
```
12 changes: 12 additions & 0 deletions c/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CC=gcc

common: common.c
CC -c common.c -o common.o

server: server.c common.o
CC server.c common.o -o server

client: client.c common.o
CC client.c common.o -o client

all: server client
178 changes: 178 additions & 0 deletions c/client.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <termios.h>
#include <unistd.h>

#define DEFAULT_PORT "5910"
#define BUFFERSIZE (64 * 1024)

struct termios saved_attributes;
void reset_input_mode()
{
tcsetattr(STDIN_FILENO, TCSANOW, &saved_attributes);
}

void readline(int sockfd, char *buf)
{
int pos = 0;
char c;
for (;;)
{
int n = recv(sockfd, &c, 1, 0);
if (n < 1)
break;
buf[pos++] = c;
if (c == '\n')
break;
}
buf[pos] = '\0';
}

void sigint_handler(int signum)
{
printf("\n\nCaught signal: %d\n\n", signum);
exit(signum);
}

void print_addrinfo(struct addrinfo *input)
{
int addr_i = 0;
for (struct addrinfo *p = input; p != NULL; p = p->ai_next)
{
char *ipver;
void *addr;

// get the pointer to the address itself, different fields in IPv4 and IPv6
if (p->ai_family == AF_INET)
{
ipver = "IPv4";
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
}
else
{
ipver = "IPv6";
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
}

char ipstr[INET6_ADDRSTRLEN];
inet_ntop(p->ai_family, addr, ipstr, sizeof(ipstr)); // convert the IP to a string
printf("%2d. %s: %s\n", ++addr_i, ipver, ipstr);
}
}

int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "usage: %s hostname\n", argv[0]);
return 1;
}

signal(SIGINT, sigint_handler);

printf("Looking up addresses for %s ...\n", argv[1]);

struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;

struct addrinfo *dnsres;
int status = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &dnsres);
if (status != 0)
{
fprintf(stderr, "dns lookup failed: %s\n", gai_strerror(status));
return 2;
}

print_addrinfo(dnsres);

printf("Connecting to %s ...\n", "the server");
int sockfd = socket(dnsres->ai_family, dnsres->ai_socktype, dnsres->ai_protocol);

if (connect(sockfd, dnsres->ai_addr, dnsres->ai_addrlen) != 0)
{
perror("connect");
return 3;
}
printf("Connected.\n");

freeaddrinfo(dnsres); // frees the memory that was dynamically allocated for the linked lists by getaddrinfo

char buf[BUFFERSIZE + 1];
int nbytes, mbytes;

// Make sure stdin is a terminal.
if (!isatty(STDIN_FILENO))
{
fprintf(stderr, "Not a terminal.\n");
exit(EXIT_FAILURE);
}

// Save the terminal attributes so we can restore them later.
tcgetattr(STDIN_FILENO, &saved_attributes);
atexit(reset_input_mode);

// Set the funny terminal modes.
struct termios tattr;
tcgetattr(STDIN_FILENO, &tattr);
tattr.c_lflag &= ~(ICANON | ECHO); // Clear ICANON and ECHO.
tattr.c_cc[VMIN] = 1;
tattr.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &tattr);

fd_set master, readfds;
FD_ZERO(&master);
FD_SET(STDIN_FILENO, &master);
FD_SET(sockfd, &master);

for (;;)
{
readfds = master;

if (select(sockfd + 1, &readfds, NULL, NULL, NULL) == -1)
{
perror("select");
return 7;
}

if (FD_ISSET(STDIN_FILENO, &readfds))
{
nbytes = read(STDIN_FILENO, buf, BUFFERSIZE);
if (nbytes < 1)
{
// perror("stdin closed");
break;
}
mbytes = send(sockfd, buf, nbytes, 0);
}

if (FD_ISSET(sockfd, &readfds))
{
nbytes = recv(sockfd, buf, BUFFERSIZE, 0);
if (nbytes < 1)
{
// perror("sockfd closed");
break;
}
mbytes = write(STDOUT_FILENO, buf, nbytes);
}
if (nbytes != mbytes)
printf("nbytes [%d] != mbytes [%d] \n", nbytes, mbytes);
}

close(sockfd);
printf("Bye 👋");
return 0;
}
41 changes: 41 additions & 0 deletions c/common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <sys/socket.h>

#include "common.h"

void trace(const char *prefix, const char *fmt, ...)
{
char line[1024];
char prefixed_line[1024];

va_list args;
va_start(args, fmt);
vsprintf(line, fmt, args);
va_end(args);

sprintf(prefixed_line, "%s: %s", prefix, line);
puts(prefixed_line);
}

bool sendall(int sockfd, const char *buf, size_t len)
{
size_t total_bytes = 0;
size_t bytes = 0;

while (len > 0) {
bytes = send(sockfd, buf + total_bytes, len, 0);
CHECK(bytes != -1);

total_bytes += bytes;
len -= bytes;
}

return true;

error:
TRACE("error");
return false;
}
17 changes: 17 additions & 0 deletions c/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef __COMMON_H_
#define __COMMON_H_

#include <stdarg.h>
#include <stdlib.h>
#include <stdbool.h>

#define TRACE(...) trace(__PRETTY_FUNCTION__, __VA_ARGS__)
#define CHECK(expression) if (!(expression)) { goto error; }

void trace(const char *prefix, const char *fmt, ...);

bool sendall(int sockfd, const char *buf, size_t len);



#endif // __COMMON_H_
Loading

0 comments on commit a42a5bc

Please sign in to comment.