nomeaning-ctf 0.0.3

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: c3a7148c4238a98d0574bc50be9b388b0548ed7e
4
+ data.tar.gz: 367fe6f527bf7bc3a41d49234914ef6e21c9c374
5
+ SHA512:
6
+ metadata.gz: 00af00ff18cb4c1b208ab194a69bd1daab0ccc61d9bf476fe59116e2b4f6564c03a810c1768f5753fc681bd2eff9fd3086f6017d6143ed7e565b669ca58357ca
7
+ data.tar.gz: dedf64cbae558382b59f8d47fd5baadb1f3cb4485f5ea59e33132e89e5700bfb2628eb55d759f208798e65265134ab306f5b2f2707abb428964a355ec437df21
data/.gitignore ADDED
@@ -0,0 +1,16 @@
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
15
+ .idea
16
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ctf.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 nomeaning
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/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # CTF
2
+
3
+ Add some useful method for solving CTF problems.
4
+
5
+ ## Usage
6
+
7
+ See examples directory.
8
+
9
+ ## Contributing
10
+
11
+ Fork and create pull request.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/ctf.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ctf/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nomeaning-ctf"
8
+ spec.version = CTF::VERSION
9
+ spec.authors = ["nomeaning"]
10
+ spec.email = ["nomeaning@mma.club.uec.ac.jp"]
11
+ spec.summary = %q{Utils for CTF}
12
+ spec.description = %q{Utils for CTF}
13
+ spec.homepage = "https://bitbucket.org/nomeaning777/ctf"
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_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_dependency "io-interactive"
25
+ spec.add_dependency "highline"
26
+ spec.add_dependency "metasm", '~> 1.0.2'
27
+ end
@@ -0,0 +1,8 @@
1
+ require 'ctf'
2
+
3
+ TCPSocket.open(ARGV[0], ARGV[1]) do |s|
4
+ s.echo = true # Enable debug output
5
+ s.echo_color = true # Enable output with color(Default)
6
+
7
+ s.expect(/hoge/) # Read until match /hoge/. See IO#expect
8
+ end
data/lib/ctf/math.rb ADDED
@@ -0,0 +1,179 @@
1
+ require 'prime'
2
+ require 'pp'
3
+ module CTF
4
+ module Math
5
+ def sqrt?(n)
6
+ sqrtint(n) ** 2 == n
7
+ end
8
+
9
+ def sqrtint(n)
10
+ root(n, 2)
11
+ end
12
+
13
+ def root(n, k)
14
+ low, high = 0, n + 1
15
+ while low + 1 < high
16
+ mid = (low + high) / 2
17
+ if n < mid ** k
18
+ high = mid
19
+ else
20
+ low = mid
21
+ end
22
+ end
23
+ low
24
+ end
25
+
26
+ def mod_pow(a, n, mod)
27
+ ret = 1
28
+ while n > 0
29
+ ret = (ret * a) % mod if n.odd?
30
+ a = (a * a) % mod
31
+ n >>= 1
32
+ end
33
+ ret
34
+ end
35
+
36
+ def extgcd(a,b)
37
+ return [1,0] if b == 0
38
+ y,x = extgcd(b, a % b)
39
+ y -= (a/b)*x
40
+ return [x,y]
41
+ end
42
+
43
+ def mod_inverse(a, mod)
44
+ x,y = extgcd(a, mod)
45
+ return x % mod
46
+ end
47
+
48
+ def chinese_remainder(m1,m2,a,b)
49
+ return (m2 * a * mod_inverse(m2,m1) + m1 * b * mod_inverse(m1,m2)) % (m1 * m2)
50
+ end
51
+
52
+ # 離散対数 O(k ^ (1/2) * log(mod))
53
+ def discrete_log(a, b, mod, k = nil)
54
+ n = ::Math::sqrt(k || mod).ceil + 1
55
+ p, q = 1, b
56
+ inverse = mod_pow(mod_inverse(a, mod), n, mod)
57
+ t = Hash.new
58
+ n.times do |i|
59
+ t[p] = i unless t.key?(q)
60
+ p = p * a % mod
61
+ end
62
+ n.times do |i|
63
+ return i * n + t[q] if t.key?(q)
64
+ q = (q * inverse) % mod
65
+ end
66
+ return nil # not found
67
+ end
68
+
69
+ # Pohlig-Hellman Algorithmによる離散対数
70
+ # http://en.wikipedia.org/wiki/Pohlig%E2%80%93Hellman_algorithm
71
+ # require mod is prime!
72
+ def discrete_log2(g, e, mod)
73
+ res = 0
74
+ mod2 = 1
75
+ prime_factorization2(mod - 1).each do |pi, ei|
76
+ m = pi ** ei
77
+ ng = mod_pow(g,(mod - 1) / m, mod)
78
+ ne = mod_pow(e,(mod - 1) / m, mod)
79
+ x = discrete_log(ng, ne, mod, m)
80
+ res = chinese_remainder(mod2, m, res, x % m)
81
+ mod2 *= m
82
+ end
83
+ return res
84
+ end
85
+
86
+ def check_prime(p, count = nil)
87
+ return true if [2,3].include?(p)
88
+ return false if p.even? || p < 2
89
+
90
+ d, s = p - 1, 0
91
+ d, s = d >> 1, s + 1 while d.even?
92
+
93
+ count = [16, p.to_s(4).size].max unless count
94
+ count.times do
95
+ a = rand(2...(p - 1))
96
+ return false if p.gcd(a) != 1
97
+ if (x = mod_pow(a, d, p)) != 1
98
+ return false unless (0...s).inject(false) do |res, r|
99
+ break true if(x == p - 1)
100
+ x = x * x % p
101
+ next false
102
+ end
103
+ end
104
+ end
105
+ return true
106
+ end
107
+
108
+ # 素因数分解(試し割り法)
109
+ def prime_factorization(n)
110
+ res = []
111
+ Prime.each do |p|
112
+ break if p * p > n
113
+ cnt = 0
114
+ cnt, n = cnt + 1, n / p while n % p == 0
115
+ res << [p,cnt] if cnt > 0
116
+ end
117
+ res << [n, 1] if n > 1
118
+ return res
119
+ end
120
+
121
+ # 素因数分解(Pollards-Rho)
122
+ def prime_factorization2(n, max_sieve = 65536, rec = true)
123
+ res = []
124
+ Prime.each do |p|
125
+ break if p > max_sieve
126
+ while n % p == 0
127
+ res << p
128
+ n = n / p
129
+ end
130
+ end
131
+ if check_prime(n)
132
+ res << n
133
+ elsif n == 1
134
+ else
135
+ [1,51,73].each do |i|
136
+ x,y,d = 2,2,1
137
+ while d == 1
138
+ x = (x * x + i) % n
139
+ y = (((y * y + i) % n) ** 2 + i) % n
140
+ d = n.gcd((x-y).abs)
141
+ end
142
+ next if d == n
143
+ res += prime_factorization2(d, 0, false) + prime_factorization2(n / d, 0, false)
144
+ break
145
+ end
146
+ end
147
+ res = res.sort.uniq.map{|a| [a, res.count(a)]} if rec
148
+ return res
149
+ end
150
+
151
+ def eulerphi(n, factorized = nil)
152
+ factorized = prime_factorization2(n) unless factorized
153
+ phi = n
154
+ factorized.each do |p,k|
155
+ phi = phi - phi / p
156
+ end
157
+ phi
158
+ end
159
+
160
+ # 元の位数の計算
161
+ def order(g, mod)
162
+ tmp = eulerphi(mod)
163
+ ret = prime_factorization2(tmp)
164
+ order = 1
165
+ ret.each do |p, k|
166
+ order *= p ** (k - (0..k).select{|i|
167
+ mod_pow(g, tmp / p ** i, mod) == 1
168
+ }.max)
169
+ end
170
+ order
171
+ end
172
+
173
+ module_function :sqrtint, :root, :sqrt?
174
+ module_function :mod_pow, :extgcd, :mod_inverse, :chinese_remainder, :discrete_log, :check_prime
175
+ module_function :discrete_log2
176
+ module_function :prime_factorization
177
+ module_function :prime_factorization2
178
+ end
179
+ end
data/lib/ctf/rop.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'metasm'
2
+ module CTF
3
+ module Rop
4
+ class RelocatableELF
5
+ attr :offset
6
+ attr_reader :elf
7
+ def initialize(filename, offset = 0)
8
+ @offset = offset
9
+ @elf = ::Metasm::ELF.decode_file(filename)
10
+ @functions = {}
11
+ @elf.symbols.find_all do |s|
12
+ s.name and s.type == 'FUNC' && s.shndx != 'UNDEF' && s.bind == 'GLOBAL'
13
+ end.each do |s|
14
+ @functions[s.name] = s.value
15
+ end
16
+ end
17
+
18
+ def function(name)
19
+ if @functions.include? name.to_s
20
+ @functions[name.to_s] + offset
21
+ else
22
+ raise RuntimeError.new("No such function #{name}")
23
+ end
24
+ end
25
+ end
26
+
27
+ class ELF < RelocatableELF
28
+ def initialize(filename)
29
+ super filename, 0
30
+ end
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,32 @@
1
+ require 'metasm'
2
+ module CTF
3
+ module Shellcode
4
+ module X86
5
+ def binsh
6
+ shellcode = <<EOS
7
+ xor eax, eax
8
+ push eax
9
+ push #{"n/sh".unpack("I")[0]}
10
+ push #{"//bi".unpack("I")[0]}
11
+ mov ebx, esp
12
+ push eax
13
+ pop ecx
14
+ push eax
15
+ pop edx
16
+ mov al, 0xc
17
+ dec al
18
+ int 0x80
19
+ EOS
20
+ Metasm::Shellcode.assemble(Metasm::Ia32.new, shellcode).encode_string
21
+ end
22
+
23
+ module_function :binsh
24
+ end
25
+
26
+ module Amd64
27
+ def binsh
28
+
29
+ end
30
+ end
31
+ end
32
+ end
data/lib/ctf/socket.rb ADDED
@@ -0,0 +1,92 @@
1
+ require 'socket'
2
+ require 'expect'
3
+ require 'highline'
4
+ require 'stringio'
5
+ require 'io/interactive'
6
+
7
+ module CTF
8
+ module SocketWithEcho
9
+ attr_accessor :echo, :echo_input, :echo_output, :echo_color
10
+ def echo_input
11
+ @echo_input || STDOUT
12
+ end
13
+
14
+ def echo_output
15
+ @echo_output || STDOUT
16
+ end
17
+
18
+ def echo_color
19
+ @echo_color || true
20
+ end
21
+
22
+ def write(str)
23
+ echo_output_print str
24
+ super str
25
+ end
26
+
27
+ def read(length = nil, outbuf = '')
28
+ result = super length, outbuf
29
+ echo_input_print result if result
30
+ result
31
+ end
32
+
33
+ def interactive!(input = STDIN, output = STDOUT)
34
+ @echo = false
35
+ super input, output
36
+ end
37
+
38
+ def gets
39
+ expect("\n")[0]
40
+ end
41
+
42
+ # TODO: Don't ignore timeout
43
+ def expect(pattern, timeout = 9999999)
44
+ result = nil
45
+ loop do
46
+ if pattern.is_a?(Regexp)
47
+ break if result && result.match(pattern)
48
+ else
49
+ break if result && result.end_with?(pattern)
50
+ end
51
+ data = read(1)
52
+ break unless data
53
+ result ||= ''
54
+ result.force_encoding('ASCII-8BIT')
55
+ result << data
56
+ end
57
+ return result unless result
58
+ if pattern.is_a?(Regexp)
59
+ [result] + result.match(pattern).to_a[1..-1]
60
+ else
61
+ [result]
62
+ end
63
+ end
64
+
65
+ private
66
+ def echo_output_print(str)
67
+ if echo
68
+ if echo_color
69
+ echo_output.print HighLine.color(str, :yellow)
70
+ else
71
+ echo_output.print str
72
+ end
73
+ echo_output.flush
74
+ end
75
+ end
76
+
77
+ def echo_input_print(str)
78
+ if echo
79
+ if echo_color
80
+ echo_input.print HighLine.color(str, :green)
81
+ else
82
+ echo_input.print str
83
+ end
84
+ echo_input.flush
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ class BasicSocket
91
+ prepend CTF::SocketWithEcho
92
+ end
data/lib/ctf/utils.rb ADDED
@@ -0,0 +1,20 @@
1
+ module CTF
2
+ module Utils
3
+ module StringWithXor
4
+ def ^(s)
5
+ if s.is_a?(String)
6
+ chars.zip(s[0, length].chars).map{|a,b|
7
+ b ? (a.ord ^ b.ord).chr : a
8
+ }.join
9
+ else
10
+ s = s.to_i
11
+ chars.map{|a|(a.ord^s).chr}.join
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ class String
19
+ include CTF::Utils::StringWithXor
20
+ end
@@ -0,0 +1,3 @@
1
+ module CTF
2
+ VERSION = "0.0.3"
3
+ end
data/lib/ctf.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "ctf/version"
2
+ require 'ctf/socket'
3
+ require 'ctf/utils'
4
+ require 'ctf/math'
5
+ require 'ctf/rop'
6
+ require 'ctf/shellcode'
7
+
8
+ module CTF
9
+ end
data/spec/math_spec.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ require 'ctf'
3
+
4
+ describe CTF::Math do
5
+ describe '#mod_pow' do
6
+ it 'returns correct value' do
7
+ expect(CTF::Math.mod_pow(2, 5, 10)).to eq 2
8
+ end
9
+
10
+ it 'works very big exponent' do
11
+ expect(CTF::Math.mod_pow(3, 2 ** 2048, 81)).to eq 0
12
+ expect(CTF::Math.mod_pow(4, 2 ** 2048, 1000000009)).to eq 409738618
13
+ end
14
+ end
15
+
16
+ describe '#sqrt?' do
17
+ it 'return correct value' do
18
+ expect(CTF::Math.sqrt?(100)).to be_truthy
19
+ expect(CTF::Math.sqrt?(101)).to be_falsey
20
+ expect(CTF::Math.sqrt?(0)).to be_truthy
21
+ expect(CTF::Math.sqrt?(1)).to be_truthy
22
+ expect(CTF::Math.sqrt?(-1)).to be_falsey
23
+ end
24
+ end
25
+
26
+ describe '#sqrtint' do
27
+ it 'returns correct value' do
28
+ expect(CTF::Math.sqrtint(100)).to eq 10
29
+ expect(CTF::Math.sqrtint(101)).to eq 10
30
+ expect(CTF::Math.sqrtint(120)).to eq 10
31
+ expect(CTF::Math.sqrtint(99)).to eq 9
32
+ expect(CTF::Math.sqrtint(81)).to eq 9
33
+ end
34
+ end
35
+ end
File without changes
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nomeaning-ctf
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - nomeaning
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-02 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: io-interactive
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: highline
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: metasm
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.0.2
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.0.2
97
+ description: Utils for CTF
98
+ email:
99
+ - nomeaning@mma.club.uec.ac.jp
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - ctf.gemspec
110
+ - examples/socket-example.rb
111
+ - lib/ctf.rb
112
+ - lib/ctf/math.rb
113
+ - lib/ctf/rop.rb
114
+ - lib/ctf/shellcode.rb
115
+ - lib/ctf/socket.rb
116
+ - lib/ctf/utils.rb
117
+ - lib/ctf/version.rb
118
+ - spec/math_spec.rb
119
+ - spec/spec_helper.rb
120
+ homepage: https://bitbucket.org/nomeaning777/ctf
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.5.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Utils for CTF
144
+ test_files:
145
+ - spec/math_spec.rb
146
+ - spec/spec_helper.rb