Commit c82dd51d authored by Robert David Graham's avatar Robert David Graham
Browse files

json

parent 65bf2050
Loading
Loading
Loading
Loading

src/out-json.c

0 → 100644
+143 −0
Original line number Diff line number Diff line
#include "output.h"
#include "masscan-app.h"
#include "masscan-status.h"
#include "string_s.h"
#include <ctype.h>


/****************************************************************************
 ****************************************************************************/
static void
json_out_open(struct Output *out, FILE *fp)
{
}


/****************************************************************************
 ****************************************************************************/
static void
json_out_close(struct Output *out, FILE *fp)
{    
    fprintf(fp, "{finished: 1}\n");
}

//{ ip: "124.53.139.201", ports: [ {port: 443, proto: "tcp", status: "open", reason: "syn-ack", ttl: 48} ] }
/****************************************************************************
 ****************************************************************************/
static void
json_out_status(struct Output *out, FILE *fp, time_t timestamp, int status,
               unsigned ip, unsigned ip_proto, unsigned port, unsigned reason, unsigned ttl)
{
    char reason_buffer[128];
    UNUSEDPARM(out);
    
    fprintf(fp, "{ ");
    fprintf(fp, "  ip: \"%u.%u.%u.%u\", ", 
            (ip>>24)&0xFF, (ip>>16)&0xFF, (ip>> 8)&0xFF, (ip>> 0)&0xFF);
    fprintf(fp, "  ports: [ {port: %u, proto: \"%s\", status: \"%s\","
                " reason: \"%s\", ttl: %u} ] ",
                port,
                name_from_ip_proto(ip_proto),
                status_string(status),
                reason_string(reason, reason_buffer, sizeof(reason_buffer)),
                ttl
            );
    fprintf(fp, "},\n");
    

}

/*****************************************************************************
 * Remove bad characters from the banner, especially new lines and HTML
 * control codes.
 *****************************************************************************/
static const char *
normalize_json_string(const unsigned char *px, size_t length,
                 char *buf, size_t buf_len)
{
    size_t i=0;
    size_t offset = 0;
    
    
    for (i=0; i<length; i++) {
        unsigned char c = px[i];
        
        if (isprint(c) && c != '<' && c != '>' && c != '&' && c != '\\' && c != '\"' && c != '\'') {
            if (offset + 2 < buf_len)
                buf[offset++] = px[i];
        } else {
            if (offset + 7 < buf_len) {
                buf[offset++] = '\\';
                buf[offset++] = 'u';
                buf[offset++] = '0';
                buf[offset++] = '0';
                buf[offset++] = "0123456789abcdef"[px[i]>>4];
                buf[offset++] = "0123456789abcdef"[px[i]&0xF];
            }
        }
    }
    
    buf[offset] = '\0';
    
    return buf;
}

/******************************************************************************
 ******************************************************************************/
static void
json_out_banner(struct Output *out, FILE *fp, time_t timestamp,
               unsigned ip, unsigned ip_proto, unsigned port,
               enum ApplicationProtocol proto, 
               unsigned ttl,
               const unsigned char *px, unsigned length)
{
    char banner_buffer[65536];

    
    fprintf(fp, "{ ");
    fprintf(fp, "  ip: \"%u.%u.%u.%u\", ", 
            (ip>>24)&0xFF, (ip>>16)&0xFF, (ip>> 8)&0xFF, (ip>> 0)&0xFF);
    fprintf(fp, "  ports: [ {port: %u, proto: \"%s\", service: {name: \"%s\", banner: \"%s\"} } ] ",
            port,
            name_from_ip_proto(ip_proto),
            masscan_app_to_string(proto),
            normalize_json_string(px, length, banner_buffer, sizeof(banner_buffer))
            );
    fprintf(fp, "},\n");
    
    UNUSEDPARM(out);
    
/*    fprintf(fp, "<host endtime=\"%u\">"
            "<address addr=\"%u.%u.%u.%u\" addrtype=\"ipv4\"/>"
            "<ports>"
            "<port protocol=\"%s\" portid=\"%u\">"
            "<state state=\"open\" reason=\"%s\" reason_ttl=\"%u\" />"
            "<service name=\"%s\" banner=\"%s\"></service>"
            "</port>"
            "</ports>"
            "</host>"
            "\r\n",
            (unsigned)timestamp,
            (ip>>24)&0xFF,
            (ip>>16)&0xFF,
            (ip>> 8)&0xFF,
            (ip>> 0)&0xFF,
            name_from_ip_proto(ip_proto),
            port,
            reason, ttl,
            masscan_app_to_string(proto),
            normalize_string(px, length, banner_buffer, sizeof(banner_buffer))
            );*/
}

/****************************************************************************
 ****************************************************************************/
