security_events.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2011, Digium, Inc.
  5. *
  6. * Michael L. Young <elgueromexicano@gmail.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. *
  21. * \brief Generate security events in the SIP channel
  22. *
  23. * \author Michael L. Young <elgueromexicano@gmail.com>
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "include/sip.h"
  31. #include "include/security_events.h"
  32. /*! \brief Determine transport type used to receive request*/
  33. static enum ast_security_event_transport_type security_event_get_transport(const struct sip_pvt *p)
  34. {
  35. int res = 0;
  36. switch (p->socket.type) {
  37. case SIP_TRANSPORT_UDP:
  38. return AST_SECURITY_EVENT_TRANSPORT_UDP;
  39. case SIP_TRANSPORT_TCP:
  40. return AST_SECURITY_EVENT_TRANSPORT_TCP;
  41. case SIP_TRANSPORT_TLS:
  42. return AST_SECURITY_EVENT_TRANSPORT_TLS;
  43. }
  44. return res;
  45. }
  46. static struct sockaddr_in *security_event_encode_sin_local(const struct sip_pvt *p, struct sockaddr_in *sin_local)
  47. {
  48. ast_sockaddr_to_sin(&p->ourip, sin_local);
  49. return sin_local;
  50. }
  51. static struct sockaddr_in *security_event_encode_sin_remote(const struct sip_pvt *p, struct sockaddr_in *sin_remote)
  52. {
  53. ast_sockaddr_to_sin(&p->sa, sin_remote);
  54. return sin_remote;
  55. }
  56. void sip_report_invalid_peer(const struct sip_pvt *p)
  57. {
  58. char session_id[32];
  59. struct sockaddr_in sin_local;
  60. struct sockaddr_in sin_remote;
  61. struct ast_security_event_inval_acct_id inval_acct_id = {
  62. .common.event_type = AST_SECURITY_EVENT_INVAL_ACCT_ID,
  63. .common.version = AST_SECURITY_EVENT_INVAL_ACCT_ID_VERSION,
  64. .common.service = "SIP",
  65. .common.account_id = p->exten,
  66. .common.local_addr = {
  67. .sin = security_event_encode_sin_local(p, &sin_local),
  68. .transport = security_event_get_transport(p)
  69. },
  70. .common.remote_addr = {
  71. .sin = security_event_encode_sin_remote(p, &sin_remote),
  72. .transport = security_event_get_transport(p)
  73. },
  74. .common.session_id = session_id,
  75. };
  76. snprintf(session_id, sizeof(session_id), "%p", p);
  77. ast_security_event_report(AST_SEC_EVT(&inval_acct_id));
  78. }
  79. void sip_report_failed_acl(const struct sip_pvt *p, const char *aclname)
  80. {
  81. char session_id[32];
  82. struct sockaddr_in sin_local;
  83. struct sockaddr_in sin_remote;
  84. struct ast_security_event_failed_acl failed_acl_event = {
  85. .common.event_type = AST_SECURITY_EVENT_FAILED_ACL,
  86. .common.version = AST_SECURITY_EVENT_FAILED_ACL_VERSION,
  87. .common.service = "SIP",
  88. .common.account_id = p->exten,
  89. .common.local_addr = {
  90. .sin = security_event_encode_sin_local(p, &sin_local),
  91. .transport = security_event_get_transport(p)
  92. },
  93. .common.remote_addr = {
  94. .sin = security_event_encode_sin_remote(p, &sin_remote),
  95. .transport = security_event_get_transport(p)
  96. },
  97. .common.session_id = session_id,
  98. .acl_name = aclname,
  99. };
  100. snprintf(session_id, sizeof(session_id), "%p", p);
  101. ast_security_event_report(AST_SEC_EVT(&failed_acl_event));
  102. }
  103. void sip_report_inval_password(const struct sip_pvt *p, const char *response_challenge, const char *response_hash)
  104. {
  105. char session_id[32];
  106. struct sockaddr_in sin_local;
  107. struct sockaddr_in sin_remote;
  108. struct ast_security_event_inval_password inval_password = {
  109. .common.event_type = AST_SECURITY_EVENT_INVAL_PASSWORD,
  110. .common.version = AST_SECURITY_EVENT_INVAL_PASSWORD_VERSION,
  111. .common.service = "SIP",
  112. .common.account_id = p->exten,
  113. .common.local_addr = {
  114. .sin = security_event_encode_sin_local(p, &sin_local),
  115. .transport = security_event_get_transport(p)
  116. },
  117. .common.remote_addr = {
  118. .sin = security_event_encode_sin_remote(p, &sin_remote),
  119. .transport = security_event_get_transport(p)
  120. },
  121. .common.session_id = session_id,
  122. .challenge = p->randdata,
  123. .received_challenge = response_challenge,
  124. .received_hash = response_hash,
  125. };
  126. snprintf(session_id, sizeof(session_id), "%p", p);
  127. ast_security_event_report(AST_SEC_EVT(&inval_password));
  128. }
  129. void sip_report_auth_success(const struct sip_pvt *p, uint32_t *using_password)
  130. {
  131. char session_id[32];
  132. struct sockaddr_in sin_local;
  133. struct sockaddr_in sin_remote;
  134. struct ast_security_event_successful_auth successful_auth = {
  135. .common.event_type = AST_SECURITY_EVENT_SUCCESSFUL_AUTH,
  136. .common.version = AST_SECURITY_EVENT_SUCCESSFUL_AUTH_VERSION,
  137. .common.service = "SIP",
  138. .common.account_id = p->exten,
  139. .common.local_addr = {
  140. .sin = security_event_encode_sin_local(p, &sin_local),
  141. .transport = security_event_get_transport(p)
  142. },
  143. .common.remote_addr = {
  144. .sin = security_event_encode_sin_remote(p, &sin_remote),
  145. .transport = security_event_get_transport(p)
  146. },
  147. .common.session_id = session_id,
  148. .using_password = using_password,
  149. };
  150. snprintf(session_id, sizeof(session_id), "%p", p);
  151. ast_security_event_report(AST_SEC_EVT(&successful_auth));
  152. }
  153. void sip_report_session_limit(const struct sip_pvt *p)
  154. {
  155. char session_id[32];
  156. struct sockaddr_in sin_local;
  157. struct sockaddr_in sin_remote;
  158. struct ast_security_event_session_limit session_limit = {
  159. .common.event_type = AST_SECURITY_EVENT_SESSION_LIMIT,
  160. .common.version = AST_SECURITY_EVENT_SESSION_LIMIT_VERSION,
  161. .common.service = "SIP",
  162. .common.account_id = p->exten,
  163. .common.local_addr = {
  164. .sin = security_event_encode_sin_local(p, &sin_local),
  165. .transport = security_event_get_transport(p)
  166. },
  167. .common.remote_addr = {
  168. .sin = security_event_encode_sin_remote(p, &sin_remote),
  169. .transport = security_event_get_transport(p)
  170. },
  171. .common.session_id = session_id,
  172. };
  173. snprintf(session_id, sizeof(session_id), "%p", p);
  174. ast_security_event_report(AST_SEC_EVT(&session_limit));
  175. }
  176. void sip_report_failed_challenge_response(const struct sip_pvt *p, const char *response, const char *expected_response)
  177. {
  178. char session_id[32];
  179. struct sockaddr_in sin_local;
  180. struct sockaddr_in sin_remote;
  181. char account_id[256];
  182. struct ast_security_event_chal_resp_failed chal_resp_failed = {
  183. .common.event_type = AST_SECURITY_EVENT_CHAL_RESP_FAILED,
  184. .common.version = AST_SECURITY_EVENT_CHAL_RESP_FAILED_VERSION,
  185. .common.service = "SIP",
  186. .common.account_id = account_id,
  187. .common.local_addr = {
  188. .sin = security_event_encode_sin_local(p, &sin_local),
  189. .transport = security_event_get_transport(p)
  190. },
  191. .common.remote_addr = {
  192. .sin = security_event_encode_sin_remote(p, &sin_remote),
  193. .transport = security_event_get_transport(p)
  194. },
  195. .common.session_id = session_id,
  196. .challenge = p->randdata,
  197. .response = response,
  198. .expected_response = expected_response,
  199. };
  200. if (!ast_strlen_zero(p->from)) { /* When dialing, show account making call */
  201. ast_copy_string(account_id, p->from, sizeof(account_id));
  202. } else {
  203. ast_copy_string(account_id, p->exten, sizeof(account_id));
  204. }
  205. snprintf(session_id, sizeof(session_id), "%p", p);
  206. ast_security_event_report(AST_SEC_EVT(&chal_resp_failed));
  207. }
  208. void sip_report_chal_sent(const struct sip_pvt *p)
  209. {
  210. char session_id[32];
  211. struct sockaddr_in sin_local;
  212. struct sockaddr_in sin_remote;
  213. char account_id[256];
  214. struct ast_security_event_chal_sent chal_sent = {
  215. .common.event_type = AST_SECURITY_EVENT_CHAL_SENT,
  216. .common.version = AST_SECURITY_EVENT_CHAL_SENT_VERSION,
  217. .common.service = "SIP",
  218. .common.account_id = account_id,
  219. .common.local_addr = {
  220. .sin = security_event_encode_sin_local(p, &sin_local),
  221. .transport = security_event_get_transport(p)
  222. },
  223. .common.remote_addr = {
  224. .sin = security_event_encode_sin_remote(p, &sin_remote),
  225. .transport = security_event_get_transport(p)
  226. },
  227. .common.session_id = session_id,
  228. .challenge = p->randdata,
  229. };
  230. if (!ast_strlen_zero(p->from)) { /* When dialing, show account making call */
  231. ast_copy_string(account_id, p->from, sizeof(account_id));
  232. } else {
  233. ast_copy_string(account_id, p->exten, sizeof(account_id));
  234. }
  235. snprintf(session_id, sizeof(session_id), "%p", p);
  236. ast_security_event_report(AST_SEC_EVT(&chal_sent));
  237. }
  238. void sip_report_inval_transport(const struct sip_pvt *p, const char *transport)
  239. {
  240. char session_id[32];
  241. struct sockaddr_in sin_local;
  242. struct sockaddr_in sin_remote;
  243. struct ast_security_event_inval_transport inval_transport = {
  244. .common.event_type = AST_SECURITY_EVENT_INVAL_TRANSPORT,
  245. .common.version = AST_SECURITY_EVENT_INVAL_TRANSPORT_VERSION,
  246. .common.service = "SIP",
  247. .common.account_id = p->exten,
  248. .common.local_addr = {
  249. .sin = security_event_encode_sin_local(p, &sin_local),
  250. .transport = security_event_get_transport(p)
  251. },
  252. .common.remote_addr = {
  253. .sin = security_event_encode_sin_remote(p, &sin_remote),
  254. .transport = security_event_get_transport(p)
  255. },
  256. .common.session_id = session_id,
  257. .transport = transport,
  258. };
  259. snprintf(session_id, sizeof(session_id), "%p", p);
  260. ast_security_event_report(AST_SEC_EVT(&inval_transport));
  261. }
  262. int sip_report_security_event(const struct sip_pvt *p, const struct sip_request *req, const int res) {
  263. struct sip_peer *peer_report;
  264. enum check_auth_result res_report = res;
  265. struct ast_str *buf;
  266. char *c;
  267. const char *authtoken;
  268. char *reqheader, *respheader;
  269. int result = 0;
  270. char aclname[256];
  271. struct digestkeys keys[] = {
  272. [K_RESP] = { "response=", "" },
  273. [K_URI] = { "uri=", "" },
  274. [K_USER] = { "username=", "" },
  275. [K_NONCE] = { "nonce=", "" },
  276. [K_LAST] = { NULL, NULL}
  277. };
  278. peer_report = sip_find_peer(p->exten, NULL, TRUE, FINDPEERS, FALSE, 0);
  279. switch(res_report) {
  280. case AUTH_DONT_KNOW:
  281. break;
  282. case AUTH_SUCCESSFUL:
  283. if (peer_report) {
  284. if (ast_strlen_zero(peer_report->secret) && ast_strlen_zero(peer_report->md5secret)) {
  285. sip_report_auth_success(p, (uint32_t *) 0);
  286. } else {
  287. sip_report_auth_success(p, (uint32_t *) 1);
  288. }
  289. }
  290. break;
  291. case AUTH_CHALLENGE_SENT:
  292. sip_report_chal_sent(p);
  293. break;
  294. case AUTH_SECRET_FAILED:
  295. case AUTH_USERNAME_MISMATCH:
  296. sip_auth_headers(WWW_AUTH, &respheader, &reqheader);
  297. authtoken = sip_get_header(req, reqheader);
  298. buf = ast_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN);
  299. ast_str_set(&buf, 0, "%s", authtoken);
  300. c = buf->str;
  301. sip_digest_parser(c, keys);
  302. if (res_report == AUTH_SECRET_FAILED) {
  303. sip_report_inval_password(p, keys[K_NONCE].s, keys[K_RESP].s);
  304. } else {
  305. if (peer_report) {
  306. sip_report_failed_challenge_response(p, keys[K_USER].s, peer_report->username);
  307. }
  308. }
  309. break;
  310. case AUTH_NOT_FOUND:
  311. /* with sip_cfg.alwaysauthreject on, generates 2 events */
  312. sip_report_invalid_peer(p);
  313. break;
  314. case AUTH_UNKNOWN_DOMAIN:
  315. snprintf(aclname, sizeof(aclname), "domain_must_match");
  316. sip_report_failed_acl(p, aclname);
  317. break;
  318. case AUTH_PEER_NOT_DYNAMIC:
  319. snprintf(aclname, sizeof(aclname), "peer_not_dynamic");
  320. sip_report_failed_acl(p, aclname);
  321. break;
  322. case AUTH_ACL_FAILED:
  323. /* with sip_cfg.alwaysauthreject on, generates 2 events */
  324. snprintf(aclname, sizeof(aclname), "device_must_match_acl");
  325. sip_report_failed_acl(p, aclname);
  326. break;
  327. case AUTH_BAD_TRANSPORT:
  328. sip_report_inval_transport(p, sip_get_transport(req->socket.type));
  329. break;
  330. case AUTH_RTP_FAILED:
  331. break;
  332. case AUTH_SESSION_LIMIT:
  333. sip_report_session_limit(p);
  334. break;
  335. }
  336. if (peer_report) {
  337. sip_unref_peer(peer_report, "sip_report_security_event: sip_unref_peer: from handle_incoming");
  338. }
  339. return result;
  340. }