I am taking an online software security course. I am attempting to experiment with shellcode. I have written a vulnerable server, an injection program, a (probably broken) shellcode I convert to assembly, that I then strip with a python script. I then compile and run everything with a shell script. I am including all of my files, even though I am pretty sure I am not making the shellcode
binary properly.
vulnerable.c
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
int main(void) {
int sd, sdc, addrsize, ret, i;
struct sockaddr_in addr;
char exec[1024], buf[128];
void (*fn)(void);
const short family = AF_INET;
fn = (void (*)(void))exec;
addrsize = sizeof(struct sockaddr_in);
sd = socket(family, SOCK_STREAM, IPPROTO_TCP);
if (sd < 0) {
perror("socket");
return 1;
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(8000);
if (bind(sd, (struct sockaddr *) &addr,
sizeof(addr)) < 0) {
perror("bind");
return 2;
}
listen(sd, 1);
sdc = accept(sd, (struct sockaddr *)&addr, &addrsize);
i = 0;
do {
i += ret = read(sdc, buf, 128);
memcpy(exec + i, buf, ret);
} while (ret > 0);
close(sd);
fn();
return 0;
}
inject.c
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
int main(void) {
int sd, fd, ret, buflen = 128;
struct sockaddr_in addr;
FILE *f;
const char fname[] = "shellcode";
char buf[buflen];
const short family = AF_INET;
const char host[] = "127.0.0.1";
f = fopen(fname, "r");
fd = fileno(f);
sd = socket(family, SOCK_STREAM, IPPROTO_TCP);
if (sd < 0) {
perror("socket");
return 1;
}
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = family;
addr.sin_port = htons(8000);
inet_pton(family, host, &(addr.sin_addr.s_addr));
ret = connect(sd, (struct sockaddr*)&addr, sizeof(struct sockaddr));
if (ret < 0) {
perror("connect");
return 2;
}
do {
ret = read(fd, buf, buflen);
if (write(sd, buf, ret) != ret)
perror("write");
} while (ret > 0);
close(fd);
close(sd);
return 0;
}
shellcode.c
void shellcode(void) {
char sz[6] = { 'h', 'e', 'l', 'l', 'o', '\0' };
write(1, sz, 6);
}
stripshellcode.py
copy = False
outf = open("shellcode-stripped.s", "w")
for line in open("shellcode.s").read().split("\n"):
if copy:
outf.write(line + "\n")
if "shellcode:" in line:
copy = True
elif "ret" in line and copy:
copy = False
outf.close()
make.sh
gcc -S shellcode.c && \
python stripshellcode.py && \
gcc -c shellcode-stripped.s -o shellcode && \
gcc inject.c -o inject && \
gcc vulnerable.c -o vulnerable && \
./vulnerable & ./inject
output
$ sh make.sh
make.sh: line 6: 13905 Segmentation fault ./vulnerable
Where am I going wrong in this experimental process?
EDIT:
I should note by "closed system" I mean this is being executed in a virtual machine (check the bind address), and also it means some of the variables are hard-coded in a dependent way for brevity and ease.