security_events.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, 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. case SIP_TRANSPORT_WS:
  41. return AST_SECURITY_EVENT_TRANSPORT_TCP;
  42. case SIP_TRANSPORT_TLS:
  43. case SIP_TRANSPORT_WSS:
  44. return AST_SECURITY_EVENT_TRANSPORT_TLS;
  45. }
  46. return res;
  47. }
  48. void sip_report_invalid_peer(const struct sip_pvt *p)
  49. {
  50. char session_id[32];
  51. struct ast_security_event_inval_acct_id inval_acct_id = {
  52. .common.event_type = AST_SECURITY_EVENT_INVAL_ACCT_ID,
  53. .common.version = AST_SECURITY_EVENT_INVAL_ACCT_ID_VERSION,
  54. .common.service = "SIP",
  55. .common.account_id = p->exten,
  56. .common.local_addr = {
  57. .addr = &p->ourip,
  58. .transport = security_event_get_transport(p)
  59. },
  60. .common.remote_addr = {
  61. .addr = &p->sa,
  62. .transport = security_event_get_transport(p)
  63. },
  64. .common.session_id = session_id,
  65. };
  66. snprintf(session_id, sizeof(session_id), "%p", p);
  67. ast_security_event_report(AST_SEC_EVT(&inval_acct_id));
  68. }
  69. void sip_report_failed_acl(const struct sip_pvt *p, const char *aclname)
  70. {
  71. char session_id[32];
  72. struct ast_security_event_failed_acl failed_acl_event = {
  73. .common.event_type = AST_SECURITY_EVENT_FAILED_ACL,
  74. .common.version = AST_SECURITY_EVENT_FAILED_ACL_VERSION,
  75. .common.service = "SIP",
  76. .common.account_id = p->exten,
  77. .common.local_addr = {
  78. .addr = &p->ourip,
  79. .transport = security_event_get_transport(p)
  80. },
  81. .common.remote_addr = {
  82. .addr = &p->sa,
  83. .transport = security_event_get_transport(p)
  84. },
  85. .common.session_id = session_id,
  86. .acl_name = aclname,
  87. };
  88. snprintf(session_id, sizeof(session_id), "%p", p);
  89. ast_security_event_report(AST_SEC_EVT(&failed_acl_event));
  90. }
  91. void sip_report_inval_password(const struct sip_pvt *p, const char *response_challenge, const char *response_hash)
  92. {
  93. char session_id[32];
  94. struct ast_security_event_inval_password inval_password = {
  95. .common.event_type = AST_SECURITY_EVENT_INVAL_PASSWORD,
  96. .common.version = AST_SECURITY_EVENT_INVAL_PASSWORD_VERSION,
  97. .common.service = "SIP",
  98. .common.account_id = p->exten,
  99. .common.local_addr = {
  100. .addr = &p->ourip,
  101. .transport = security_event_get_transport(p)
  102. },
  103. .common.remote_addr = {
  104. .addr = &p->sa,
  105. .transport = security_event_get_transport(p)
  106. },
  107. .common.session_id = session_id,
  108. .challenge = p->nonce,
  109. .received_challenge = response_challenge,
  110. .received_hash = response_hash,
  111. };
  112. snprintf(session_id, sizeof(session_id), "%p", p);
  113. ast_security_event_report(AST_SEC_EVT(&inval_password));
  114. }
  115. void sip_report_auth_success(const struct sip_pvt *p, uint32_t *using_password)
  116. {
  117. char session_id[32];
  118. struct ast_security_event_successful_auth successful_auth = {
  119. .common.event_type = AST_SECURITY_EVENT_SUCCESSFUL_AUTH,
  120. .common.version = AST_SECURITY_EVENT_SUCCESSFUL_AUTH_VERSION,
  121. .common.service = "SIP",
  122. .common.account_id = p->exten,
  123. .common.local_addr = {
  124. .addr = &p->ourip,
  125. .transport = security_event_get_transport(p)
  126. },
  127. .common.remote_addr = {
  128. .addr = &p->sa,
  129. .transport = security_event_get_transport(p)
  130. },
  131. .common.session_id = session_id,
  132. .using_password = using_password,
  133. };
  134. snprintf(session_id, sizeof(session_id), "%p", p);
  135. ast_security_event_report(AST_SEC_EVT(&successful_auth));
  136. }
  137. void sip_report_session_limit(const struct sip_pvt *p)
  138. {
  139. char session_id[32];
  140. struct ast_security_event_session_limit session_limit = {
  141. .common.event_type = AST_SECURITY_EVENT_SESSION_LIMIT,
  142. .common.version = AST_SECURITY_EVENT_SESSION_LIMIT_VERSION,
  143. .common.service = "SIP",
  144. .common.account_id = p->exten,
  145. .common.local_addr = {
  146. .addr = &p->ourip,
  147. .transport = security_event_get_transport(p)
  148. },
  149. .common.remote_addr = {
  150. .addr = &p->sa,
  151. .transport = security_event_get_transport(p)
  152. },
  153. .common.session_id = session_id,
  154. };
  155. snprintf(session_id, sizeof(session_id), "%p", p);
  156. ast_security_event_report(AST_SEC_EVT(&session_limit));
  157. }
  158. void sip_report_failed_challenge_response(const struct sip_pvt *p, const char *response, const char *expected_response)
  159. {
  160. char session_id[32];
  161. char account_id[256];
  162. struct ast_security_event_chal_resp_failed chal_resp_failed = {
  163. .common.event_type = AST_SECURITY_EVENT_CHAL_RESP_FAILED,
  164. .common.version = AST_SECURITY_EVENT_CHAL_RESP_FAILED_VERSION,
  165. .common.service = "SIP",
  166. .common.account_id = account_id,
  167. .common.local_addr = {
  168. .addr = &p->ourip,
  169. .transport = security_event_get_transport(p)
  170. },
  171. .common.remote_addr = {
  172. .addr = &p->sa,
  173. .transport = security_event_get_transport(p)
  174. },
  175. .common.session_id = session_id,
  176. .challenge = p->nonce,
  177. .response = response,
  178. .expected_response = expected_response,
  179. };
  180. if (!ast_strlen_zero(p->from)) { /* When dialing, show account making call */
  181. ast_copy_string(account_id, p->from, sizeof(account_id));
  182. } else {
  183. ast_copy_string(account_id, p->exten, sizeof(account_id));
  184. }
  185. snprintf(session_id, sizeof(session_id), "%p", p);
  186. ast_security_event_report(AST_SEC_EVT(&chal_resp_failed));
  187. }
  188. void sip_report_chal_sent(const struct sip_pvt *p)
  189. {
  190. char session_id[32];
  191. char account_id[256];
  192. struct ast_security_event_chal_sent chal_sent = {
  193. .common.event_type = AST_SECURITY_EVENT_CHAL_SENT,
  194. .common.version = AST_SECURITY_EVENT_CHAL_SENT_VERSION,
  195. .common.service = "SIP",
  196. .common.account_id = account_id,
  197. .common.local_addr = {
  198. .addr = &p->ourip,
  199. .transport = security_event_get_transport(p)
  200. },
  201. .common.remote_addr = {
  202. .addr = &p->sa,
  203. .transport = security_event_get_transport(p)
  204. },
  205. .common.session_id = session_id,
  206. .challenge = p->nonce,
  207. };
  208. if (!ast_strlen_zero(p->from)) { /* When dialing, show account making call */
  209. ast_copy_string(account_id, p->from, sizeof(account_id));
  210. } else {
  211. ast_copy_string(account_id, p->exten, sizeof(account_id));
  212. }
  213. snprintf(session_id, sizeof(session_id), "%p", p);
  214. ast_security_event_report(AST_SEC_EVT(&chal_sent));
  215. }
  216. void sip_report_inval_transport(const struct sip_pvt *p, const char *transport)
  217. {
  218. char session_id[32];
  219. struct ast_security_event_inval_transport inval_transport = {
  220. .common.event_type = AST_SECURITY_EVENT_INVAL_TRANSPORT,
  221. .common.version = AST_SECURITY_EVENT_INVAL_TRANSPORT_VERSION,
  222. .common.service = "SIP",
  223. .common.account_id = p->exten,
  224. .common.local_addr = {
  225. .addr = &p->ourip,
  226. .transport = security_event_get_transport(p)
  227. },
  228. .common.remote_addr = {
  229. .addr = &p->sa,
  230. .transport = security_event_get_transport(p)
  231. },
  232. .common.session_id = session_id,
  233. .transport = transport,
  234. };
  235. snprintf(session_id, sizeof(session_id), "%p", p);
  236. ast_security_event_report(AST_SEC_EVT(&inval_transport));
  237. }
  238. int sip_report_security_event(const struct sip_pvt *p, const struct sip_request *req, const int res) {
  239. struct sip_peer *peer_report;
  240. enum check_auth_result res_report = res;
  241. struct ast_str *buf;
  242. char *c;
  243. const char *authtoken;
  244. char *reqheader, *respheader;
  245. int result = 0;
  246. char aclname[256];
  247. struct digestkeys keys[] = {
  248. [K_RESP] = { "response=", "" },
  249. [K_URI] = { "uri=", "" },
  250. [K_USER] = { "username=", "" },
  251. [K_NONCE] = { "nonce=", "" },
  252. [K_LAST] = { NULL, NULL}
  253. };
  254. peer_report = sip_find_peer(p->exten, NULL, TRUE, FINDPEERS, FALSE, 0);
  255. switch(res_report) {
  256. case AUTH_DONT_KNOW:
  257. break;
  258. case AUTH_SUCCESSFUL:
  259. if (peer_report) {
  260. if (ast_strlen_zero(peer_report->secret) && ast_strlen_zero(peer_report->md5secret)) {
  261. sip_report_auth_success(p, (uint32_t *) 0);
  262. } else {
  263. sip_report_auth_success(p, (uint32_t *) 1);
  264. }
  265. }
  266. break;
  267. case AUTH_CHALLENGE_SENT:
  268. sip_report_chal_sent(p);
  269. break;
  270. case AUTH_SECRET_FAILED:
  271. case AUTH_USERNAME_MISMATCH:
  272. sip_auth_headers(WWW_AUTH, &respheader, &reqheader);
  273. authtoken = sip_get_header(req, reqheader);
  274. buf = ast_str_thread_get(&check_auth_buf, CHECK_AUTH_BUF_INITLEN);
  275. ast_str_set(&buf, 0, "%s", authtoken);
  276. c = ast_str_buffer(buf);
  277. sip_digest_parser(c, keys);
  278. if (res_report == AUTH_SECRET_FAILED) {
  279. sip_report_inval_password(p, keys[K_NONCE].s, keys[K_RESP].s);
  280. } else {
  281. if (peer_report) {
  282. sip_report_failed_challenge_response(p, keys[K_USER].s, peer_report->username);
  283. }
  284. }
  285. break;
  286. case AUTH_NOT_FOUND:
  287. /* with sip_cfg.alwaysauthreject on, generates 2 events */
  288. sip_report_invalid_peer(p);
  289. break;
  290. case AUTH_UNKNOWN_DOMAIN:
  291. snprintf(aclname, sizeof(aclname), "domain_must_match");
  292. sip_report_failed_acl(p, aclname);
  293. break;
  294. case AUTH_PEER_NOT_DYNAMIC:
  295. snprintf(aclname, sizeof(aclname), "peer_not_dynamic");
  296. sip_report_failed_acl(p, aclname);
  297. break;
  298. case AUTH_ACL_FAILED:
  299. /* with sip_cfg.alwaysauthreject on, generates 2 events */
  300. snprintf(aclname, sizeof(aclname), "device_must_match_acl");
  301. sip_report_failed_acl(p, aclname);
  302. break;
  303. case AUTH_BAD_TRANSPORT:
  304. sip_report_inval_transport(p, sip_get_transport(req->socket.type));
  305. break;
  306. case AUTH_RTP_FAILED:
  307. break;
  308. case AUTH_SESSION_LIMIT:
  309. sip_report_session_limit(p);
  310. break;
  311. }
  312. if (peer_report) {
  313. sip_unref_peer(peer_report, "sip_report_security_event: sip_unref_peer: from handle_incoming");
  314. }
  315. return result;
  316. }