onepass 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 780ae33029de7cf60dfa6a1b16feb41dbff4c4e7
4
+ data.tar.gz: 087efe99c57afbc5e782b01b7dfd1774c30b731f
5
+ SHA512:
6
+ metadata.gz: 01fa89e1b5ee6a87c8ebd1cab37f88c4f98fc0fb638ad52f7b0d8e96d3799778c67c90a7dcf47ad4b41cecbbac53c481166b6ab06af865668932ce66f84df954
7
+ data.tar.gz: 4ae603513c043f6545973729efcc40d3e1863e2566d725b05d1aa9ab5356f8538e5fdd6c8c6a38a768eb5d1b7c5f5b9fb27553e20fabfa6d435f7ab57b4884cd
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in OnePass.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Kai Lieth
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/OnePass.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'OnePass/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "onepass"
8
+ spec.version = OnePass::VERSION
9
+ spec.authors = ["Kai Lieth"]
10
+ spec.email = ["kai@squareup.com"]
11
+ spec.summary = %q{Decrypt the secrets stored in 1Password 4}
12
+ spec.description = %q{A gem that decrypts the secrets that are stored in the 1Password 4 native SQLite database.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "sqlite3", "~> 1.3.9"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "highline"
26
+ end
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # OnePass
2
+
3
+ This is a gem to decrypt the 1Password 4 format from its internal sqlite database.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'OnePass'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install OnePass
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/klieth/OnePass/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "OnePass"
4
+ require "highline"
5
+
6
+ if __FILE__ == $0
7
+ require "highline/import"
8
+ master_password ||= ask('Enter 1Password Master Password') { |q| q.echo = false }
9
+ OnePass::Manager.new(master_password)
10
+ end
@@ -0,0 +1,3 @@
1
+ module OnePass
2
+ VERSION = "0.0.2"
3
+ end
data/lib/OnePass.rb ADDED
@@ -0,0 +1,123 @@
1
+ require "OnePass/version"
2
+ require "openssl"
3
+ require "sqlite3"
4
+ require "json"
5
+ require "tempfile"
6
+
7
+ module OnePass
8
+ class VerifyException < Exception
9
+ end
10
+
11
+ class Opdata
12
+ attr_reader :data
13
+
14
+ class InvalidException < Exception
15
+ end
16
+
17
+ def initialize(buf, key, mac)
18
+ if buf[0..7] != "opdata01"
19
+ raise OnePass::Opdata::InvalidException.new("Header was incorrect")
20
+ end
21
+ @length = buf[8..15].unpack("V")[0]
22
+ @mac = buf[-32..-1]
23
+ if OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, mac, buf[0..-33]) != @mac
24
+ raise OnePass::VerifyException.new("MAC doesn't match; verify failed. Check your encryption/mac keys.")
25
+ end
26
+ @data = decrypt(buf[16..-33], key)[-1*@length..-1]
27
+ end
28
+
29
+ private
30
+
31
+ def decrypt(data, key)
32
+ @iv = data[0..15]
33
+ cipher = OpenSSL::Cipher::AES.new(256, :CBC)
34
+ cipher.decrypt
35
+ cipher.padding = 0
36
+ cipher.iv = @iv
37
+ cipher.key = key
38
+ return cipher.update(data[16..-1]) + cipher.final
39
+ end
40
+ end
41
+
42
+ class Manager
43
+ def initialize(master_password, path = nil)
44
+ path ||= "#{ENV["HOME"]}/Library/Application Support/1Password 4/Data/OnePassword.sqlite"
45
+ raise "Can't find sqlite db at #{path}" unless File.exist? path
46
+
47
+ db_filename = File.basename(path)
48
+ dir_path = File.dirname(path)
49
+
50
+ # 1Password keeps the sqlite db open in exclusive mode. So we copy it to
51
+ # a tempdir and use that.
52
+ #
53
+ # Note that Dir.mktmpdir will clean up after itself when
54
+ # passed a block.
55
+ Dir.mktmpdir('OnePass') do |tmpdir|
56
+ FileUtils.cp_r("#{dir_path}/.", tmpdir)
57
+ sqlite_file = File.join(tmpdir, db_filename)
58
+
59
+ # roll the main db forward using write-ahead-log.
60
+ db = SQLite3::Database.new(sqlite_file)
61
+ db.execute "VACUUM;"
62
+
63
+ # read profile data
64
+ @overviews = []
65
+ @masters = []
66
+ db.execute "SELECT id,master_key_data,overview_key_data,salt,iterations FROM profiles" do |profile|
67
+
68
+ # derive the key from the password
69
+ derived_key = OpenSSL::PKCS5.pbkdf2_hmac(master_password, profile[3], profile[4], 64, OpenSSL::Digest::SHA512.new)
70
+ derived_encryption_key = derived_key[0..31]
71
+ derived_mac_key = derived_key[32..-1]
72
+
73
+ # try to unlock profile data. return fail if failed login
74
+ overview_key_data = OnePass::Opdata.new(profile[2], derived_encryption_key, derived_mac_key)
75
+ overview_key = OpenSSL::Digest::SHA512.new.digest(overview_key_data.data)
76
+ overview_encryption_key, overview_mac_key = overview_key[0..31], overview_key[32..-1]
77
+
78
+ # load overview opdata into object based format. overviews are stored decrypted for use later.
79
+ # the encrypted data for the keys is included, but is not decrypted unless requested later
80
+ db.execute "SELECT items.key_data, items.overview_data, item_details.data FROM items INNER JOIN item_details ON items.id=item_details.item_id WHERE items.profile_id=#{profile[0]};" do |row|
81
+ overview = OnePass::Opdata.new(row[1], overview_encryption_key, overview_mac_key)
82
+ json = JSON.parse(overview.data).merge({profile: profile[0], key_data: row[0], data: row[2]})
83
+ @overviews << json
84
+ end
85
+
86
+ # decrypt the master key for use later
87
+ master_key_data = OnePass::Opdata.new(profile[1], derived_encryption_key, derived_mac_key)
88
+ master_key = OpenSSL::Digest::SHA512.new.digest(master_key_data.data)
89
+ @masters[profile[0]] = {enc_key: master_key[0..31], mac_key: master_key[32..-1]}
90
+ end
91
+
92
+ db.close
93
+
94
+ # tmpdir removed when block exits
95
+ end
96
+ end
97
+
98
+ def load_all_regex(re)
99
+ all = []
100
+ @overviews.each do |overview|
101
+ all << overview if /#{re}/.match(overview["title"])
102
+ end
103
+ return all unless all.empty?
104
+ return nil
105
+ end
106
+
107
+ def decrypt(overview)
108
+ key_data = overview[:key_data][0..-33]
109
+ mac = overview[:key_data][-32..-1]
110
+ profile = overview[:profile]
111
+ if OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, @masters[profile][:mac_key], key_data) != mac
112
+ raise VerifyException.new("The item's encryption key couldn't be verified.")
113
+ end
114
+ cipher = OpenSSL::Cipher::AES.new(256, :CBC)
115
+ cipher.decrypt
116
+ cipher.padding = 0
117
+ cipher.iv = key_data[0..15]
118
+ cipher.key = @masters[profile][:enc_key]
119
+ key_data = cipher.update(key_data[16..-1]) + cipher.final
120
+ return JSON.parse(OnePass::Opdata.new(overview[:data],key_data[0..31],key_data[32..-1]).data)["password"]
121
+ end
122
+ end
123
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onepass
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Kai Lieth
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sqlite3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.9
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.3.9
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: highline
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A gem that decrypts the secrets that are stored in the 1Password 4 native
70
+ SQLite database.
71
+ email:
72
+ - kai@squareup.com
73
+ executables:
74
+ - onepass_test_stub
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - OnePass.gemspec
82
+ - README.md
83
+ - Rakefile
84
+ - bin/onepass_test_stub
85
+ - lib/OnePass.rb
86
+ - lib/OnePass/version.rb
87
+ homepage: ''
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.4.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Decrypt the secrets stored in 1Password 4
111
+ test_files: []