CVE-2017-20161: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection') (CWE-74)
value: A vulnerability classified as problematic has been found in rofl0r MacGeiger. Affected is the function dump_wlan_at of the file macgeiger.c of the component ESSID Handler. The manipulation leads to injection. Access to the local network is required for this attack to succeed. The complexity of an attack is rather high. The exploitability is told to be difficult. The name of the patch is 57f1dd50a4821b8c8e676e8020006ae4bfd3c9cb. It is recommended to apply a patch to fix this issue. The identifier of this vulnerability is VDB-217188.
CWE description: The product constructs all or part of a command, data structure, or record using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify how it is parsed or interpreted when it is sent to a downstream component.
CVSS v3 base score: 7.8
Published: 2023-01-02T16:15Z
Repository: https://github.com/rofl0r/MacGeiger
Language: C
Files changed: macgeiger.c
Commit date: 2017-12-02 11:50:10 +0000
Commit message: sanitize displayed essids

if the ncurses backend is used, having UTF-8 chars in an ESSID could lead to
"funny" unexpected effects, or an attacker might even set up an AP with an ESSID
containing terminal escapes to execute commands on the pentester's box...

Diff: diff --git a/macgeiger.c b/macgeiger.c
index df8a279..462b64c 100644
--- a/macgeiger.c
+++ b/macgeiger.c
@@ -561,10 +561,23 @@ static void dump_wlan_info(unsigned wlanidx) {
 	console_goto(t, ESSID_PRINT_END +1+25, line);
 	console_printf(t, "CURR %d dBm", w->last_rssi);
 	unlock();
 }
 
+static void sanitize_string(char *s, char *new) {
+	size_t i,j, l = strlen(s), ls=l;
+	for(i=0,j=0;i<ls;i++) {
+		if(s[i] < ' ' || s[i] > 127) {
+			sprintf(new + j, "\\x%02x", s[i] & 0xff);
+			j  += 3;
+		} else new[j] = s[i];
+		j++;
+	}
+	new[j] = 0;
+}
+
+
 static void dump_wlan_at(unsigned wlanidx, unsigned line) {
 	console_goto(t, 0, line);
 	console_setcolor(t, 0, BGCOL);
 
 	console_setcolor(t, 1, RGB(255,255,0));
@@ -586,13 +599,15 @@ static void dump_wlan_at(unsigned wlanidx, unsigned line) {
 	console_setcolor(t, 1, RGB(a,a,a));
 	console_goto(t, ESSID_PRINT_START, line);
 
 	char macbuf[18];
 
-	if(*w->essid)
-		console_printf(t, "%*s", ESSID_PRINT_LEN, w->essid);
-	else
+	if(*w->essid) {
+		char essid_san[32*4+1];
+		sanitize_string(w->essid, essid_san);
+		console_printf(t, "%*s", ESSID_PRINT_LEN, essid_san);
+	} else
 		console_printf(t, "<hidden> %*s", ESSID_PRINT_LEN-9, mac2str(w->mac, macbuf));
 
 	console_goto(t, ESSID_PRINT_END +1, line);
 
 	int scale = max - min;

Vulnerable code: 	if(*w->essid)
		console_printf(t, "%*s", ESSID_PRINT_LEN, w->essid);
	else
Fixed code: static void sanitize_string(char *s, char *new) {
	size_t i,j, l = strlen(s), ls=l;
	for(i=0,j=0;i<ls;i++) {
		if(s[i] < ' ' || s[i] > 127) {
			sprintf(new + j, "\\x%02x", s[i] & 0xff);
			j  += 3;
		} else new[j] = s[i];
		j++;
	}
	new[j] = 0;
}


	if(*w->essid) {
		char essid_san[32*4+1];
		sanitize_string(w->essid, essid_san);
		console_printf(t, "%*s", ESSID_PRINT_LEN, essid_san);
	} else