Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

automotive/uds: special case to allow sending malformed 7f request #4347

Merged
merged 1 commit into from Apr 23, 2024

Conversation

BenGardiner
Copy link
Contributor

@BenGardiner BenGardiner commented Apr 9, 2024

Similar to the motivation in #3947 (comment) : I would like to scan all the possible services including a nonsense/malformed/invalid request to a 7f service.

Checklist:

  • If you are new to Scapy: I have checked CONTRIBUTING.md (esp. section submitting-pull-requests)
  • I squashed commits belonging together
  • I added unit tests or explained why they are not relevant
  • I executed the regression tests (using cd test && ./run_tests or tox)
  • If the PR is still not finished, please create a Draft Pull Request

enables uds scanning of 0x00 - 0xff without crash

Similar to the motivation in #3947 (comment) : I would like to scan all the possible services including a nonsense/malformed/invalid request to a 7f service.

It is definitely invalid UDS to make requests to services with 0x40 bit set; however, the motivation is precisely to test targets with invalid UDS.

At present UDS_Scanner(isock, test_cases=[UDS_ServiceEnumerator], UDS_ServiceEnumerator_kwargs={...,'scan_range': range(256)}) will fail with AttributeError: requestServiceId when the scanner gets to sending 017f. This small patch makes scapy.contrib.automotive.uds.UDS.hashret() check if the payload.fields has requestedServiceId before trying to grab that field value for struct.pack().

I'm proposing the patch here even though this is invalid UDS because a crash seems like the wrong outcome. Happy to adapt the patch or testing -- or withdraw at you discretion.

@BenGardiner BenGardiner marked this pull request as draft April 9, 2024 17:14
Copy link

codecov bot commented Apr 9, 2024

Codecov Report

Merging #4347 (26d21f9) into master (0a2b2bc) will decrease coverage by 0.55%.
Report is 5 commits behind head on master.
The diff coverage is 100.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4347      +/-   ##
==========================================
- Coverage   82.03%   81.48%   -0.55%     
==========================================
  Files         350      350              
  Lines       82888    83136     +248     
==========================================
- Hits        67994    67744     -250     
- Misses      14894    15392     +498     
Files Coverage Δ
scapy/contrib/automotive/uds.py 98.41% <100.00%> (ø)

... and 40 files with indirect coverage changes

@polybassa
Copy link
Contributor

polybassa commented Apr 10, 2024

Hi, thanks for the PR. I think an additional check in hashret is a good idea in general.

I suggest to change the check to

‘’’ … and len(self) >= 2… ‘’’

I’m not a fan of string comparisons inside hashret.

@polybassa polybassa self-requested a review April 10, 2024 09:39
@polybassa polybassa added the bug label Apr 10, 2024
@polybassa
Copy link
Contributor

Could you please provide a test case as well?

@BenGardiner
Copy link
Contributor Author

I suggest to change the check to ... I’m not a fan of string comparisons inside hashret.

Will do

Could you please provide a test case as well?

I can, yes. I don't really know where to start; could you please tell me which file to add to @polybassa ?

@polybassa
Copy link
Contributor

Great.

I think you can copy this test case and change the scan_range

@BenGardiner
Copy link
Contributor Author

BenGardiner commented Apr 14, 2024 via email

@polybassa
Copy link
Contributor

I think you can just copy the testcase from line 259 to 305 and change the kwargs in line 284.

finally you need to change the asserts

@BenGardiner
Copy link
Contributor Author

OK I did eventually understand it enough to create testcases.

using … and len(self) >= 2… in hashret() was insufficient; there appears to be another case where len(self) > 1 BUT also .requestServiceId does not exist... (where bytes(self.payload) == b'\x00' or bytes(self.payload) == b'\x00\x00') . I added another compound condition here... but I am not confident this is correct.

@BenGardiner BenGardiner marked this pull request as ready for review April 22, 2024 13:21
Similar to the motivation in secdev#3947 (comment) :
I would like to scan all the possible services including a
nonsense/malformed/invalid request to a 7f service.

v2: adding unit test for range(256) of UDS Scanner and trying to avoid
expensive checks in .hashret() (@polybassa)
v3: flake8 formatting fixes
@polybassa
Copy link
Contributor

Looks good so far

@polybassa polybassa merged commit e7ae05a into secdev:master Apr 23, 2024
22 of 23 checks passed
@BenGardiner BenGardiner deleted the patch-1 branch April 23, 2024 19:06
polybassa added a commit that referenced this pull request Apr 25, 2024
gpotter2 pushed a commit that referenced this pull request Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants