Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make logging real and timestamped

  • Loading branch information...
commit e80f4c30ff9392239dbd39b617e5d2f64d247855 1 parent 5ad3e9e
@txus authored
View
11 Readme.md
@@ -42,7 +42,7 @@ Just fire up the server with some fake service, such an ftp server:
If everything is ok, you will see something like this:
- Fake ftp service listening on port 8765...
+ [Sun Oct 2 16:16:32 2011] Fake ftp server ready and listening on port 8765...
Most port scanners such as *nmap* have some kind of fingerprinting
capabilities. This means that, in order to discover which services and
@@ -61,7 +61,11 @@ ftp service with nmap fingerprinting:
We are scanning localhost, port 8765, and -A means service version detection
and OS guessing. After a while, in our micetrap server terminal we see:
- Incoming connection from 127.0.0.1
+ [Sun Oct 2 16:16:40 2011] Incoming connection from 127.0.0.1
+ [Sun Oct 2 16:16:46 2011] Message received:
+
+ [Sun Oct 2 16:16:46 2011] Sent a fake probe: 220-----------------------------
+ 220-This is the "Banner" message for the Mac OS X Server's FTP server process.
And in the nmap terminal:
@@ -105,6 +109,3 @@ and be a superhero, you shall follow these steps:
## Copyright
Copyright (c) 2011 Josep M. Bach. See LICENSE for details.
-
-
-
View
6 src/log.h
@@ -0,0 +1,6 @@
+#ifndef __log_h__
+#define __log_h__
+
+#define log(M, ...) fprintf(stdout, "[%s] " M "\n", timestamp(), ##__VA_ARGS__)
+
+#endif
View
7 src/server.c
@@ -11,6 +11,7 @@
#include <arpa/inet.h>
#include "utils.h"
+#include "log.h" // Always require after utils.h
#include "service.h"
#include "server.h"
@@ -60,7 +61,7 @@ void Server_start(Service *service, int port)
die("Could not bind the socket.");
}
- printf("Fake %s server ready and listening on port %d...\n", service->name, port);
+ log("Fake %s server ready and listening on port %d...", service->name, port);
listen(listen_fd, 5);
while(1) {
@@ -73,13 +74,15 @@ void Server_start(Service *service, int port)
die("Could not accept incoming connection.");
}
- printf("Incoming connection from %s\n", inet_ntoa(client.sin_addr));
+ log("Incoming connection from %s", inet_ntoa(client.sin_addr));
// Receive the probe
char buf[256];
received = recv(accept_fd, buf, 255, 0);
+ log("Message received: %s", buf);
if (received == 1) {
send(accept_fd, response, response_length, 0);
+ log("Sent a fake probe: %s", response);
}
// Close the connection
View
33 src/utils.c
@@ -1,6 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
+#include <time.h>
+#include <string.h>
#include "utils.h"
void die(const char *message)
@@ -13,3 +15,34 @@ void die(const char *message)
exit(1);
}
+
+char *timestamp()
+{
+ time_t ltime;
+ char* retval;
+
+ ltime = time(NULL);
+ retval = asctime(localtime(&ltime));
+
+ retval = sub(retval, '\n', '\0');
+
+ return retval;
+}
+
+char *sub(char *string, char from, char to)
+{
+ int length = strlen(string);
+ int i = 0;
+
+ char *buffer = malloc(sizeof(char) * length);
+
+ for(i = 0; i < length-1; i++) {
+ if (string[i] == from) {
+ buffer[i] = to;
+ } else {
+ buffer[i] = string[i];
+ }
+ }
+
+ return buffer;
+}
View
2  src/utils.h
@@ -2,5 +2,7 @@
#define __utils_h__
void die(const char *message);
+char *timestamp();
+char *sub(char *string, char from, char to);
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.