security_SUITE.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. %% Copyright (c) 2018, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(security_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [gun_open/1]).
  20. -import(cowboy_test, [raw_open/1]).
  21. -import(cowboy_test, [raw_send/2]).
  22. -import(cowboy_test, [raw_recv_head/1]).
  23. -import(cowboy_test, [raw_recv/3]).
  24. %% ct.
  25. all() ->
  26. cowboy_test:common_all().
  27. groups() ->
  28. Tests = [nc_rand, nc_zero],
  29. H1Tests = [slowloris, slowloris_chunks],
  30. H2CTests = [
  31. http2_data_dribble,
  32. http2_empty_frame_flooding_data,
  33. http2_empty_frame_flooding_headers_continuation,
  34. http2_empty_frame_flooding_push_promise,
  35. http2_ping_flood,
  36. http2_reset_flood,
  37. http2_settings_flood,
  38. http2_zero_length_header_leak
  39. ],
  40. [
  41. {http, [parallel], Tests ++ H1Tests},
  42. {https, [parallel], Tests ++ H1Tests},
  43. {h2, [parallel], Tests},
  44. {h2c, [parallel], Tests ++ H2CTests},
  45. {h3, [], Tests},
  46. {http_compress, [parallel], Tests ++ H1Tests},
  47. {https_compress, [parallel], Tests ++ H1Tests},
  48. {h2_compress, [parallel], Tests},
  49. {h2c_compress, [parallel], Tests ++ H2CTests},
  50. {h3_compress, [], Tests}
  51. ].
  52. init_per_suite(Config) ->
  53. ct_helper:create_static_dir(config(priv_dir, Config) ++ "/static"),
  54. Config.
  55. end_per_suite(Config) ->
  56. ct_helper:delete_static_dir(config(priv_dir, Config) ++ "/static").
  57. init_per_group(Name, Config) ->
  58. cowboy_test:init_common_groups(Name, Config, ?MODULE).
  59. end_per_group(Name, _) ->
  60. cowboy_test:stop_group(Name).
  61. %% Routes.
  62. init_dispatch(_) ->
  63. cowboy_router:compile([{"localhost", [
  64. {"/", hello_h, []},
  65. {"/echo/:key", echo_h, []},
  66. {"/long_polling", long_polling_h, []},
  67. {"/resp/:key[/:arg]", resp_h, []}
  68. ]}]).
  69. %% Tests.
  70. http2_data_dribble(Config) ->
  71. doc("Request a very large response then update the window 1 byte at a time. (CVE-2019-9511)"),
  72. {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
  73. %% Send a GET request for a very large response.
  74. {HeadersBlock, _} = cow_hpack:encode([
  75. {<<":method">>, <<"GET">>},
  76. {<<":scheme">>, <<"http">>},
  77. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  78. {<<":path">>, <<"/resp/stream_body/loop">>}
  79. ]),
  80. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  81. %% Receive a response with a few DATA frames draining the window.
  82. {ok, <<SkipLen:24, 1:8, _:8, 1:32>>} = gen_tcp:recv(Socket, 9, 1000),
  83. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  84. {ok, <<16384:24, 0:8, 0:8, 1:32, _:16384/unit:8>>} = gen_tcp:recv(Socket, 9 + 16384, 1000),
  85. {ok, <<16384:24, 0:8, 0:8, 1:32, _:16384/unit:8>>} = gen_tcp:recv(Socket, 9 + 16384, 1000),
  86. {ok, <<16384:24, 0:8, 0:8, 1:32, _:16384/unit:8>>} = gen_tcp:recv(Socket, 9 + 16384, 1000),
  87. {ok, <<16383:24, 0:8, 0:8, 1:32, _:16383/unit:8>>} = gen_tcp:recv(Socket, 9 + 16383, 1000),
  88. %% Send WINDOW_UPDATE frames with a value of 1. The server should
  89. %% not attempt to send data until the window is over a configurable threshold.
  90. ok = gen_tcp:send(Socket, [
  91. cow_http2:window_update(1),
  92. cow_http2:window_update(1, 1)
  93. ]),
  94. {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
  95. ok.
  96. http2_empty_frame_flooding_data(Config) ->
  97. doc("Confirm that Cowboy detects empty DATA frame flooding. (CVE-2019-9518)"),
  98. {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
  99. %% Send a POST request followed by many empty DATA frames.
  100. {HeadersBlock, _} = cow_hpack:encode([
  101. {<<":method">>, <<"POST">>},
  102. {<<":scheme">>, <<"http">>},
  103. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  104. {<<":path">>, <<"/echo/read_body">>}
  105. ]),
  106. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  107. _ = [gen_tcp:send(Socket, cow_http2:data(1, nofin, <<>>)) || _ <- lists:seq(1, 20000)],
  108. %% When Cowboy detects a flood it must close the connection.
  109. %% We skip WINDOW_UPDATE frames sent when Cowboy starts to read the body.
  110. case gen_tcp:recv(Socket, 43, 6000) of
  111. {ok, <<_:26/unit:8, _:24, 7:8, _:72, 11:32>>} ->
  112. ok;
  113. %% We also accept the connection being closed immediately,
  114. %% which may happen because we send the GOAWAY right before closing.
  115. {error, closed} ->
  116. ok;
  117. %% At least on Windows this might also occur.
  118. {error, enotconn} ->
  119. ok
  120. end.
  121. http2_empty_frame_flooding_headers_continuation(Config) ->
  122. doc("Confirm that Cowboy detects empty HEADERS/CONTINUATION frame flooding. (CVE-2019-9518)"),
  123. {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
  124. %% Send many empty HEADERS/CONTINUATION frames before the headers.
  125. ok = gen_tcp:send(Socket, <<0:24, 1:8, 0:9, 1:31>>),
  126. _ = [gen_tcp:send(Socket, <<0:24, 9:8, 0:9, 1:31>>) || _ <- lists:seq(1, 20000)],
  127. {HeadersBlock, _} = cow_hpack:encode([
  128. {<<":method">>, <<"POST">>},
  129. {<<":scheme">>, <<"http">>},
  130. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  131. {<<":path">>, <<"/">>}
  132. ]),
  133. Len = iolist_size(HeadersBlock),
  134. _ = gen_tcp:send(Socket, [<<Len:24, 9:8, 0:5, 1:1, 0:1, 1:1, 0:1, 1:31>>, HeadersBlock]),
  135. %% When Cowboy detects a flood it must close the connection.
  136. case gen_tcp:recv(Socket, 17, 6000) of
  137. {ok, <<_:24, 7:8, _:72, 11:32>>} ->
  138. ok;
  139. %% We also accept the connection being closed immediately,
  140. %% which may happen because we send the GOAWAY right before closing.
  141. {error, closed} ->
  142. ok;
  143. %% At least on Windows this might also occur.
  144. {error, enotconn} ->
  145. ok
  146. end.
  147. http2_empty_frame_flooding_push_promise(Config) ->
  148. doc("Confirm that Cowboy detects empty PUSH_PROMISE frame flooding. (CVE-2019-9518)"),
  149. {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
  150. %% Send a HEADERS frame to which we will attach a PUSH_PROMISE.
  151. %% We use nofin in order to keep the stream alive.
  152. {HeadersBlock, _} = cow_hpack:encode([
  153. {<<":method">>, <<"GET">>},
  154. {<<":scheme">>, <<"http">>},
  155. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  156. {<<":path">>, <<"/long_polling">>}
  157. ]),
  158. ok = gen_tcp:send(Socket, cow_http2:headers(1, nofin, HeadersBlock)),
  159. %% Send nofin PUSH_PROMISE frame without any data.
  160. ok = gen_tcp:send(Socket, <<4:24, 5:8, 0:8, 0:1, 1:31, 0:1, 3:31>>),
  161. %% Receive a PROTOCOL_ERROR connection error.
  162. %%
  163. %% Cowboy rejects all PUSH_PROMISE frames therefore no flooding
  164. %% can take place.
  165. {ok, <<_:24, 7:8, _:72, 1:32>>} = gen_tcp:recv(Socket, 17, 6000),
  166. ok.
  167. %% @todo http2_internal_data_buffering(Config) -> I do not know how to test this.
  168. % doc("Request many very large responses, with a larger than necessary window size, "
  169. % "but do not attempt to read from the socket. (CVE-2019-9517)"),
  170. http2_ping_flood(Config) ->
  171. doc("Confirm that Cowboy detects PING floods. (CVE-2019-9512)"),
  172. {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
  173. %% Flood the server with PING frames.
  174. _ = [gen_tcp:send(Socket, cow_http2:ping(0)) || _ <- lists:seq(1, 20000)],
  175. %% Receive a number of PING ACK frames in return, following by the closing of the connection.
  176. try
  177. [case gen_tcp:recv(Socket, 17, 6000) of
  178. {ok, <<8:24, 6:8, _:7, 1:1, _:32, 0:64>>} -> ok;
  179. {ok, <<_:24, 7:8, _:72, 11:32>>} -> throw(goaway);
  180. %% We also accept the connection being closed immediately,
  181. %% which may happen because we send the GOAWAY right before closing.
  182. {error, closed} -> throw(goaway)
  183. end || _ <- lists:seq(1, 20000)],
  184. error(flood_successful)
  185. catch throw:goaway ->
  186. ok
  187. end.
  188. http2_reset_flood(Config) ->
  189. doc("Confirm that Cowboy detects reset floods. (CVE-2019-9514)"),
  190. {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
  191. %% Flood the server with HEADERS frames without a :method pseudo-header.
  192. {HeadersBlock, _} = cow_hpack:encode([
  193. {<<":scheme">>, <<"http">>},
  194. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  195. {<<":path">>, <<"/">>}
  196. ]),
  197. _ = [gen_tcp:send(Socket, cow_http2:headers(ID, fin, HeadersBlock)) || ID <- lists:seq(1, 100, 2)],
  198. %% Receive a number of RST_STREAM frames in return, following by the closing of the connection.
  199. try
  200. [case gen_tcp:recv(Socket, 13, 6000) of
  201. {ok, <<_:24, 3:8, _:8, ID:32, 1:32>>} -> ok;
  202. {ok, <<_:24, 7:8, _:72>>} ->
  203. {ok, <<11:32>>} = gen_tcp:recv(Socket, 4, 1000),
  204. throw(goaway);
  205. %% We also accept the connection being closed immediately,
  206. %% which may happen because we send the GOAWAY right before closing.
  207. {error, closed} ->
  208. throw(goaway)
  209. end || ID <- lists:seq(1, 100, 2)],
  210. error(flood_successful)
  211. catch throw:goaway ->
  212. ok
  213. end.
  214. %% @todo If we ever implement the PRIORITY mechanism, this test should
  215. %% be implemented as well. CVE-2019-9513 https://www.kb.cert.org/vuls/id/605641/
  216. %% http2_resource_loop
  217. http2_settings_flood(Config) ->
  218. doc("Confirm that Cowboy detects SETTINGS floods. (CVE-2019-9515)"),
  219. {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
  220. %% Flood the server with empty SETTINGS frames.
  221. _ = [gen_tcp:send(Socket, cow_http2:settings(#{})) || _ <- lists:seq(1, 20000)],
  222. %% Receive a number of SETTINGS ACK frames in return, following by the closing of the connection.
  223. try
  224. [case gen_tcp:recv(Socket, 9, 6000) of
  225. {ok, <<0:24, 4:8, 0:7, 1:1, 0:32>>} -> ok;
  226. {ok, <<_:24, 7:8, _:40>>} ->
  227. {ok, <<_:32, 11:32>>} = gen_tcp:recv(Socket, 8, 1000),
  228. throw(goaway);
  229. %% We also accept the connection being closed immediately,
  230. %% which may happen because we send the GOAWAY right before closing.
  231. {error, closed} ->
  232. throw(goaway)
  233. end || _ <- lists:seq(1, 20000)],
  234. error(flood_successful)
  235. catch throw:goaway ->
  236. ok
  237. end.
  238. http2_zero_length_header_leak(Config) ->
  239. doc("Confirm that Cowboy rejects HEADERS frame with a 0-length header name. (CVE-2019-9516)"),
  240. {ok, Socket} = rfc7540_SUITE:do_handshake(Config),
  241. %% Send a GET request with a 0-length header name.
  242. {HeadersBlock, _} = cow_hpack:encode([
  243. {<<":method">>, <<"GET">>},
  244. {<<":scheme">>, <<"http">>},
  245. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  246. {<<":path">>, <<"/">>},
  247. {<<>>, <<"CVE-2019-9516">>}
  248. ]),
  249. ok = gen_tcp:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  250. %% Receive a PROTOCOL_ERROR stream error.
  251. {ok, <<_:24, 3:8, _:8, 1:32, 1:32>>} = gen_tcp:recv(Socket, 13, 6000),
  252. ok.
  253. nc_rand(Config) ->
  254. doc("Throw random garbage at the server, then check if it's still up."),
  255. do_nc(Config, "/dev/urandom").
  256. nc_zero(Config) ->
  257. doc("Throw zeroes at the server, then check if it's still up."),
  258. do_nc(Config, "/dev/zero").
  259. do_nc(Config, Input) ->
  260. Cat = os:find_executable("cat"),
  261. Nc = os:find_executable("nc"),
  262. case {Cat, Nc} of
  263. {false, _} ->
  264. {skip, "The cat executable was not found."};
  265. {_, false} ->
  266. {skip, "The nc executable was not found."};
  267. _ ->
  268. StrPort = integer_to_list(config(port, Config)),
  269. _ = [
  270. os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort)
  271. || _ <- lists:seq(1, 100)],
  272. ConnPid = gun_open(Config),
  273. Ref = gun:get(ConnPid, "/"),
  274. {response, _, 200, _} = gun:await(ConnPid, Ref),
  275. ok
  276. end.
  277. slowloris(Config) ->
  278. doc("Send request headers one byte at a time. "
  279. "Confirm that the connection gets closed."),
  280. Client = raw_open(Config),
  281. try
  282. [begin
  283. ok = raw_send(Client, [C]),
  284. timer:sleep(250)
  285. end || C <- "GET / HTTP/1.1\r\nHost: localhost\r\n"
  286. "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)\r\n"
  287. "Cookie: name=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n\r\n"],
  288. error(failure)
  289. catch error:{badmatch, _} ->
  290. ok
  291. end.
  292. slowloris_chunks(Config) ->
  293. doc("Send request headers one line at a time. "
  294. "Confirm that the connection gets closed."),
  295. Client = raw_open(Config),
  296. ok = raw_send(Client, "GET / HTTP/1.1\r\n"),
  297. timer:sleep(300),
  298. ok = raw_send(Client, "Host: localhost\r\n"),
  299. timer:sleep(300),
  300. Data = raw_recv_head(Client),
  301. {'HTTP/1.1', 408, _, Rest} = cow_http:parse_status_line(Data),
  302. {Headers, _} = cow_http:parse_headers(Rest),
  303. {_, <<"close">>} = lists:keyfind(<<"connection">>, 1, Headers),
  304. {error, closed} = raw_recv(Client, 0, 1000).