From 5cba44f5fa538103b9c7cd47a56aa9c852f14fb5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Yves=20R=C5=B1tschl=C3=A9?= <git1@rutschle.net>
Date: Sun, 11 Sep 2022 21:51:06 +0200
Subject: [PATCH] Add built-in MSRDP support

---
 probe.c      | 15 +++++++++++++++
 sslh-conf.c  | 24 +++++++++++++++++++++++-
 sslh-conf.h  |  2 +-
 sslhconf.cfg | 11 +++++++++++
 4 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/probe.c b/probe.c
index da35d3d..aaacb9c 100644
--- a/probe.c
+++ b/probe.c
@@ -42,6 +42,7 @@ static int is_adb_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_
 static int is_socks5_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
 static int is_syslog_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
 static int is_teamspeak_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
+static int is_msrdp_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item*);
 static int is_true(const char *p, ssize_t len, struct sslhcfg_protocols_item* proto) { return 1; }
 
 /* Table of protocols that have a built-in probe
@@ -59,6 +60,7 @@ static struct protocol_probe_desc builtins[] = {
     { "socks5",     is_socks5_protocol },
     { "syslog",     is_syslog_protocol },
     { "teamspeak",  is_teamspeak_protocol },
+    { "msrdp",      is_msrdp_protocol },
     { "anyprot",    is_true }
 };
 
@@ -381,6 +383,19 @@ static int is_teamspeak_protocol(const char *p, ssize_t len, struct sslhcfg_prot
     return !strncmp(p, "TS3INIT1", len);
 }
 
+static int is_msrdp_protocol(const char *p, ssize_t len, struct sslhcfg_protocols_item* proto)
+{
+    char version;
+    char packet_len;
+    if (len < 7)
+        return PROBE_NEXT;
+    version=*p;
+    if (version!=0x03)
+        return 0;
+    packet_len = ntohs(*(uint16_t*)(p+2));
+    return packet_len == len;
+}
+
 static int regex_probe(const char *p, ssize_t len, struct sslhcfg_protocols_item* proto)
 {
 #ifdef ENABLE_REGEX
diff --git a/sslh-conf.c b/sslh-conf.c
index e4e6dd7..b60cfc1 100644
--- a/sslh-conf.c
+++ b/sslh-conf.c
@@ -1,5 +1,5 @@
 /* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
- * on Sun Sep  4 18:47:04 2022. 
+ * on Sun Sep 11 21:43:25 2022. 
 
 # conf2struct: generate libconf parsers that read to structs
 # Copyright (C) 2018-2021  Yves Rutschle
@@ -479,6 +479,7 @@ struct arg_file* sslhcfg_conffile;
  	struct arg_str* sslhcfg_adb;
  	struct arg_str* sslhcfg_socks5;
  	struct arg_str* sslhcfg_syslog;
+ 	struct arg_str* sslhcfg_msrdp;
  	struct arg_str* sslhcfg_anyprot;
  struct arg_end* sslhcfg_end;
 
@@ -1257,6 +1258,14 @@ static struct compound_cl_target sslhcfg_anyprot_targets [] = {
 	{ 0 }
 };
 
+static struct compound_cl_target sslhcfg_msrdp_targets [] = {
+	{ & table_sslhcfg_protocols[0], 0, .value.def_string = "msrdp" },
+	{ & table_sslhcfg_protocols[1], 1, .value.def_string = "0" },
+	{ & table_sslhcfg_protocols[2], 2, .value.def_string = "0" },
+	{ & table_sslhcfg_protocols[10], 0, .value.def_int = 1 },
+	{ 0 }
+};
+
 static struct compound_cl_target sslhcfg_syslog_targets [] = {
 	{ & table_sslhcfg_protocols[0], 0, .value.def_string = "syslog" },
 	{ & table_sslhcfg_protocols[1], 1, .value.def_string = "0" },
@@ -1482,6 +1491,18 @@ static struct compound_cl_arg compound_cl_args[] = {
             .override_const = "syslog",
         },
 
+        {   /* arg: msrdp */
+            .regex =           "(.+):(\\w+)",
+            .arg_cl =          & sslhcfg_msrdp,
+            .base_entry =      & table_sslhcfg [26],
+            .targets =         sslhcfg_msrdp_targets,
+
+
+            .override_desc =   & table_sslhcfg_protocols [0],
+            .override_matchindex = 0,
+            .override_const = "msrdp",
+        },
+
         {   /* arg: anyprot */
             .regex =           "(.+):(\\w+)",
             .arg_cl =          & sslhcfg_anyprot,
@@ -2186,6 +2207,7 @@ int sslhcfg_cl_parse(int argc, char* argv[], struct sslhcfg_item* cfg)
  	sslhcfg_adb = arg_strn(NULL, "adb", "<host:port>", 0, 10, "Set up ADB (Android Debug) target"),
  	sslhcfg_socks5 = arg_strn(NULL, "socks5", "<host:port>", 0, 10, "Set up socks5 target"),
  	sslhcfg_syslog = arg_strn(NULL, "syslog", "<host:port>", 0, 10, "Set up syslog target"),
+ 	sslhcfg_msrdp = arg_strn(NULL, "msrdp", "<host:port>", 0, 10, "Set up msrdp target"),
  	sslhcfg_anyprot = arg_strn(NULL, "anyprot", "<host:port>", 0, 10, "Set up default target"),
  	sslhcfg_end = arg_end(10)
 
diff --git a/sslh-conf.h b/sslh-conf.h
index 62cab82..e52dfe0 100644
--- a/sslh-conf.h
+++ b/sslh-conf.h
@@ -1,5 +1,5 @@
 /* Generated by conf2struct (https://www.rutschle.net/tech/conf2struct/README)
- * on Sun Sep  4 18:47:04 2022. 
+ * on Sun Sep 11 21:43:25 2022. 
 
 # conf2struct: generate libconf parsers that read to structs
 # Copyright (C) 2018-2021  Yves Rutschle
diff --git a/sslhconf.cfg b/sslhconf.cfg
index 46d408b..ff1b7d3 100644
--- a/sslhconf.cfg
+++ b/sslhconf.cfg
@@ -277,6 +277,17 @@ cl_groups: (
         { path: "log_level"; value: 1 }
         );
     },
+    { name: "msrdp"; pattern: "(.+):(\w+)"; description: "Set up msrdp target";
+        list: "protocols";
+        override: "name";
+        argdesc: "<host:port>";
+        targets: (
+        { path: "name"; value: "msrdp" },
+        { path: "host"; value: "$1" },
+        { path: "port"; value: "$2" },
+        { path: "log_level"; value: 1 }
+        );
+    },
     { name: "anyprot"; pattern: "(.+):(\w+)"; description: "Set up default target";
         list: "protocols";
         override: "name";