This level is at /opt/protostar/bin/net1

Source code

#include "../common/common.c"

#define NAME "net1"
#define UID 998
#define GID 998
#define PORT 2998

void run()
{
char buf[12];
char fub[12];
char *q;

unsigned int wanted;

wanted = random();

sprintf(fub, "%d", wanted);

if(write(0, &wanted, sizeof(wanted)) != sizeof(wanted)) {
errx(1, ":(\n");
}

if(fgets(buf, sizeof(buf)-1, stdin) == NULL) {
errx(1, ":(\n");
}

q = strchr(buf, '\r'); if(q) *q = 0;
q = strchr(buf, '\n'); if(q) *q = 0;

if(strcmp(fub, buf) == 0) {
printf("you correctly sent the data\n");
} else {
printf("you didn't send the data properly\n");
}
}

int main(int argc, char **argv, char **envp)
{
int fd;
char *username;

/* Run the process as a daemon */
background_process(NAME, UID, GID);

/* Wait for socket activity and return */
fd = serve_forever(PORT);

/* Set the client socket to STDIN, STDOUT, and STDERR */
set_io(fd);

/* Don't do this :> */
srandom(time(NULL));

run();
}

这题与上一题其实差不多,上一题是直接将字符串格式的输出,而这道题是以%d出输出。如果在客户端接收到这个%d数字直接输出的话肯定会出现乱码的,因此需要在客户端转化后再输出即可。直接上代码:

#!/usr/bin/env python

from socket import *
from struct import *
from optparse import OptionParser

def main(hostname,port):
s = socket(AF_INET,SOCK_STREAM)
s.connect((hostname,port))

rec = s.recv(1024)
num = unpack(" print str(num[0])
buf = num[0]
s.send(str(buf))

print s.recv(1024)


if __name__=="__main__":
parse = OptionParser("usage: %prog [options]")
parse.add_option("-H",dest="hostname",default="127.0.0.1",type="string",help="The ip of the target")
parse.add_option("-P",dest="port",default=2998,type="int",help="The port of the host")

(options,args)=parse.parse_args()

main(options.hostname,options.port)
运行结果:
D:\Python27\a\protostar>debug.py -H 192.168.0.71
1436569335
you correctly sent the data