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

2123: Improve the speed of bit flipping code #2204

Merged
merged 1 commit into from
Aug 17, 2022

Conversation

cakekoa
Copy link
Contributor

@cakekoa cakekoa commented Aug 17, 2022

What does this PR do?

Fixes #2123.

Improves the ransomware encryption performance by about 2-3x via the following improvements (in order of effectiveness):

  • Remove a function call
  • Use a generator
  • Use a more efficient flip calculation (subtraction instead of xor)

PR Checklist

  • Have you added an explanation of what your changes do and why you'd like to include them?
  • Is the TravisCI build passing?
  • Was the CHANGELOG.md updated to reflect the changes?
  • Was the documentation framework updated to reflect the changes?
  • Have you checked that you haven't introduced any duplicate code?

Testing Checklist

  • Added relevant unit tests?
  • Have you successfully tested your changes locally? Elaborate:

    Tested by running the unit tests, running a script to benchmark the performance improvement, and testing the encryption of a file.

  • If applicable, add screenshots or log transcripts of the feature working
    $ python3.7 ./test_bit_flip.py
    New method: 1.1626720659987768
    Old method: 2.61535847299092
    speedup: 2.2494377817052253
    $ python3.7 ./test_bit_flip.py
    New method: 0.9523072919982951
    Old method: 2.428888782989816
    speedup: 2.550530488843683
    $ python3.7 ./test_bit_flip.py
    New method: 0.9450294990092516
    Old method: 3.04705688499962
    speedup: 3.224298170791587
    $ python3.7 ./test_bit_flip.py
    New method: 1.3276102009986062
    Old method: 2.5821857940027257
    speedup: 1.94498791291333
    $ python3.7 ./test_bit_flip.py
    New method: 1.3388573650008766
    Old method: 2.565408591006417
    speedup: 1.916117921198228
    

@codecov
Copy link

codecov bot commented Aug 17, 2022

Codecov Report

Merging #2204 (2b5718f) into develop (2edaf52) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

@@             Coverage Diff             @@
##           develop    #2204      +/-   ##
===========================================
- Coverage    57.17%   57.16%   -0.01%     
===========================================
  Files          501      501              
  Lines        13440    13439       -1     
===========================================
- Hits          7684     7683       -1     
  Misses        5756     5756              
Impacted Files Coverage Δ
monkey/infection_monkey/utils/bit_manipulators.py 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@cakekoa cakekoa force-pushed the 2123-speedup-ransomware-encryption branch 2 times, most recently from 547c476 to 2b5718f Compare August 17, 2022 14:36
:param data: The bytes whose bits to flip
:return: Bytes with the bits flipped
"""
return bytes(generate_flipped_bits(data))
Copy link
Collaborator

@mssalvatore mssalvatore Aug 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe try this and see if performance changes:

Suggested change
return bytes(generate_flipped_bits(data))
return bytes(map(lambda byte: 255 - byte, data))

We could also try:

Suggested change
return bytes(generate_flipped_bits(data))
from functools import partial
from operator import sub
flip_bits = partial(sub, 255)
return bytes(map(flip_bits, data))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The map call is actually about 25% slower than the generator:

$ python3.7 ./test_bit_flip_new.py 
Generating random data...
Testing encryption methods...
New method: 21.68712888698792
Old method: 16.69294242199976
speedup: 0.7697165682459411
$ python3.7 ./test_bit_flip_new.py 
Generating random data...
Testing encryption methods...
New method: 21.919233062013518
Old method: 16.677228108019335
speedup: 0.7608490708062826

The use of the partial function improves the map call slightly, but it's still 20% slower than the generator:

$ python3.7 ./test_bit_flip_new.py 
Generating random data...
Testing encryption methods...
New method: 21.326405769999838
Old method: 17.232965190021787
speedup: 0.8080576434620619
$ python3.7 ./test_bit_flip_new.py 
Generating random data...
Testing encryption methods...
New method: 21.268566457991255
Old method: 16.70874281899887
speedup: 0.7856073822371269

- Remove a function call
- Use a generator
- Use a more efficient flip calculation (subtraction instead of xor)

Issue #2123
@cakekoa cakekoa force-pushed the 2123-speedup-ransomware-encryption branch from 2b5718f to 51ce19b Compare August 17, 2022 14:52
@mssalvatore mssalvatore merged commit 639fb26 into develop Aug 17, 2022
@mssalvatore mssalvatore deleted the 2123-speedup-ransomware-encryption branch August 17, 2022 14:52
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.

Ransomware Encryption Performance
2 participants