详情

火影 Lv.8

关注
auth_delay 让服务器在报告身份验证失败前短暂停息,以增加对数据库密码进行暴力破解的难度。须要留意的是,这对阻止拒绝服务攻击毫无帮助,甚至可能加剧攻击,由于在报告身份验证失败前等待的进程仍会占用连接。
要利用这个模块必须要在 postgresql.conf 中设置参数
  1. shared_preload_libraries = 'auth_delay'
  2. auth_delay.milliseconds = '500'
这个代码比较简单,一共分为三个部分。

  • hook 函数
在 libpq 中界说了一个 ClientAuthentication_hook 函数指针,代码如下:
  1. typedef void (*ClientAuthentication_hook_type) (Port *, int);
  2. /*
  3. * This hook allows plugins to get control following client authentication,
  4. * but before the user has been informed about the results.  It could be used
  5. * to record login events, insert a delay after failed authentication, etc.
  6. */
  7. ClientAuthentication_hook_type ClientAuthentication_hook = NULL;

  • _PG_init 函数
  1. void
  2. _PG_init(void)
  3. {
  4.         /* Define custom GUC variables */
  5.         DefineCustomIntVariable("auth_delay.milliseconds",
  6.                                                         "Milliseconds to delay before reporting authentication failure",
  7.                                                         NULL,
  8.                                                         &auth_delay_milliseconds,
  9.                                                         0,
  10.                                                         0, INT_MAX / 1000,
  11.                                                         PGC_SIGHUP,
  12.                                                         GUC_UNIT_MS,
  13.                                                         NULL,
  14.                                                         NULL,
  15.                                                         NULL);
  16.         /* Install Hooks */
  17.         original_client_auth_hook = ClientAuthentication_hook;
  18.         ClientAuthentication_hook = auth_delay_checks;
  19. }
这个 _PG_init 函数会在模块调用之前执行,这个函数主要实现的功能是界说了一个 GUC 参数 auth_delay.milliseconds 这个参数我们须要在 postgresql.conf 中进行设置,然后它将原来的钩子函数保存在了 original_client_auth_hook 变量中,然后将我们自己的 auth_delay_checks 函数赋给了 ClientAuthentication_hook 变量,这样我们就可以让数据库调用我们的函数了。

  • auth_delay_checks 函数
  1. static void
  2. auth_delay_checks(Port *port, int status)
  3. {
  4.         /*
  5.          * Any other plugins which use ClientAuthentication_hook.
  6.          */
  7.         if (original_client_auth_hook)
  8.                 original_client_auth_hook(port, status);
  9.         /*
  10.          * Inject a short delay if authentication failed.
  11.          */
  12.         if (status != STATUS_OK)
  13.         {
  14.                 pg_usleep(1000L * auth_delay_milliseconds);
  15.         }
  16. }
这个函数的意思是如果原来存在有钩子函数就先运行原来的钩子函数,然后我们看一下验证的状态 status 是不是精确,如果不精确,就延时 auth_delay_milliseconds 个时间。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
93阅读
0回复

暂无评论,点我抢沙发吧