Skip to content

Instantly share code, notes, and snippets.

@romiras
Last active June 9, 2020 21:05
Show Gist options
  • Select an option

  • Save romiras/5b17372597d0887fbbc4f3f5e5ee7700 to your computer and use it in GitHub Desktop.

Select an option

Save romiras/5b17372597d0887fbbc4f3f5e5ee7700 to your computer and use it in GitHub Desktop.
EBK backup extraction scripts

How to use

Assumed you have files with extension .ebk stored by Kies.

Prerequisites

Install Ruby 2.3 or later

Example for running in Bash terminal. Linux is not mandatory for running Ruby program.

  1. Decrypt .ebk container into .bin file
# define environment variables for proper work - can be taken from https://goo.gl/gViBqb lines 31, 32
export KIES_KEY='HERE YOU PUT ENCRYPTION KEY'
export KIES_IV='HERE YOU PUT ENCRYPTION IV'

ruby ebk_decrypter.rb myfile.ebk

Header of .bin container represents XML file. If XML tag "zipType" has value "ZLib" (by Kies 4) then you can extract ZIP from .bin file with this command:

ruby extract_zip.rb myfile.ebk.bin
# Note: KIES_KEY and KIES_IV must be passed as environment variables for work.
require 'openssl'
CRYPT_BLOCK_LEN = 0x110
CRYPT_BLOCK_PAD = 0x100
cipher = OpenSSL::Cipher::AES256.new(:CBC)
cipher.decrypt
cipher.key = ENV['KIES_KEY']
cipher.iv = ENV['KIES_IV']
plain = ""
File.open(ARGV[0], 'rb') do |f|
begin
s = f.read(CRYPT_BLOCK_LEN)
plain << cipher.update(s)[0, CRYPT_BLOCK_PAD]
cipher.reset
end until f.eof
end
File.open(ARGV[0] + '.bin', 'wb') do |f2|
f2.write(plain)
end
# Uncomment this for debugging output
# print plain
# Read decrypted EBK file and write ZIP file
s = IO.binread ARGV[0]
i = s.bytes.rindex { |x| x != 16 }
IO.binwrite ARGV[0] + '.zip', s[512..i]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment