Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improved Multipart Form handling#5598

Merged
stamparm merged 3 commits intosqlmapproject:masterfrom
rohitkumarankam:master
Jan 9, 2024
Merged

Improved Multipart Form handling#5598
stamparm merged 3 commits intosqlmapproject:masterfrom
rohitkumarankam:master

Conversation

@rohitkumarankam
Copy link
Copy Markdown
Contributor

Fixes and Improvements in multipart form handling

1. improved multipart marker

before

-----------------------------106520758920784323291953035226
Content-Disposition: form-data; name="name"
*

-----------------------------106520758920784323291953035226
Content-Disposition: form-data; name="email"

[email protected]*
-----------------------------106520758920784323291953035226--

marking at wrong location

after fix

-----------------------------106520758920784323291953035226
Content-Disposition: form-data; name="name"

*
-----------------------------106520758920784323291953035226
Content-Disposition: form-data; name="email"

[email protected]*
-----------------------------106520758920784323291953035226--

refering to rfc 2046 multipart forms must have one CRLF between field header and field body. but here multipart filed processor is marking at wrong location in name field.

fix: in lib/core/target.py line 229
(?si)((Content-Disposition[^\n]+?name\s*=\s*[\"']?(?P<name>[^\"'\r\n]+)[\"']?).+?)((%s)+--)
(?si)((Content-Disposition[^\n]+?name\s*=\s*[\"']?(?P<name>[^\"'\r\n]+)[\"']?).+?)((%s)--)

this regex group ((%s)+--) is trying to match as many CRLFs as possible but according to rfc need to match one CRLF before -- removing + will only match one CRLF

2. Fixed file corruption.

before

-----------------------------106520758920784323291953035226
Content-Disposition: form-data; name="name"

rohit*
-----------------------------106520758920784323291953035226
Content-Disposition: form-data; name="testfile"; filename="test.pdf"
Content-Type: application/pdf

filecontents*
-----------------------------106520758920784323291953035226--

after change

-----------------------------106520758920784323291953035226
Content-Disposition: form-data; name="name"

rohit*
-----------------------------106520758920784323291953035226
Content-Disposition: form-data; name="testfile"; filename="test.pdf"
Content-Type: application/pdf

filecontents
-----------------------------106520758920784323291953035226--

marking file fields is leading to file corruption.
added a if condition to check if the field is file or not.

3. Improved dumb LF to CRLF converter in case of Multipart file headers.

before:

b'-----------------------------106520758920784323291953035226\r\nContent-Disposition: form-data; name="file"; filename="test.pdf"\r\nContent-Type: application/pdf\n\nfilecontents\r\n-----------------------------106520758920784323291953035226--'

it's not properly converting LF to CRLF in case there is any extra header. to fix this added Content-Type header which is most common.

b'-----------------------------106520758920784323291953035226\r\nContent-Disposition: form-data; name="testfile"; filename="test.pdf"\r\nContent-Type: application/pdf\r\n\r\nidk filename\r\n-----------------------------106520758920784323291953035226--'

@stamparm stamparm merged commit 93a8828 into sqlmapproject:master Jan 9, 2024
@stamparm
Copy link
Copy Markdown
Member

stamparm commented Jan 9, 2024

functools.partial was there for a reason

stamparm added a commit that referenced this pull request Jan 9, 2024
stamparm added a commit that referenced this pull request Jan 9, 2024
@stamparm
Copy link
Copy Markdown
Member

stamparm commented Jan 9, 2024

@rohitkumarankam can you please copy-paste the request files and pinpoint what's the current and what's the expected behavior?

@rohitkumarankam
Copy link
Copy Markdown
Contributor Author

testreq.raw.txt
in case of empty form fields payload is getting inserted at wrong location.

current behavior (look at course_name field)

POST https://example.com/help_center/send_help_center_query HTTP/1.1
host: example.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
X-Requested-With: XMLHttpRequest
Content-Type: multipart/form-data; boundary=---------------------------415273848417249099023591489318
content-length: 295
Origin: https://example.com
Connection: keep-alive
Cookie: PHPSESSID=r4cepsk5nkrk2brors14sah119ov0mkt
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin

-----------------------------415273848417249099023591489318
Content-Disposition: form-data; name="name"

rohit*
-----------------------------415273848417249099023591489318
Content-Disposition: form-data; name="course_name"
*

-----------------------------415273848417249099023591489318--

but as per the spec there should be a empty line(CRLF) after header and before body.

expected behavior

POST https://example.com/help_center/send_help_center_query HTTP/1.1
host: example.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0
Accept: application/json, text/plain, */*
Accept-Language: en-US,en;q=0.5
X-Requested-With: XMLHttpRequest
Content-Type: multipart/form-data; boundary=---------------------------415273848417249099023591489318
content-length: 295
Origin: https://example.com
Connection: keep-alive
Cookie: PHPSESSID=r4cepsk5nkrk2brors14sah119ov0mkt
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin

-----------------------------415273848417249099023591489318
Content-Disposition: form-data; name="name"

rohit*
-----------------------------415273848417249099023591489318
Content-Disposition: form-data; name="course_name"

*
-----------------------------415273848417249099023591489318--

this behavior is fixed through this commit.

@stamparm
Copy link
Copy Markdown
Member

stamparm commented Jan 9, 2024

@rohitkumarankam ok. fixed with latest commit (same as you proposed)

ThisIzAli2 pushed a commit to ThisIzAli2/sqlmap that referenced this pull request May 4, 2024
* improved multipart marker

* Improved file field handling in Multipart forms

* improved dumb LF to CRLF converter
ThisIzAli2 pushed a commit to ThisIzAli2/sqlmap that referenced this pull request May 4, 2024
ThisIzAli2 pushed a commit to ThisIzAli2/sqlmap that referenced this pull request May 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants