Exetools  

Go Back   Exetools > General > Community Tools

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 10-22-2018, 19:26
Elesty Elesty is offline
Friend
 
Join Date: Feb 2017
Posts: 9
Rept. Given: 0
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 9
Thanks Rcvd at 14 Times in 5 Posts
Elesty Reputation: 0
MD5 replace tool

Hi,

I just wrote a quick tool to patch all occurrences of a file inside a program folder (no matter the file name) given an MD5 checksum.
It is useful when the original files may have different names and are present in multiple sub directories.

Usage:
python replace.py FOLDER MD5_SUM CRACKED_FILE_PATH

Example:
python replace.py 098f6bcd4621d373cade4e832627b4f6 c:\mysoftware license.dll

Where:
-> 098f6bcd4621d373cade4e832627b4f6: is the MD5 of the original files to be replaced by the cracked file. You can get the MD5 checksum by using powershell with the function Get-FileHash -alg md5 ORIGINAL_FILE
-> c:\mysoftware is the folder of the software
-> license.dll is the path to the cracked file

You can also edit the file and set DEBUG_MODE=True to only print the occurrence of the files instead of replacing them.

Compiled version is also available attached.


Code:
import os
import sys
import hashlib
import shutil

DEBUG_MODE=False

if len(sys.argv) < 4:
  print("Usage: python replace.py FOLDER MD5_SUM FILE_PATH_TO_REPLACE_BY")
  sys.exit(-1)

g_path = sys.argv[1]
md5_to_find = sys.argv[2]
to_replace_by = sys.argv[3]

if not os.path.isfile(to_replace_by):
  print("Error: the path of the file to replace by does not exists")
  sys.exit(-1)

def md5sum(fname):
  try:
    hash_md5 = hashlib.md5()
    with open(fname, "rb") as f:
      for chunk in iter(lambda: f.read(4096), b""):
        hash_md5.update(chunk)
    return hash_md5.hexdigest()
  except:
    pass
  return "ERROR"

for root, directories, filenames in os.walk(g_path):
  for filename in filenames:
    full_path = os.path.join(root, filename)
    checksum = md5sum(full_path)
    #print(full_path, md5sum(full_path))
    if not DEBUG_MODE and checksum == md5_to_find:
      print("Replacing [%s] by [%s]" % (full_path, to_replace_by))
      shutil.copy(to_replace_by, full_path)
    elif checksum == md5_to_find:
      print("Should replace [%s] by [%s]" %  (full_path, to_replace_by))
Attached Files
File Type: 7z replace.7z (4.81 MB, 56 views)
Reply With Quote
The Following 2 Users Say Thank You to Elesty For This Useful Post:
Indigo (07-19-2019)
  #2  
Old 10-23-2018, 20:21
tigerfish tigerfish is offline
Guest
 
Join Date: Dec 2017
Posts: 3
Rept. Given: 0
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 4
Thanks Rcvd at 1 Time in 1 Post
tigerfish Reputation: 0
can it be uploaded to other site? thx
Reply With Quote
The Following User Says Thank You to tigerfish For This Useful Post:
Indigo (07-19-2019)
  #3  
Old 10-26-2018, 18:11
chants chants is offline
VIP
 
Join Date: Jul 2016
Posts: 723
Rept. Given: 35
Rept. Rcvd 48 Times in 30 Posts
Thanks Given: 665
Thanks Rcvd at 1,050 Times in 475 Posts
chants Reputation: 48
Probably you would include the file size as a pre-filter before filtering based on MD5 checksum. Otherwise this is the sort of inelegance that grinds away hard drives especially when huge files might be in that same directory. Since MD5 requires disk reading and is generally thereby expensive, it should not be treated so lightly. It should only be done when absolutely needed. Hence file size hint.
Reply With Quote
The Following 4 Users Say Thank You to chants For This Useful Post:
aldente (11-23-2019), Elesty (11-05-2018), Indigo (07-19-2019), Sir.V65j (02-25-2020)
  #4  
Old 10-29-2018, 01:52
ranadharm ranadharm is offline
Friend
 
Join Date: May 2012
Posts: 65
Rept. Given: 7
Rept. Rcvd 18 Times in 6 Posts
Thanks Given: 9
Thanks Rcvd at 21 Times in 18 Posts
ranadharm Reputation: 18
any chance to upload elsewhere ?
Reply With Quote
The Following User Says Thank You to ranadharm For This Useful Post:
Indigo (07-19-2019)
  #5  
Old 11-05-2018, 00:58
Elesty Elesty is offline
Friend
 
Join Date: Feb 2017
Posts: 9
Rept. Given: 0
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 9
Thanks Rcvd at 14 Times in 5 Posts
Elesty Reputation: 0
@chants: Thank you for your feedbacks

Version 2 with the following changelog:
-> Added a 4th optional argument MAX_FILE_SIZE_IN_MB, the md5 wont be computed is the file size in MB is larger than the MAX_FILE_SIZE_IN_MB specified.
-> If the 4th argument is not specified, all files md5 will be computed.

Code:
import os
import sys
import hashlib
import shutil

DEBUG_MODE=False

def get_filesize_mb(file_path):
  size_b = os.path.getsize(file_path)
  return size_b / 1048576 

def md5sum(fname):
  try:
    hash_md5 = hashlib.md5()
    with open(fname, "rb") as f:
      for chunk in iter(lambda: f.read(4096), b""):
        hash_md5.update(chunk)
    return hash_md5.hexdigest()
  except:
    pass
  return "ERROR"

if len(sys.argv) < 4:
  print("Usage: python replace.py FOLDER MD5_SUM_TO_FIND FILE_PATH_TO_REPLACE_BY [MAX_FILE_SIZE_IN_MB]")
  sys.exit(-1)

g_path = sys.argv[1]
md5_to_find = sys.argv[2].lower()
to_replace_by = sys.argv[3]

max_file_size = None
if len(sys.argv) >= 5:
  max_file_size = int(sys.argv[4])
  print("Maximum file size to check is %s MB" % sys.argv[4])

if not os.path.isfile(to_replace_by):
  print("Error: the path of the file to replace by does not exists")
  sys.exit(-1)

if __name__ == '__main__':
  for root, directories, filenames in os.walk(g_path):
    for filename in filenames:
      full_path = os.path.join(root, filename)
      fsize = get_filesize_mb(full_path)
      if max_file_size and get_filesize_mb(full_path) > max_file_size:
        # The current file size is too big, skipping it
        print("Skipping file [%s] with size %sMB" % (full_path, str(fsize)))
        continue
      # No maximum filesize specified, processing all files
      if max_file_size is None:
        checksum = md5sum(full_path)
        #print(full_path, md5sum(full_path))
        if not DEBUG_MODE and checksum == md5_to_find:
          print("Replacing [%s] by [%s]" % (full_path, to_replace_by))
          shutil.copy(to_replace_by, full_path)
        elif checksum == md5_to_find:
          print("Should replace [%s] by [%s]" %  (full_path, to_replace_by))
Attached Files
File Type: 7z replacer_v2.7z (4.81 MB, 41 views)

Last edited by Elesty; 11-05-2018 at 01:04.
Reply With Quote
The Following 3 Users Say Thank You to Elesty For This Useful Post:
athapa (11-29-2018), Indigo (07-19-2019), uel888 (11-08-2018)
  #6  
Old 11-29-2018, 03:19
athapa athapa is offline
Friend
 
Join Date: Jul 2013
Posts: 24
Rept. Given: 4
Rept. Rcvd 1 Time in 1 Post
Thanks Given: 6
Thanks Rcvd at 4 Times in 3 Posts
athapa Reputation: 1
If you include file size (in bytes) then the script would be even faster!
Reply With Quote
The Following 2 Users Say Thank You to athapa For This Useful Post:
Indigo (07-19-2019), niculaita (11-29-2018)
  #7  
Old 07-07-2019, 20:44
afeng
 
Posts: n/a
replace all file type?
Reply With Quote
The Following User Says Thank You to For This Useful Post:
Indigo (07-19-2019)
  #8  
Old 09-30-2019, 10:28
icscrew icscrew is offline
Friend
 
Join Date: Aug 2018
Posts: 12
Rept. Given: 0
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 5
Thanks Rcvd at 9 Times in 4 Posts
icscrew Reputation: 0
can @Elesty upload to other site.
cant download from attachment forum.(you do not have permission to access this page)
Reply With Quote
  #9  
Old 01-23-2020, 03:46
Stingered Stingered is offline
Friend
 
Join Date: Dec 2017
Posts: 256
Rept. Given: 0
Rept. Rcvd 2 Times in 2 Posts
Thanks Given: 296
Thanks Rcvd at 178 Times in 89 Posts
Stingered Reputation: 2
Quote:
Originally Posted by icscrew View Post
can @Elesty upload to other site.
cant download from attachment forum.(you do not have permission to access this page)
Download compiled exe HERE

fyi: Compiled using existing code in this thread.
Reply With Quote
  #10  
Old 01-23-2020, 04:35
Stingered Stingered is offline
Friend
 
Join Date: Dec 2017
Posts: 256
Rept. Given: 0
Rept. Rcvd 2 Times in 2 Posts
Thanks Given: 296
Thanks Rcvd at 178 Times in 89 Posts
Stingered Reputation: 2
Quote:
Originally Posted by Stingered View Post
Download compiled exe HERE

fyi: Compiled using existing code in this thread.
Updated D/L link HERE
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there any tool to replace the files packed in the NullSoft Install System package? BlackWhite General Discussion 4 09-02-2018 00:27
OllyCapstone :OllyDbg1.10 plugin to replace its original disasm engine with Capstone sh3dow Community Tools 3 09-27-2015 01:54
Search and Replace? prejker General Discussion 6 05-28-2004 23:32
how to replace kernel32.dll in win2k/xp tAz General Discussion 12 02-06-2004 03:46


All times are GMT +8. The time now is 13:25.


Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX, chessgod101
( 1998 - 2024 )