{"id":564,"date":"2024-10-14T11:10:14","date_gmt":"2024-10-14T03:10:14","guid":{"rendered":"https:\/\/ahui.blog\/?p=564"},"modified":"2024-10-14T11:10:28","modified_gmt":"2024-10-14T03:10:28","slug":"django-cve-2024-45231","status":"publish","type":"post","link":"https:\/\/ahui.blog\/index.php\/2024\/10\/14\/django-cve-2024-45231\/","title":{"rendered":"Django\u903b\u8f91\u6f0f\u6d1e\u590d\u73b0\u5206\u6790\uff08CVE-2024-45231\uff09"},"content":{"rendered":"<h3>\u6f0f\u6d1e\u4fe1\u606f<\/h3>\n<p><strong>\u5b98\u65b9\u5b89\u5168\u516c\u544a\uff1a<\/strong><a href=\"https:\/\/www.djangoproject.com\/weblog\/2024\/sep\/03\/security-releases\/\">https:\/\/www.djangoproject.com\/weblog\/2024\/sep\/03\/security-releases\/<\/a><\/p>\n<p><strong>\u6f0f\u6d1e\u5f71\u54cd\uff1a<\/strong><\/p>\n<ul>\n<li>Django 5.1 &lt;  5.1.1<\/li>\n<li>Django 5.0 &lt; 5.0.9<\/li>\n<li>Django 4.2 &lt; 4.2.16<\/li>\n<\/ul>\n<p><strong>\u7b80\u4ecb\uff1a<\/strong>\u7531\u4e8e\u672a\u5904\u7406\u7684\u7535\u5b50\u90ae\u4ef6\u53d1\u9001\u5931\u8d25\uff0c <code>django.contrib.auth.forms.PasswordResetForm<\/code>\u7c7b\u5141\u8bb8\u8fdc\u7a0b\u653b\u51fb\u8005\u901a\u8fc7\u53d1\u51fa\u5bc6\u7801\u91cd\u7f6e\u8bf7\u6c42\u5e76\u89c2\u5bdf\u7ed3\u679c\u6765\u679a\u4e3e\u7528\u6237\u7535\u5b50\u90ae\u4ef6\u3002<\/p>\n<h3>\u6f0f\u6d1e\u73af\u5883\u642d\u5efa<\/h3>\n<p><strong>\u6ce8\u610f\uff1a\u8be5\u6f0f\u6d1e\u4e3a\u7535\u5b50\u90ae\u4ef6\u53d1\u9001\u5931\u8d25\u60c5\u51b5\u4e0b\u7684\u7528\u6237\u679a\u4e3e\uff0c\u6240\u4ee5\u6211\u4eec\u4f30\u8ba1\u8bbe\u7f6e\u4e86\u4e00\u4e2a\u65e0\u6548\u7684\u90ae\u4ef6\u670d\u52a1\u5668<\/strong><\/p>\n<p><strong>\u590d\u73b0\u73af\u5883\uff1a<\/strong><\/p>\n<ul>\n<li>Django\uff1a5.0.8\u7248\u672c<\/li>\n<li>Windows10<\/li>\n<li>Python3.12<\/li>\n<\/ul>\n<p><strong>Django\u73af\u5883\u90e8\u7f72\uff1a<\/strong><\/p>\n<p>\u4e00\u3001\u65b0\u5efa\u4e00\u4e2aDjango\uff0c\u6211\u8fd9\u91cc\u53ebHelloworld\uff0c\u4e0d\u4f1a\u65b0\u5efa\u53c2\u8003\u83dc\u9e1f\u6559\u7a0b<\/p>\n<p>\u4e8c\u3001\u7f16\u5199\u4e00\u4e0b\u4ee3\u7801<\/p>\n<p>Helloworld\/views.py<\/p>\n<pre><code class=\"language-python\">from django.contrib.auth.forms import PasswordResetForm\nfrom django.http import JsonResponse\n\ndef password_reset_api(request):\n    if request.method == &#039;POST&#039;:\n        form = PasswordResetForm(request.POST)\n        if form.is_valid():\n            # \u5c1d\u8bd5\u53d1\u9001\u5bc6\u7801\u91cd\u7f6e\u90ae\u4ef6\n            form.save(\n                request=request,\n                from_email=&#039;no-reply@example.com&#039;,\n                email_template_name=&#039;registration\/password_reset_email.html&#039;,\n            )\n            # \u8fd4\u56de\u6210\u529f\u54cd\u5e94\n            return JsonResponse({&#039;message&#039;: &#039;\u5bc6\u7801\u91cd\u7f6e\u90ae\u4ef6\u5df2\u53d1\u9001&#039;}, status=200)\n        else:\n            # \u8fd4\u56de\u8868\u5355\u9519\u8bef\n            return JsonResponse({&#039;errors&#039;: form.errors}, status=400)\n    else:\n        # \u8fd4\u56de\u65b9\u6cd5\u4e0d\u5141\u8bb8\u7684\u9519\u8bef\n        return JsonResponse({&#039;error&#039;: &#039;\u4ec5\u652f\u6301 POST \u8bf7\u6c42&#039;}, status=405)<\/code><\/pre>\n<p>Helloworld\/urls.py<\/p>\n<pre><code class=\"language-python\">from django.urls import path\nfrom .views import password_reset_api\n\nurlpatterns = [\n    path(&#039;api\/password_reset\/&#039;, password_reset_api, name=&#039;password_reset_api&#039;),\n]<\/code><\/pre>\n<p>Helloworld\/settings.py \u6700\u4e0b\u9762\u589e\u52a0\u4ee5\u4e0b\u5185\u5bb9<\/p>\n<pre><code class=\"language-python\">DATABASES = {\n    &#039;default&#039;: {\n        &#039;ENGINE&#039;: &#039;django.db.backends.sqlite3&#039;,  # \u6216\u5176\u4ed6\u6570\u636e\u5e93\u5f15\u64ce\n        &#039;NAME&#039;: BASE_DIR \/ &#039;db.sqlite3&#039;,         # \u6570\u636e\u5e93\u540d\u79f0\u6216\u8def\u5f84\n        # \u5176\u4ed6\u6570\u636e\u5e93\u914d\u7f6e\uff0c\u5982\u7528\u6237\u540d\u3001\u5bc6\u7801\u3001\u4e3b\u673a\u7b49\n    }\n}\n\nEMAIL_BACKEND = &#039;django.core.mail.backends.smtp.EmailBackend&#039;\nEMAIL_HOST = &#039;invalid.smtp.server&#039;  # \u6545\u610f\u4f7f\u7528\u65e0\u6548\u7684\u90ae\u4ef6\u670d\u52a1\u5668\nEMAIL_PORT = 587\nEMAIL_USE_TLS = True\nEMAIL_HOST_USER = &#039;your_email@example.com&#039;\nEMAIL_HOST_PASSWORD = &#039;your_email_password&#039;<\/code><\/pre>\n<p>templates\/registration\/password_reset_email.html<\/p>\n<pre><code class=\"language-html\">{% autoescape off %}\n\u8981\u91cd\u7f6e\u60a8\u7684\u5bc6\u7801\uff0c\u8bf7\u70b9\u51fb\u4ee5\u4e0b\u94fe\u63a5(\u6d4b\u8bd5\u7528\uff0c\u94fe\u63a5\u683c\u5f0f\u968f\u610f\uff0c\u5bf9\u4e0d\u5bf9\u90fd\u884c)\uff1a\n{{ protocol }}:\/\/{{ domain }}{{uid}}{{token}}\n{% endautoescape %}<\/code><\/pre>\n<p>\u4e09\u3001\u521b\u5efa\u5185\u7f6e\u7528\u6237\u5e76\u542f\u52a8Django\u73af\u5883<\/p>\n<pre><code class=\"language-shell\"># \u751f\u6210\u8fc1\u79fb\u6587\u4ef6\npython manage.py makemigrations\n\n# \u5e94\u7528\u8fc1\u79fb\uff0c\u521b\u5efa\u6570\u636e\u5e93\u8868\npython manage.py migrate\n\n# \u521b\u5efa\u8d85\u7ea7\u7528\u6237\npython manage.py createsuperuser\n\u63a5\u4e0b\u6765\u4f1a\u63d0\u793a\u4f60\u521b\u5efa\u7528\u6237\uff0c\u6839\u636e\u63d0\u793a\u521b\u5efa\u4e00\u4e2a\u7528\u6237\u5373\u53ef\uff0c\u7528\u4e8e\u540e\u7eed\u7684\u5bf9\u6bd4\u56de\u663e\uff0c\u6211\u8fd9\u91cc\u521b\u5efa\u4e86admin@qq.com\n\n# \u8fd0\u884c\u5f00\u53d1\u670d\u52a1\u5668\npython manage.py runserver<\/code><\/pre>\n<h3>\u6f0f\u6d1e\u590d\u73b0<\/h3>\n<p>\u8bbf\u95ee\u6211\u4eec\u7f16\u5199\u7684api\uff0c\u5e76\u53d1\u9001post\u8bf7\u6c42\uff0c\u76f4\u63a5\u4f7f\u7528HackBar\u5373\u53ef<\/p>\n<p>\u4e00\u3001\u6d4b\u8bd5\u7528\u6237\u5b58\u5728\u65f6\uff0c\u90ae\u4ef6\u53d1\u9001\u529f\u80fd\u4e0d\u53ef\u7528\u7684\u56de\u663e\u4e3a500<br \/>\n<img decoding=\"async\" src=\"https:\/\/ahui.blog\/wp-content\/uploads\/2024\/10\/image-20241011141737750.png\" alt=\"\" \/><\/p>\n<p>\u4e8c\u3001\u6d4b\u8bd5\u7528\u6237\u4e0d\u5b58\u5728\u65f6\uff0c\u90ae\u4ef6\u53d1\u9001\u529f\u80fd\u4e0d\u53ef\u7528\u7684\u56de\u663e\u4e3a200<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ahui.blog\/wp-content\/uploads\/2024\/10\/image-20241011142034780.png\" alt=\"image-20241011142034780\" \/><\/p>\n<h3>\u6f0f\u6d1e\u5206\u6790<\/h3>\n<p>\u4e00\u3001\u8c03\u8bd5\u7528\u6237\u5b58\u5728\u65f6\u7684\u4ee3\u7801\uff0c\u53d1\u73b0\u95ee\u9898\u70b9\u5728\u4e8e<code>django.contrib.auth.forms.PasswordResetForm<\/code>\u7c7b\u7684save\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">def save(\n    self,\n    domain_override=None,\n    subject_template_name=&quot;registration\/password_reset_subject.txt&quot;,\n    email_template_name=&quot;registration\/password_reset_email.html&quot;,\n    use_https=False,\n    token_generator=default_token_generator,\n    from_email=None,\n    request=None,\n    html_email_template_name=None,\n    extra_email_context=None,\n):\n    &quot;&quot;&quot;\n    Generate a one-use only link for resetting password and send it to the\n    user.\n    &quot;&quot;&quot;\n    email = self.cleaned_data[&quot;email&quot;]\n    if not domain_override:\n        current_site = get_current_site(request)\n        site_name = current_site.name\n        domain = current_site.domain\n    else:\n        site_name = domain = domain_override\n    email_field_name = UserModel.get_email_field_name()\n    for user in self.get_users(email):\n        user_email = getattr(user, email_field_name)\n        context = {\n            &quot;email&quot;: user_email,\n            &quot;domain&quot;: domain,\n            &quot;site_name&quot;: site_name,\n            &quot;uid&quot;: urlsafe_base64_encode(force_bytes(user.pk)),\n            &quot;user&quot;: user,\n            &quot;token&quot;: token_generator.make_token(user),\n            &quot;protocol&quot;: &quot;https&quot; if use_https else &quot;http&quot;,\n            **(extra_email_context or {}),\n        }\n        self.send_mail(\n            subject_template_name,\n            email_template_name,\n            context,\n            from_email,\n            user_email,\n            html_email_template_name=html_email_template_name,\n        )<\/code><\/pre>\n<p>\u4e8c\u3001\u5728\u5b58\u5728\u7528\u6237\u65f6\uff0c\u4ee3\u7801\u8fdb\u884c\u5230for user in self.get_users(email)\u65f6\uff0cself.get_users(email)\u4f1a\u83b7\u53d6\u5230\u7528\u6237\u7684email\uff0c\u7136\u540e\u6700\u7ec8\u8c03\u7528self.send_mail\u6765\u53d1\u9001\u91cd\u7f6e\u5bc6\u7801\u7684\u90ae\u4ef6\uff0c\u5982\u679c\u53d1\u9001\u90ae\u4ef6\u7684\u529f\u80fd\u9519\u8bef\u65f6\uff0c\u5c31\u4f1a\u4ea7\u751f500\u62a5\u9519<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ahui.blog\/wp-content\/uploads\/2024\/10\/image-20241011142716034.png\" alt=\"image-20241011142716034\" \/><\/p>\n<p>\u4e09\u3001\u5982\u679c\u7528\u6237\u4e0d\u5b58\u5728\uff0c\u4ee3\u7801\u8fdb\u884c\u5230for user in self.get_users(email)\u65f6\uff0cself.get_users(email)\u83b7\u53d6\u7684\u5185\u5bb9\u4e3a\u7a7a\uff0c\u90a3\u4e48\u4e5f\u5c31\u4e0d\u4f1a\u8fdb\u5165for\u5faa\u73af\u7684\u903b\u8f91\uff0c\u4ece\u800c\u76f4\u63a5\u5230\u4e0b\u4e00\u6b65\u8fd4\u56de200\u7ed9\u5ba2\u6237\u7aef<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ahui.blog\/wp-content\/uploads\/2024\/10\/\/image-20241011143211450.png\" alt=\"image-20241011143211450\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ahui.blog\/wp-content\/uploads\/2024\/10\/image-20241011143250782.png\" alt=\"image-20241011143250782\" \/><\/p>\n<h3>\u5b98\u65b9\u4fee\u590d\u65b9\u6cd5\u5206\u6790<\/h3>\n<p>\u5b98\u65b9\u7ed9send_mail\u65b9\u6cd5\u52a0\u5165\u4e86\u5f02\u5e38\u5904\u7406\uff0c\u4f7f\u5f97\u8be5\u65b9\u6cd5\u5728\u90ae\u4ef6\u670d\u52a1\u5668\u9519\u8bef\u65f6\u4e0d\u518d\u62a5\u9519<\/p>\n<p><strong>\u4fee\u590d\u524d\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ahui.blog\/wp-content\/uploads\/2024\/10\/image-20241011144001806.png\" alt=\"image-20241011144001806\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ahui.blog\/wp-content\/uploads\/2024\/10\/image-20241011142716034.png\" alt=\"image-20241011142716034\" \/><\/p>\n<p><strong>\u4fee\u590d\u540e\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ahui.blog\/wp-content\/uploads\/2024\/10\/image-20241011143748568.png\" alt=\"image-20241011143748568\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/ahui.blog\/wp-content\/uploads\/2024\/10\/image-20241011143606487.png\" alt=\"image-20241011143606487\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6f0f\u6d1e\u4fe1\u606f \u5b98\u65b9\u5b89\u5168\u516c\u544a\uff1ahttps:\/\/www.djangoproject.com\/weblog\/2024\/s &#8230; <a title=\"Django\u903b\u8f91\u6f0f\u6d1e\u590d\u73b0\u5206\u6790\uff08CVE-2024-45231\uff09\" class=\"read-more\" href=\"https:\/\/ahui.blog\/index.php\/2024\/10\/14\/django-cve-2024-45231\/\" aria-label=\"\u7ee7\u7eed\u9605\u8bfbDjango\u903b\u8f91\u6f0f\u6d1e\u590d\u73b0\u5206\u6790\uff08CVE-2024-45231\uff09\">\u9605\u8bfb\u66f4\u591a<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,8],"tags":[],"class_list":["post-564","post","type-post","status-publish","format-standard","hentry","category-recurrence_analysis","category-vulnerability_mining"],"_links":{"self":[{"href":"https:\/\/ahui.blog\/index.php\/wp-json\/wp\/v2\/posts\/564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ahui.blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ahui.blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ahui.blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ahui.blog\/index.php\/wp-json\/wp\/v2\/comments?post=564"}],"version-history":[{"count":1,"href":"https:\/\/ahui.blog\/index.php\/wp-json\/wp\/v2\/posts\/564\/revisions"}],"predecessor-version":[{"id":574,"href":"https:\/\/ahui.blog\/index.php\/wp-json\/wp\/v2\/posts\/564\/revisions\/574"}],"wp:attachment":[{"href":"https:\/\/ahui.blog\/index.php\/wp-json\/wp\/v2\/media?parent=564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ahui.blog\/index.php\/wp-json\/wp\/v2\/categories?post=564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ahui.blog\/index.php\/wp-json\/wp\/v2\/tags?post=564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}