const struct OutputType json_output = {
    "json",
    0,
    json_out_open,
    json_out_close,
    json_out_status,
    json_out_banner
};
+3 −0
Original line number Diff line number Diff line
@@ -410,6 +410,9 @@ output_create(const struct Masscan *masscan, unsigned thread_index)
    case Output_XML:
        out->funcs = &xml_output;
        break;
    case Output_JSON:
        out->funcs = &json_output;
        break;
    case Output_Binary:
        out->funcs = &binary_output;
        break;
+1 −0
Original line number Diff line number Diff line
@@ -122,6 +122,7 @@ const char *normalize_string(const unsigned char *px, size_t length,

extern const struct OutputType text_output;
extern const struct OutputType xml_output;
extern const struct OutputType json_output;
extern const struct OutputType binary_output;
extern const struct OutputType null_output;
extern const struct OutputType redis_output;
+1 −1
Original line number Diff line number Diff line
@@ -304,7 +304,7 @@ tcpcon_create_table( size_t entry_count,
                        )
{
    struct TCP_ConnectionTable *tcpcon;

    printf("\nsizeof(TCB) = %u\n\n", (unsigned)sizeof(struct TCP_Control_Block));
    tcpcon = (struct TCP_ConnectionTable *)malloc(sizeof(*tcpcon));
    if (tcpcon == NULL)
        exit(1);
+5 −1
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
		115C0CAB18035BC5004E6CD7 /* proto-netbios.c in Sources */ = {isa = PBXBuildFile; fileRef = 115C0CA518035BC5004E6CD7 /* proto-netbios.c */; };
		115C0CAC18035BC5004E6CD7 /* proto-ssl.c in Sources */ = {isa = PBXBuildFile; fileRef = 115C0CA718035BC5004E6CD7 /* proto-ssl.c */; };
		11A50CAE191C128F006D5802 /* out-json.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A50CAD191C128F006D5802 /* out-json.c */; };
		11A773EB1881BFC700B135DE /* crypto-base64.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A773E91881BFC700B135DE /* crypto-base64.c */; };
		11A868151816F3A7008E00B8 /* in-binary.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A868081816F3A7008E00B8 /* in-binary.c */; };
		11A868161816F3A7008E00B8 /* main-src.c in Sources */ = {isa = PBXBuildFile; fileRef = 11A8680B1816F3A7008E00B8 /* main-src.c */; };
@@ -100,6 +101,7 @@
		115C0CA818035BC5004E6CD7 /* proto-ssl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "proto-ssl.h"; sourceTree = "<group>"; };
		115C0CA918035BC5004E6CD7 /* templ-port.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "templ-port.h"; sourceTree = "<group>"; };
		115C0CAA18035BC5004E6CD7 /* unusedparm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = unusedparm.h; sourceTree = "<group>"; };
		11A50CAD191C128F006D5802 /* out-json.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "out-json.c"; sourceTree = "<group>"; };
		11A773E91881BFC700B135DE /* crypto-base64.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "crypto-base64.c"; sourceTree = "<group>"; };
		11A773EA1881BFC700B135DE /* crypto-base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "crypto-base64.h"; sourceTree = "<group>"; };
		11A868081816F3A7008E00B8 /* in-binary.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = "in-binary.c"; sourceTree = "<group>"; };
@@ -256,11 +258,11 @@
		11A9219217DBCC7E00DDFD32 /* src */ = {
			isa = PBXGroup;
			children = (
				11BA295F18902CEE0064A759 /* out-grepable.c */,
				11B05EA918B964A9009C935E /* main-readrange.h */,
				11B05EA418B9649F009C935E /* crypto-blackrock2.c */,
				11B05EA518B9649F009C935E /* main-readrange.c */,
				11BA295E18902CEE0064A759 /* masscan-version.h */,
				11BA295F18902CEE0064A759 /* out-grepable.c */,
				11BA296018902CEE0064A759 /* proto-tcp-telnet.c */,
				11BA296118902CEE0064A759 /* proto-tcp-telnet.h */,
				11AC2F99188CE34A008CB623 /* proto-sctp.c */,
@@ -300,6 +302,7 @@
				11A8680F1816F3A7008E00B8 /* out-redis.c */,
				11A921A317DBCC7E00DDFD32 /* out-text.c */,
				11A921A417DBCC7E00DDFD32 /* out-xml.c */,
				11A50CAD191C128F006D5802 /* out-json.c */,
				11A921A517DBCC7E00DDFD32 /* output.c */,
				11A921A617DBCC7E00DDFD32 /* output.h */,
				11A921A717DBCC7E00DDFD32 /* packet-queue.h */,
@@ -510,6 +513,7 @@
				11BA296B189060220064A759 /* proto-ntp.c in Sources */,
				11B05EA618B9649F009C935E /* crypto-blackrock2.c in Sources */,
				11B05EA718B9649F009C935E /* main-readrange.c in Sources */,
				11A50CAE191C128F006D5802 /* out-json.c in Sources */,
			);
			runOnlyForDeploymentPostprocessing = 0;
		};