security_SUITE.erl 12 KB

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