mirror of
https://github.com/3proxy/3proxy.git
synced 2026-11-22 23:59:41 +08:00
socks: a host name with control bytes or spaces is refused where it is read
This commit is contained in:
1 parent
6b9f6cb1e1
commit
9acfe35103
5 files changed
+102
No files matched your search
@@ -543,11 +543,26 @@ int parsehost(int family, unsigned char *host, struct sockaddr *sa){
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* A host name is written into the request lines of parent protocols and
|
||||||
|
matched against rules, so a control byte or a space in it could end a
|
||||||
|
line early or pose as another destination. A name carrying one is
|
||||||
|
refused where it is read, and every name a client sends is read on
|
||||||
|
its way to the field. */
|
||||||
|
int badhostname(const char *hostname){
|
||||||
|
const unsigned char *c;
|
||||||
|
|
||||||
|
if(!hostname) return 1;
|
||||||
|
for(c = (const unsigned char *)hostname; *c; c++)
|
||||||
|
if(*c <= ' ' || *c == 0x7f) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int parsehostname(char *hostname, struct clientparam *param, uint16_t port){
|
int parsehostname(char *hostname, struct clientparam *param, uint16_t port){
|
||||||
char *sp=NULL,*se=NULL;
|
char *sp=NULL,*se=NULL;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
if(!hostname || !*hostname)return 2;
|
if(!hostname || !*hostname)return 2;
|
||||||
|
if(badhostname(hostname)) return 1;
|
||||||
if(*hostname == '[') se=strchr(hostname, ']');
|
if(*hostname == '[') se=strchr(hostname, ']');
|
||||||
if ((sp = strchr(se?se:hostname, ':'))) {
|
if ((sp = strchr(se?se:hostname, ':'))) {
|
||||||
if(strchr(sp+1, ':'))sp = NULL;
|
if(strchr(sp+1, ':'))sp = NULL;
|
||||||
|
|||||||
@@ -467,6 +467,7 @@ void hashdelete(struct hashtable *ht, void* name);
|
|||||||
int hashresolv(struct hashtable *ht, void* name, void* value, uint32_t *ttl);
|
int hashresolv(struct hashtable *ht, void* name, void* value, uint32_t *ttl);
|
||||||
|
|
||||||
int parsehost(int family, unsigned char *host, struct sockaddr *sa);
|
int parsehost(int family, unsigned char *host, struct sockaddr *sa);
|
||||||
|
int badhostname(const char *hostname);
|
||||||
int parsehostname(char *hostname, struct clientparam *param, uint16_t port);
|
int parsehostname(char *hostname, struct clientparam *param, uint16_t port);
|
||||||
int parseusername(char *username, struct clientparam *param, int extpasswd);
|
int parseusername(char *username, struct clientparam *param, int extpasswd);
|
||||||
int parseconnusername(char *username, struct clientparam *param, int extpasswd, uint16_t port);
|
int parseconnusername(char *username, struct clientparam *param, int extpasswd, uint16_t port);
|
||||||
|
|||||||
@@ -138,6 +138,7 @@ void * sockschild(struct clientparam* param) {
|
|||||||
default:
|
default:
|
||||||
RETURN(997);
|
RETURN(997);
|
||||||
}
|
}
|
||||||
|
if(badhostname((char *)buf)) {RETURN(21);}
|
||||||
if(PARAMSTR(param, hostname, buf)){RETURN(21);}
|
if(PARAMSTR(param, hostname, buf)){RETURN(21);}
|
||||||
if (ver == 5) {
|
if (ver == 5) {
|
||||||
if ((res = sockgetcharcli(param, param->srv->timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(441);}
|
if ((res = sockgetcharcli(param, param->srv->timeouts[SINGLEBYTE_S], 0)) == EOF) {RETURN(441);}
|
||||||
@@ -157,6 +158,7 @@ void * sockschild(struct clientparam* param) {
|
|||||||
param->service = S_SOCKS45;
|
param->service = S_SOCKS45;
|
||||||
if(sockgetlinebuf(param, CLIENT, buf, BUFSIZE - 1, 0, param->srv->timeouts[STRING_S]) < 0) {RETURN(441);}
|
if(sockgetlinebuf(param, CLIENT, buf, BUFSIZE - 1, 0, param->srv->timeouts[STRING_S]) < 0) {RETURN(441);}
|
||||||
buf[127] = 0;
|
buf[127] = 0;
|
||||||
|
if(badhostname((char *)buf)) {RETURN(21);}
|
||||||
if(PARAMSTR(param, hostname, buf)){RETURN(21);}
|
if(PARAMSTR(param, hostname, buf)){RETURN(21);}
|
||||||
if(!getip46m(param->srv->resolvmode, param->srv->family, buf, (struct sockaddr *) ¶m->req)) RETURN(100);
|
if(!getip46m(param->srv->resolvmode, param->srv->family, buf, (struct sockaddr *) ¶m->req)) RETURN(100);
|
||||||
param->sinsr = param->req;
|
param->sinsr = param->req;
|
||||||
|
|||||||
@@ -271,6 +271,7 @@ int udpsockmap(struct clientparam *param, int timeo)
|
|||||||
if (len < 7 + sz) return 485;
|
if (len < 7 + sz) return 485;
|
||||||
memcpy(dstnamebuf, base + 5, sz);
|
memcpy(dstnamebuf, base + 5, sz);
|
||||||
dstnamebuf[sz] = 0;
|
dstnamebuf[sz] = 0;
|
||||||
|
if (badhostname(dstnamebuf)) continue;
|
||||||
dstname = dstnamebuf;
|
dstname = dstnamebuf;
|
||||||
i = 5 + sz;
|
i = 5 + sz;
|
||||||
if (!getip46m(param->srv->resolvmode, param->srv->family, (unsigned char *)dstnamebuf,
|
if (!getip46m(param->srv->resolvmode, param->srv->family, (unsigned char *)dstnamebuf,
|
||||||
|
|||||||
@@ -1,5 +1,19 @@
|
|||||||
"""The SOCKS proxy, reaching the built-in server."""
|
"""The SOCKS proxy, reaching the built-in server."""
|
||||||
|
|
||||||
|
import socket
|
||||||
|
import struct
|
||||||
|
|
||||||
|
|
||||||
|
def recvall(sock, count):
|
||||||
|
"""Bytes from a socket, fewer only when the peer stops sending."""
|
||||||
|
data = b""
|
||||||
|
while len(data) < count:
|
||||||
|
chunk = sock.recv(count - len(data))
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
data += chunk
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
def run(t):
|
def run(t):
|
||||||
if not t.skip_unless("httpsrv"):
|
if not t.skip_unless("httpsrv"):
|
||||||
@@ -96,3 +110,72 @@ def run(t):
|
|||||||
"wrong SOCKS5 credentials are refused")
|
"wrong SOCKS5 credentials are refused")
|
||||||
t.ne(None, t.socks_connect(guarded, "127.0.0.1", srv),
|
t.ne(None, t.socks_connect(guarded, "127.0.0.1", srv),
|
||||||
"SOCKS5 without credentials is refused")
|
"SOCKS5 without credentials is refused")
|
||||||
|
|
||||||
|
# --- names a client may not choose -------------------------------------
|
||||||
|
# A destination name travels into the request lines of parent protocols,
|
||||||
|
# so a control byte or a space in it could end the line early and name
|
||||||
|
# another destination. Such a name is refused where it is read, before
|
||||||
|
# anything is resolved or connected: reply 1, not the reply of a
|
||||||
|
# destination which does not answer.
|
||||||
|
sname = t.free_port()
|
||||||
|
standin = t.free_port()
|
||||||
|
t.start("socks_names", f"""
|
||||||
|
log
|
||||||
|
fakeresolve
|
||||||
|
auth iponly
|
||||||
|
allow *
|
||||||
|
socks -p{sname}
|
||||||
|
""", ports=[sname])
|
||||||
|
|
||||||
|
def socks5_name(name, port):
|
||||||
|
"""The reply code of a SOCKS5 CONNECT naming the given name."""
|
||||||
|
with socket.create_connection(("127.0.0.1", sname), t.timeout) as s:
|
||||||
|
s.settimeout(t.timeout)
|
||||||
|
s.sendall(b"\x05\x01\x00")
|
||||||
|
if recvall(s, 2) != b"\x05\x00":
|
||||||
|
return None
|
||||||
|
s.sendall(b"\x05\x01\x00\x03" + bytes([len(name)]) + name +
|
||||||
|
struct.pack("!H", port))
|
||||||
|
reply = recvall(s, 4)
|
||||||
|
return reply[1] if len(reply) > 1 else None
|
||||||
|
|
||||||
|
def socks4_name(name, port):
|
||||||
|
"""The reply code of a SOCKS4a CONNECT naming the given name."""
|
||||||
|
with socket.create_connection(("127.0.0.1", sname), t.timeout) as s:
|
||||||
|
s.settimeout(t.timeout)
|
||||||
|
s.sendall(struct.pack(">BBH", 4, 1, port) +
|
||||||
|
socket.inet_aton("0.0.0.0") + b"case\x00" + name +
|
||||||
|
b"\x00")
|
||||||
|
reply = recvall(s, 8)
|
||||||
|
return reply[1] if len(reply) > 1 else None
|
||||||
|
|
||||||
|
t.eq(1, socks5_name(b"127.0.0.1\r\nGET /other HTTP/1.1\r\n\r\nstand-in.example",
|
||||||
|
standin),
|
||||||
|
"a name with CR LF in it is refused")
|
||||||
|
t.eq(91, socks4_name(b"127.0.0.1\r\nGET /other HTTP/1.1\r\n\r\nstand-in.example",
|
||||||
|
standin),
|
||||||
|
"SOCKS4a refuses a name with CR LF in it")
|
||||||
|
t.eq(1, socks5_name(b"127.0.0.1 stand-in.example", standin),
|
||||||
|
"a name with a space in it is refused")
|
||||||
|
t.eq(1, socks5_name(b"stand-in\x7f.example", standin),
|
||||||
|
"a name with a control byte in it is refused")
|
||||||
|
|
||||||
|
# a well-formed name is let through, to the 127.0.0.2 fakeresolve maps
|
||||||
|
# every name to; where that address can be served, the tunnel has to be
|
||||||
|
# carried all the way to it
|
||||||
|
probe = socket.socket()
|
||||||
|
try:
|
||||||
|
probe.bind(("127.0.0.2", 0))
|
||||||
|
except OSError:
|
||||||
|
probe.close()
|
||||||
|
t.skip("the stand-in address 127.0.0.2 can not be bound here")
|
||||||
|
else:
|
||||||
|
probe.close()
|
||||||
|
stop = t.raw_server(standin, b"from the stand-in\r\n",
|
||||||
|
host="127.0.0.2")
|
||||||
|
t.eq(0, socks5_name(b"stand-in.example", standin),
|
||||||
|
"a well-formed name is connected by name")
|
||||||
|
t.eq(90, socks4_name(b"stand-in.example", standin),
|
||||||
|
"a well-formed name is connected by name over SOCKS4a")
|
||||||
|
stop()
|
||||||
|
|
||||||
Reference in new issue
Block a user