[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"$f-BmAelFTZuvRPl3DW_qBqPC6QZEq1wX3HZ4JFiR5YGk":3},{"id":4,"question":5,"answer":6,"answerHtml":7,"slug":8,"keywords":9,"article":10,"status":33,"aiModel":30,"aiConfidence":30,"updatedAt":49,"createdAt":49,"_status":48},200,"How can the Session mechanism in requests_ntlm improve efficiency in EWS interactions?","By using `requests.Session()` with `HttpNtlmAuth`, the Session mechanism reuses authentication credentials across multiple requests, reducing the number of NTLM handshakes. This shortens communication packets and improves performance, especially when performing bulk operations like email downloading. The article demonstrates switching to a session-based approach to streamline automated tasks in EWS development.","\u003Cp>By using `requests.Session()` with `HttpNtlmAuth`, the Session mechanism reuses authentication credentials across multiple requests, reducing the number of NTLM handshakes. This shortens communication packets and improves performance, especially when performing bulk operations like email downloading. The article demonstrates switching to a session-based approach to streamline automated tasks in EWS development.\u003C\u002Fp>\u003Cp>\u003Ca href=\"\u002Fnews\u002Fexchange-web-service-ews-development-guide-6-requests-ntlm\">Read the related One Day Sec article\u003C\u002Fa>\u003C\u002Fp>","how-can-the-session-mechanism-in-requests_ntlm-improve-efficiency-in-ews-interac-1777484719956","Session mechanism, requests.Session, HttpNtlmAuth, authentication efficiency, EWS, NTLM",{"id":11,"title":12,"slug":13,"description":14,"content":15,"contentHtml":27,"cover":30,"author":31,"views":19,"readingTime":32,"status":33,"publishedAt":34,"seo":35,"tags":39,"qaPairs":40,"meta":45,"updatedAt":46,"createdAt":47,"_status":48},52,"Exchange Web Service (EWS) Development Guide 6 – requests_ntlm","exchange-web-service-ews-development-guide-6-requests-ntlm","Learn to use requests_ntlm for NTLM authentication in EWS development. Includes hash-based auth, session optimization, and email automation code examples.",{"root":16},{"type":17,"format":18,"indent":19,"version":20,"children":21,"direction":29},"root","",0,1,[22],{"type":23,"format":18,"indent":19,"version":20,"children":24,"direction":29},"paragraph",[25],{"mode":26,"text":27,"type":28,"style":18,"detail":19,"format":19,"version":20},"normal","\u003Chtml>\u003Chead>\u003C\u002Fhead>\u003Cbody>\u003Ch2>0x00 Preface\u003C\u002Fh2>\u003Cp>---\u003C\u002Fp>\u003Cp>In previous articles, 'Exchange Web Service (EWS) Development Guide 4 – Auto Downloader' and 'Exchange Web Service (EWS) Development Guide 5 – exchangelib', two methods for accessing Exchange resources using hash were introduced, each with its own characteristics.\u003C\u002Fp>\u003Cp>The former employs a lower-level communication protocol, which is relatively cumbersome in terms of functionality implementation but helps in understanding the principles of communication protocols and vulnerability exploitation. The latter leverages the third-party library exchangelib, making development convenient but less suitable for vulnerability exploitation.\u003C\u002Fp>\u003Cp>From the perspective of vulnerability exploitation, using only third-party packages that encapsulate NTLM authentication neither affects vulnerability exploitation nor compromises efficiency.\u003C\u002Fp>\u003Cp>Therefore, this article selects the third-party package requests_ntlm, taking automated email downloading and attachment extraction as examples, to introduce its usage with open-source code.\u003C\u002Fp>\u003Ch2>0x01 Introduction\u003C\u002Fh2>\u003Cp>---\u003C\u002Fp>\u003Cp>This article will cover the following topics:\u003C\u002Fp>\u003Cul>\u003Cli>Usage of requests_ntlm\u003C\u002Fli>\u003Cli>Development details\u003C\u002Fli>\u003Cli>Open-source code\u003C\u002Fli>\u003C\u002Ful>\u003Ch2>0x02 Usage of requests_ntlm\u003C\u002Fh2>\u003Cp>---\u003C\u002Fp>\u003Cp>Documentation:\u003C\u002Fp>\u003Cp>https:\u002F\u002Fgithub.com\u002Frequests\u002Frequests-ntlm\u003C\u002Fp>\u003Ch3>1. Two Authentication Methods\u003C\u002Fh3>\u003Cp>I found the method for Hash-based authentication in requests_ntlm.py versions below 1.0.0, at the code location:\u003C\u002Fp>\u003Cp>https:\u002F\u002Fgithub.com\u002Frequests\u002Frequests-ntlm\u002Fblob\u002Fv0.3.0\u002Frequests_ntlm\u002Frequests_ntlm.py#L16\u003C\u002Fp>\u003Cp>Here you can find the parameter format for Hash-based authentication as ABCDABCDABCDABCD:ABCDABCDABCDABCD\u003C\u002Fp>\u003Cp>Example code for two methods to authenticate with Exchange is as follows:\u003C\u002Fp>\u003Ch4>(1) Plaintext Authentication\u003C\u002Fh4>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>target = \"192.168.1.1\"\u003Cbr>username = \"administrator@test.com\"\u003Cbr>password = \"password1\"\u003Cbr>res = requests.post(\"https:\u002F\u002F\" + target + \"\u002Fews\u002Fexchange.asmx\", data=POST_BODY, headers=headers, verify=False, auth=HttpNtlmAuth(username, password))\u003Cbr>print(res.status_code)\u003Cbr>print(res.text)\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Ch4>(2) Hash Authentication\u003C\u002Fh4>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>target = \"192.168.1.1\"\u003Cbr>username = \"administrator@test.com\"\u003Cbr>hash = \"00000000000000000000000000000000:5835048CE94AD0564E29A924A03510EF\"\u003Cbr>res = requests.post(\"https:\u002F\u002F\" + target + \"\u002Fews\u002Fexchange.asmx\", data=POST_BODY, headers=headers, verify=False, auth=HttpNtlmAuth(username, hash))\u003Cbr>print(res.status_code)\u003Cbr>print(res.text)\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Ch3>2. Session Mechanism\u003C\u002Fh3>\u003Cp>requests_ntlm supports the Session mechanism, which reduces the number of authentication attempts, shortens communication packets, and improves efficiency.\u003C\u002Fp>\u003Ch2>0x03 Development Details\u003C\u002Fh2>\u003Cp>---\u003C\u002Fp>\u003Ch3>1. Reimplement ewsManage_Downloader.py using requests_ntlm\u003C\u002Fh3>\u003Cp>Based on the original script ewsManage_Downloader.py, only the following replacements are needed.\u003C\u002Fp>\u003Cp>(1)\u003C\u002Fp>\u003Cp>Original code:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>status, responsetext = ntlm_auth_login(host, port, mode, domain, user, data, POST_BODY)\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>Replace with:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>res  = requests.post(\"https:\u002F\u002F\" + host + \"\u002Fews\u002Fexchange.asmx\", data=POST_BODY, headers=headers, verify=False, auth=HttpNtlmAuth(user, data))\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>(2)\u003C\u002Fp>\u003Cp>Original code:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>status\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>Replace with:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>res.status_code\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>(3)\u003C\u002Fp>\u003Cp>Original code:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>if res.status_code == \"200\" and \"NoError\" in responsetext:\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>Replace with:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>if res.status_code == 200 and \"NoError\" in res.text:\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>(4)\u003C\u002Fp>\u003Cp>Original code:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>host, port, mode, domain, user, data\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>Replace with:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>host, mode, user, data\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>(5)\u003C\u002Fp>\u003Cp>Original code:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>responsetext\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>Replace with:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>res.text\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>(6)\u003C\u002Fp>\u003Cp>Original code:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>sys.argv[1], int(sys.argv[2]), sys.argv[3], sys.argv[4], sys.argv[5], sys.argv[6]\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>Replace with:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>(7)\u003C\u002Fp>\u003Cp>Original code:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>path1 + '\\\\' + sys.argv[5]\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>Replace with:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>path1 + '\\\\' + sys.argv[3]\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>Finally, remove some redundant code\u003C\u002Fp>\u003Cp>The complete code has been uploaded to GitHub, address as follows:\u003C\u002Fp>\u003Cp>An open-source project\u003C\u002Fp>\u003Ch3>2. Reimplement ewsManage_Downloader.py using Session mechanism\u003C\u002Fh3>\u003Cp>Using Session mechanism can reduce the number of authentication attempts, shorten communication packets, and improve efficiency\u003C\u002Fp>\u003Cp>Therefore, reimplement ewsManage_Downloader.py using Session mechanism here\u003C\u002Fp>\u003Cp>Example of switching to Session mechanism:\u003C\u002Fp>\u003Cp>Original code:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>target = \"192.168.1.1\"\u003Cbr>username = \"administrator@test.com\"\u003Cbr>password = \"password1\"\u003Cbr>res = requests.post(\"https:\u002F\u002F\" + target + \"\u002Fews\u002Fexchange.asmx\", data=POST_BODY, headers=headers, verify=False, auth=HttpNtlmAuth(username, password))\u003Cbr>print(res.status_code)\u003Cbr>print(res.text)\u003Cbr>res = requests.post(\"https:\u002F\u002F\" + target + \"\u002Fews\u002Fexchange.asmx\", data=POST_BODY2, headers=headers, verify=False, auth=HttpNtlmAuth(username, password))\u003Cbr>print(res.status_code)\u003Cbr>print(res.text)\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>New code:\u003C\u002Fp>\u003Ctable>\u003Ctbody>\u003Ctr>\u003Ctd>\u003Cp>target = \"192.168.1.1\"\u003Cbr>username = \"administrator@test.com\"\u003Cbr>password = \"password1\"\u003Cbr>session = requests.Session()\u003Cbr>session.auth = HttpNtlmAuth(username, password)\u003Cbr>res = session.post(\"https:\u002F\u002F\" + target + \"\u002Fews\u002Fexchange.asmx\", data=POST_BODY, headers=headers, verify=False)\u003Cbr>print(res.status_code)\u003Cbr>print(res.text)\u003Cbr>res = session.post(\"https:\u002F\u002F\" + target + \"\u002Fews\u002Fexchange.asmx\", data=POST_BODY2, headers=headers, verify=False)\u003Cbr>print(res.status_code)\u003Cbr>print(res.text)\u003C\u002Fp>\u003C\u002Ftd>\u003C\u002Ftr>\u003C\u002Ftbody>\u003C\u002Ftable>\u003Cp>The complete code has been uploaded to GitHub at the following address:\u003C\u002Fp>\u003Cp>An open-source project\u003C\u002Fp>\u003Ch2>0x04 Summary\u003C\u002Fh2>\u003Cp>---\u003C\u002Fp>\u003Cp>In Exchange Web Service (EWS) development, using the third-party package requests_ntlm, which encapsulates the NTLM authentication process, not only improves development efficiency but also does not affect the writing of exploit code. Moreover, it can leverage the Session mechanism to reduce communication data during authentication, making it highly recommended.\u003C\u002Fp>\u003C\u002Fbody>\u003C\u002Fhtml>","text","ltr",null,"Onedaysec",3,"published","2026-02-02T08:19:21.006Z",{"title":36,"description":14,"keywords":37,"ogImage":30,"canonicalUrl":30,"noIndex":38},"EWS Development Guide: Using requests_ntlm for NTLM Authentication","Exchange Web Services, EWS development, requests_ntlm, NTLM authentication, email automation, Python Exchange",false,[],{"docs":41,"hasNextPage":38},[42,43,4,44],202,201,199,{"title":30,"description":30,"image":30},"2026-07-24T02:07:28.133Z","2026-07-23T16:01:10.528Z","draft","2026-07-23T16:04:29.462Z"]