easy_pow 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
+ SHA1:
3
+ metadata.gz: d04027026e757693648d73bcb5cb6f2afbee99fe
4
+ data.tar.gz: 276d10200683279b0a4e5300b0fc38e3d9cc7365
5
+ SHA512:
6
+ metadata.gz: 8803663673acadacddad3153773306232928db56dd1058c9d2b77da5fc380089c5326e27c69a5a201301220b480d521927dd792543ce68bd1d014b3a17672580
7
+ data.tar.gz: ffaeab32708fefa98404c99e084fb6226d81fcbe7322f181384b87f77dd5812a447b78c2f7adf25b1caea256360ea6d0552045da96958c544bd465c5a093ac21
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy_pow.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 nomeaning
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,60 @@
1
+ # EasyPow
2
+
3
+ Simple PoW System/Solver for CTFs.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'easy_pow'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install easy_pow
20
+
21
+ ## Usage
22
+
23
+ ### Client
24
+
25
+ ```ruby
26
+ require 'easy_pow'
27
+ TCPSocket.open('127.0.0.1', '1234') do |s|
28
+ s.puts EasyPow.solve(s.gets)
29
+ end
30
+ ```
31
+
32
+ ### Server
33
+
34
+ ```ruby
35
+ require 'easy_pow'
36
+
37
+ # use STDIN / STDOUT
38
+ exit 0 unless EasyPow.easy_pow(27) # 27bit SHA256 PoW
39
+
40
+ # use TCPServer
41
+ TCPServer.open('0.0.0.0', 1234) do |socket|
42
+ # ...
43
+ exit unless EasyPow.easy_pow(25, socket) # 25bit SHA256 PoW with Socket
44
+ end
45
+ ```
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nomeaning777/easy_pow.
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
60
+
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 "easy_pow"
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/easy_pow.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easy_pow/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "easy_pow"
8
+ spec.version = EasyPow::VERSION
9
+ spec.authors = ["nomeaning"]
10
+ spec.email = ["nomeaning777@gmail.com"]
11
+
12
+ spec.summary = %q{Simple PoW for CTF}
13
+ spec.description = %q{Simple PoW for CTF }
14
+ spec.homepage = "http://github.com/nomeaning777/easy_pow"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib", "ext"]
32
+ spec.extensions << "ext/easy_pow/extconf.rb"
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.14"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ end
@@ -0,0 +1,263 @@
1
+
2
+ SHELL = /bin/sh
3
+
4
+ # V=0 quiet, V=1 verbose. other values don't work.
5
+ V = 0
6
+ Q1 = $(V:1=)
7
+ Q = $(Q1:0=@)
8
+ ECHO1 = $(V:1=@ :)
9
+ ECHO = $(ECHO1:0=@ echo)
10
+ NULLCMD = :
11
+
12
+ #### Start of system configuration section. ####
13
+
14
+ srcdir = .
15
+ topdir = /home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0
16
+ hdrdir = $(topdir)
17
+ arch_hdrdir = /home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0/x86_64-linux
18
+ PATH_SEPARATOR = :
19
+ VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby
20
+ prefix = $(DESTDIR)/home/nomeaning/.rbenv/versions/2.4.0
21
+ rubysitearchprefix = $(rubylibprefix)/$(sitearch)
22
+ rubyarchprefix = $(rubylibprefix)/$(arch)
23
+ rubylibprefix = $(libdir)/$(RUBY_BASE_NAME)
24
+ exec_prefix = $(prefix)
25
+ vendorarchhdrdir = $(vendorhdrdir)/$(sitearch)
26
+ sitearchhdrdir = $(sitehdrdir)/$(sitearch)
27
+ rubyarchhdrdir = $(rubyhdrdir)/$(arch)
28
+ vendorhdrdir = $(rubyhdrdir)/vendor_ruby
29
+ sitehdrdir = $(rubyhdrdir)/site_ruby
30
+ rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME)
31
+ vendorarchdir = $(vendorlibdir)/$(sitearch)
32
+ vendorlibdir = $(vendordir)/$(ruby_version)
33
+ vendordir = $(rubylibprefix)/vendor_ruby
34
+ sitearchdir = $(sitelibdir)/$(sitearch)
35
+ sitelibdir = $(sitedir)/$(ruby_version)
36
+ sitedir = $(rubylibprefix)/site_ruby
37
+ rubyarchdir = $(rubylibdir)/$(arch)
38
+ rubylibdir = $(rubylibprefix)/$(ruby_version)
39
+ sitearchincludedir = $(includedir)/$(sitearch)
40
+ archincludedir = $(includedir)/$(arch)
41
+ sitearchlibdir = $(libdir)/$(sitearch)
42
+ archlibdir = $(libdir)/$(arch)
43
+ ridir = $(datarootdir)/$(RI_BASE_NAME)
44
+ mandir = $(datarootdir)/man
45
+ localedir = $(datarootdir)/locale
46
+ libdir = $(exec_prefix)/lib
47
+ psdir = $(docdir)
48
+ pdfdir = $(docdir)
49
+ dvidir = $(docdir)
50
+ htmldir = $(docdir)
51
+ infodir = $(datarootdir)/info
52
+ docdir = $(datarootdir)/doc/$(PACKAGE)
53
+ oldincludedir = $(DESTDIR)/usr/include
54
+ includedir = $(prefix)/include
55
+ localstatedir = $(prefix)/var
56
+ sharedstatedir = $(prefix)/com
57
+ sysconfdir = $(prefix)/etc
58
+ datadir = $(datarootdir)
59
+ datarootdir = $(prefix)/share
60
+ libexecdir = $(exec_prefix)/libexec
61
+ sbindir = $(exec_prefix)/sbin
62
+ bindir = $(exec_prefix)/bin
63
+ archdir = $(rubyarchdir)
64
+
65
+
66
+ CC = gcc
67
+ CXX = g++
68
+ LIBRUBY = $(LIBRUBY_A)
69
+ LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a
70
+ LIBRUBYARG_SHARED = -Wl,-R$(libdir) -L$(libdir)
71
+ LIBRUBYARG_STATIC = -Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static
72
+ empty =
73
+ OUTFLAG = -o $(empty)
74
+ COUTFLAG = -o $(empty)
75
+ CSRCFLAG = $(empty)
76
+
77
+ RUBY_EXTCONF_H =
78
+ cflags = $(optflags) $(debugflags) $(warnflags)
79
+ cxxflags = $(optflags) $(debugflags) $(warnflags)
80
+ optflags = -O3 -fno-fast-math
81
+ debugflags = -ggdb3
82
+ warnflags = -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wno-tautological-compare -Wno-parentheses-equality -Wno-constant-logical-operand -Wno-self-assign -Wunused-variable -Wimplicit-int -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wsuggest-attribute=noreturn -Wsuggest-attribute=format
83
+ CCDLFLAGS = -fPIC
84
+ CFLAGS = $(CCDLFLAGS) $(cflags) -O3 -std=c11 -march=native -fopenmp $(ARCH_FLAG)
85
+ INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir)/ruby/backward -I$(hdrdir) -I$(srcdir)
86
+ DEFS =
87
+ CPPFLAGS = -DHAVE_OPENSSL_SHA_H -I/home/nomeaning/.rbenv/versions/2.4.0/include $(DEFS) $(cppflags)
88
+ CXXFLAGS = $(CCDLFLAGS) $(cxxflags) $(ARCH_FLAG)
89
+ ldflags = -L. -L/home/nomeaning/.rbenv/versions/2.4.0/lib -fstack-protector -rdynamic -Wl,-export-dynamic
90
+ dldflags = -L/home/nomeaning/.rbenv/versions/2.4.0/lib
91
+ ARCH_FLAG =
92
+ DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG)
93
+ LDSHARED = $(CC) -shared
94
+ LDSHAREDXX = $(CXX) -shared
95
+ AR = ar
96
+ EXEEXT =
97
+
98
+ RUBY_INSTALL_NAME = $(RUBY_BASE_NAME)
99
+ RUBY_SO_NAME = ruby
100
+ RUBYW_INSTALL_NAME =
101
+ RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version)
102
+ RUBYW_BASE_NAME = rubyw
103
+ RUBY_BASE_NAME = ruby
104
+
105
+ arch = x86_64-linux
106
+ sitearch = $(arch)
107
+ ruby_version = 2.4.0
108
+ ruby = $(bindir)/$(RUBY_BASE_NAME)
109
+ RUBY = $(ruby)
110
+ ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/backward.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h
111
+
112
+ RM = rm -f
113
+ RM_RF = $(RUBY) -run -e rm -- -rf
114
+ RMDIRS = rmdir --ignore-fail-on-non-empty -p
115
+ MAKEDIRS = /bin/mkdir -p
116
+ INSTALL = /usr/bin/install -c
117
+ INSTALL_PROG = $(INSTALL) -m 0755
118
+ INSTALL_DATA = $(INSTALL) -m 644
119
+ COPY = cp
120
+ TOUCH = exit >
121
+
122
+ #### End of system configuration section. ####
123
+
124
+ preload =
125
+ libpath = . $(libdir)
126
+ LIBPATH = -L. -L$(libdir) -Wl,-R$(libdir)
127
+ DEFFILE =
128
+
129
+ CLEANFILES = mkmf.log
130
+ DISTCLEANFILES =
131
+ DISTCLEANDIRS =
132
+
133
+ extout =
134
+ extout_prefix =
135
+ target_prefix = /easy_pow
136
+ LOCAL_LIBS =
137
+ LIBS = -lgomp -lcrypto -lstdc++ -lpthread -lgmp -ldl -lcrypt -lm -lc
138
+ ORIG_SRCS = ext.c
139
+ SRCS = $(ORIG_SRCS)
140
+ OBJS = ext.o
141
+ HDRS =
142
+ LOCAL_HDRS =
143
+ TARGET = ext
144
+ TARGET_NAME = ext
145
+ TARGET_ENTRY = Init_$(TARGET_NAME)
146
+ DLLIB = $(TARGET).so
147
+ EXTSTATIC =
148
+ STATIC_LIB =
149
+
150
+ TIMESTAMP_DIR = .
151
+ BINDIR = $(bindir)
152
+ RUBYCOMMONDIR = $(sitedir)$(target_prefix)
153
+ RUBYLIBDIR = $(sitelibdir)$(target_prefix)
154
+ RUBYARCHDIR = $(sitearchdir)$(target_prefix)
155
+ HDRDIR = $(rubyhdrdir)/ruby$(target_prefix)
156
+ ARCHHDRDIR = $(rubyhdrdir)/$(arch)/ruby$(target_prefix)
157
+ TARGET_SO_DIR =
158
+ TARGET_SO = $(TARGET_SO_DIR)$(DLLIB)
159
+ CLEANLIBS = $(TARGET_SO)
160
+ CLEANOBJS = *.o *.bak
161
+
162
+ all: $(DLLIB)
163
+ static: $(STATIC_LIB) install-rb
164
+ .PHONY: all install static install-so install-rb
165
+ .PHONY: clean clean-so clean-static clean-rb
166
+
167
+ clean-static::
168
+ clean-rb-default::
169
+ clean-rb::
170
+ clean-so::
171
+ clean: clean-so clean-static clean-rb-default clean-rb
172
+ -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time
173
+
174
+ distclean-rb-default::
175
+ distclean-rb::
176
+ distclean-so::
177
+ distclean-static::
178
+ distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb
179
+ -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log
180
+ -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES)
181
+ -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true
182
+
183
+ realclean: distclean
184
+ install: install-so install-rb
185
+
186
+ install-so: $(DLLIB) $(TIMESTAMP_DIR)/.sitearchdir.-.easy_pow.time
187
+ $(INSTALL_PROG) $(DLLIB) $(RUBYARCHDIR)
188
+ clean-static::
189
+ -$(Q)$(RM) $(STATIC_LIB)
190
+ install-rb: pre-install-rb do-install-rb install-rb-default
191
+ install-rb-default: pre-install-rb-default do-install-rb-default
192
+ pre-install-rb: Makefile
193
+ pre-install-rb-default: Makefile
194
+ do-install-rb:
195
+ do-install-rb-default:
196
+ pre-install-rb-default:
197
+ @$(NULLCMD)
198
+ $(TIMESTAMP_DIR)/.sitearchdir.-.easy_pow.time:
199
+ $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR)
200
+ $(Q) $(TOUCH) $@
201
+
202
+ site-install: site-install-so site-install-rb
203
+ site-install-so: install-so
204
+ site-install-rb: install-rb
205
+
206
+ .SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S
207
+
208
+ .cc.o:
209
+ $(ECHO) compiling $(<)
210
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
211
+
212
+ .cc.S:
213
+ $(ECHO) translating $(<)
214
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
215
+
216
+ .mm.o:
217
+ $(ECHO) compiling $(<)
218
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
219
+
220
+ .mm.S:
221
+ $(ECHO) translating $(<)
222
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
223
+
224
+ .cxx.o:
225
+ $(ECHO) compiling $(<)
226
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
227
+
228
+ .cxx.S:
229
+ $(ECHO) translating $(<)
230
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
231
+
232
+ .cpp.o:
233
+ $(ECHO) compiling $(<)
234
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
235
+
236
+ .cpp.S:
237
+ $(ECHO) translating $(<)
238
+ $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
239
+
240
+ .c.o:
241
+ $(ECHO) compiling $(<)
242
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
243
+
244
+ .c.S:
245
+ $(ECHO) translating $(<)
246
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
247
+
248
+ .m.o:
249
+ $(ECHO) compiling $(<)
250
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $(CSRCFLAG)$<
251
+
252
+ .m.S:
253
+ $(ECHO) translating $(<)
254
+ $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $(CSRCFLAG)$<
255
+
256
+ $(TARGET_SO): $(OBJS) Makefile
257
+ $(ECHO) linking shared-object easy_pow/$(DLLIB)
258
+ -$(Q)$(RM) $(@)
259
+ $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS)
260
+
261
+
262
+
263
+ $(OBJS): $(HDRS) $(ruby_headers)
@@ -0,0 +1,173 @@
1
+ #include <openssl/sha.h>
2
+ #include <openssl/md5.h>
3
+ #include "ruby.h"
4
+ #include <stdint.h>
5
+ #include <assert.h>
6
+ #include <stdlib.h>
7
+ #include <mm_malloc.h>
8
+
9
+ struct search_condition {
10
+ unsigned char (*hash)(const unsigned char *d, size_t n, unsigned char *md);
11
+ size_t md_len;
12
+ unsigned char * restrict mask;
13
+ unsigned char *target;
14
+ unsigned char *result;
15
+ size_t chars_len;
16
+ unsigned char *chars;
17
+ size_t search_length;
18
+ size_t length;
19
+ };
20
+
21
+ unsigned char * safe_malloc(size_t length) {
22
+ unsigned char *data = _mm_malloc(length + 15, 16);
23
+ if(data == 0) {
24
+ rb_raise(rb_eNoMemError, "Failed to allocate memory");
25
+ }
26
+ return data;
27
+ }
28
+
29
+ void search(unsigned char *data, size_t pos, unsigned char * restrict md, struct search_condition *cond) {
30
+ if(cond->result != NULL) return; // Found Already
31
+ if(pos == cond->search_length) {
32
+ int64_t *mdc = __builtin_assume_aligned(md, 16);
33
+ int64_t *mask = __builtin_assume_aligned(cond->mask, 16);
34
+ // ハッシュ関数
35
+ (cond->hash)(data, cond->length, md);
36
+
37
+ for(size_t i = 0; i < cond->md_len / 8; i++) {
38
+ mdc[i] &= mask[i];
39
+ }
40
+
41
+ if(memcmp(md, cond->target, cond->md_len) == 0) {
42
+ #pragma omp critical
43
+ {
44
+ if(cond->result == NULL) {
45
+ cond->result = malloc(cond->length);
46
+ if(cond->result == 0) {
47
+ rb_raise(rb_eNoMemError, "Failed to allocate memory");
48
+ }
49
+ memcpy(cond->result, data, cond->length);
50
+ }
51
+ }
52
+ }
53
+ } else {
54
+ for(size_t i = 0; i < cond->chars_len; i++) {
55
+ data[pos] = cond->chars[i];
56
+ search(data, pos + 1, md, cond);
57
+ }
58
+ }
59
+ }
60
+
61
+ void search_parallel(unsigned char *data, size_t pos, unsigned char * restrict md, struct search_condition *cond) {
62
+ #pragma omp parallel for
63
+ for(size_t i = 0; i < cond->chars_len; i++) {
64
+ unsigned char *data2 = safe_malloc(cond->length);
65
+ unsigned char *md2 = safe_malloc(cond->md_len);
66
+ memcpy(data2, data, cond->length);
67
+ data2[pos] = cond->chars[i];
68
+ search(data2, pos + 1, md2, cond);
69
+ _mm_free(data2);
70
+ _mm_free(md2);
71
+ }
72
+ }
73
+
74
+
75
+ static VALUE search_general(VALUE self, VALUE prefix, VALUE suffix, VALUE length, VALUE target, VALUE mask, VALUE chars, VALUE paralell, struct search_condition *cond) {
76
+ Check_Type(prefix, T_STRING);
77
+ Check_Type(suffix, T_STRING);
78
+ FIXNUM_P(length);
79
+ Check_Type(target, T_STRING);
80
+ Check_Type(mask, T_STRING);
81
+ Check_Type(chars, T_STRING);
82
+ if((size_t)RSTRING_LEN(mask) != cond->md_len)
83
+ rb_raise(rb_eArgError, "Invalid Mask length");
84
+ if((size_t)RSTRING_LEN(target) != cond->md_len)
85
+ rb_raise(rb_eArgError, "Invalid Target length");
86
+ cond->mask = safe_malloc(cond->md_len);
87
+ memcpy(cond->mask, (unsigned char*)RSTRING_PTR(mask), cond->md_len);
88
+ cond->target = (unsigned char*)RSTRING_PTR(target);
89
+ cond->result = NULL;
90
+ cond->chars = (unsigned char*)RSTRING_PTR(chars);
91
+ cond->chars_len = RSTRING_LEN(chars);
92
+ cond->search_length = FIX2INT(length) + RSTRING_LEN(prefix);
93
+ cond->length = cond->search_length + RSTRING_LEN(suffix);
94
+ #ifdef _OPENMP
95
+ if(RTEST(paralell) && length > 0) { // openmp
96
+ unsigned char *data = safe_malloc(cond->length);
97
+ unsigned char *md = safe_malloc(cond->md_len);
98
+ memcpy(data, RSTRING_PTR(prefix), RSTRING_LEN(prefix));
99
+ memcpy(data + cond->search_length, RSTRING_PTR(suffix), RSTRING_LEN(suffix));
100
+ search_parallel(data, RSTRING_LEN(prefix), md, cond);
101
+ _mm_free(data);
102
+ _mm_free(md);
103
+ } else {
104
+ #endif
105
+ unsigned char *data = safe_malloc(cond->length);
106
+ unsigned char *md = safe_malloc(cond->md_len);
107
+ memcpy(data, RSTRING_PTR(prefix), RSTRING_LEN(prefix));
108
+ memcpy(data + cond->search_length, RSTRING_PTR(suffix), RSTRING_LEN(suffix));
109
+ search(data, RSTRING_LEN(prefix), md, cond);
110
+ _mm_free(data);
111
+ _mm_free(md);
112
+ #ifdef _OPENMP
113
+ }
114
+ #endif
115
+ _mm_free(cond->mask);
116
+ if(cond->result) {
117
+ return rb_str_new((char*)cond->result, cond->length);
118
+ } else {
119
+ return Qnil;
120
+ }
121
+ }
122
+
123
+ static VALUE search_md5(VALUE self, VALUE prefix, VALUE suffix, VALUE length, VALUE target, VALUE mask, VALUE chars, VALUE paralell) {
124
+ struct search_condition cond;
125
+ cond.hash = MD5;
126
+ cond.md_len = 128 / 8;
127
+ return search_general(self, prefix, suffix, length, target, mask, chars, paralell, &cond);
128
+ }
129
+
130
+ static VALUE search_sha1(VALUE self, VALUE prefix, VALUE suffix, VALUE length, VALUE target, VALUE mask, VALUE chars, VALUE paralell) {
131
+ struct search_condition cond;
132
+ cond.hash = SHA1;
133
+ cond.md_len = 160 / 8;
134
+ return search_general(self, prefix, suffix, length, target, mask, chars, paralell, &cond);
135
+ }
136
+
137
+ static VALUE search_sha224(VALUE self, VALUE prefix, VALUE suffix, VALUE length, VALUE target, VALUE mask, VALUE chars, VALUE paralell) {
138
+ struct search_condition cond;
139
+ cond.hash = SHA224;
140
+ cond.md_len = 224 / 8;
141
+ return search_general(self, prefix, suffix, length, target, mask, chars, paralell, &cond);
142
+ }
143
+
144
+ static VALUE search_sha256(VALUE self, VALUE prefix, VALUE suffix, VALUE length, VALUE target, VALUE mask, VALUE chars, VALUE paralell) {
145
+ struct search_condition cond;
146
+ cond.hash = SHA256;
147
+ cond.md_len = 256 / 8;
148
+ return search_general(self, prefix, suffix, length, target, mask, chars, paralell, &cond);
149
+ }
150
+
151
+ static VALUE search_sha384(VALUE self, VALUE prefix, VALUE suffix, VALUE length, VALUE target, VALUE mask, VALUE chars, VALUE paralell) {
152
+ struct search_condition cond;
153
+ cond.hash = SHA384;
154
+ cond.md_len = 384 / 8;
155
+ return search_general(self, prefix, suffix, length, target, mask, chars, paralell, &cond);
156
+ }
157
+
158
+ static VALUE search_sha512(VALUE self, VALUE prefix, VALUE suffix, VALUE length, VALUE target, VALUE mask, VALUE chars, VALUE paralell) {
159
+ struct search_condition cond;
160
+ cond.hash = SHA512;
161
+ cond.md_len = 512 / 8;
162
+ return search_general(self, prefix, suffix, length, target, mask, chars, paralell, &cond);
163
+ }
164
+ void Init_ext()
165
+ {
166
+ VALUE module = rb_define_module("EasyPow");
167
+ rb_define_module_function(module, "search_md5_ext", search_md5, 7);
168
+ rb_define_module_function(module, "search_sh1_ext", search_sha1, 7);
169
+ rb_define_module_function(module, "search_sha224_ext", search_sha224, 7);
170
+ rb_define_module_function(module, "search_sha256_ext", search_sha256, 7);
171
+ rb_define_module_function(module, "search_sha384_ext", search_sha384, 7);
172
+ rb_define_module_function(module, "search_sha512_ext", search_sha512, 7);
173
+ }
Binary file
Binary file
@@ -0,0 +1,8 @@
1
+ require 'mkmf'
2
+ $CFLAGS += " -O3 -std=c11 -march=native -fopenmp"
3
+
4
+ have_library("stdc++")
5
+ have_header('openssl/sha.h')
6
+ have_library('crypto')
7
+ have_library('gomp')
8
+ create_makefile("easy_pow/ext")
@@ -0,0 +1,92 @@
1
+ have_library: checking for -lstdc++... -------------------- yes
2
+
3
+ "gcc -o conftest -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0/x86_64-linux -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0/ruby/backward -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0 -I. -I/home/nomeaning/.rbenv/versions/2.4.0/include -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wno-tautological-compare -Wno-parentheses-equality -Wno-constant-logical-operand -Wno-self-assign -Wunused-variable -Wimplicit-int -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wsuggest-attribute=noreturn -Wsuggest-attribute=format -O3 -std=c11 -march=native -fopenmp conftest.c -L. -L/home/nomeaning/.rbenv/versions/2.4.0/lib -Wl,-R/home/nomeaning/.rbenv/versions/2.4.0/lib -L. -L/home/nomeaning/.rbenv/versions/2.4.0/lib -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-R/home/nomeaning/.rbenv/versions/2.4.0/lib -L/home/nomeaning/.rbenv/versions/2.4.0/lib -lruby-static -lpthread -lgmp -ldl -lcrypt -lm -lc"
4
+ checked program was:
5
+ /* begin */
6
+ 1: #include "ruby.h"
7
+ 2:
8
+ 3: int main(int argc, char **argv)
9
+ 4: {
10
+ 5: return 0;
11
+ 6: }
12
+ /* end */
13
+
14
+ "gcc -o conftest -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0/x86_64-linux -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0/ruby/backward -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0 -I. -I/home/nomeaning/.rbenv/versions/2.4.0/include -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wno-tautological-compare -Wno-parentheses-equality -Wno-constant-logical-operand -Wno-self-assign -Wunused-variable -Wimplicit-int -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wsuggest-attribute=noreturn -Wsuggest-attribute=format -O3 -std=c11 -march=native -fopenmp conftest.c -L. -L/home/nomeaning/.rbenv/versions/2.4.0/lib -Wl,-R/home/nomeaning/.rbenv/versions/2.4.0/lib -L. -L/home/nomeaning/.rbenv/versions/2.4.0/lib -fstack-protector -rdynamic -Wl,-export-dynamic -Wl,-R/home/nomeaning/.rbenv/versions/2.4.0/lib -L/home/nomeaning/.rbenv/versions/2.4.0/lib -lruby-static -lstdc++ -lpthread -lgmp -ldl -lcrypt -lm -lc"
15
+ checked program was:
16
+ /* begin */
17
+ 1: #include "ruby.h"
18
+ 2:
19
+ 3: /*top*/
20
+ 4: extern int t(void);
21
+ 5: int main(int argc, char **argv)
22
+ 6: {
23
+ 7: if (argc > 1000000) {
24
+ 8: printf("%p", &t);
25
+ 9: }
26
+ 10:
27
+ 11: return 0;
28
+ 12: }
29
+ 13:
30
+ 14: int t(void) { ; return 0; }
31
+ /* end */
32
+
33
+ --------------------
34
+
35
+ have_header: checking for openssl/sha.h... -------------------- yes
36
+
37
+ "gcc -E -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0/x86_64-linux -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0/ruby/backward -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0 -I. -I/home/nomeaning/.rbenv/versions/2.4.0/include -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wno-tautological-compare -Wno-parentheses-equality -Wno-constant-logical-operand -Wno-self-assign -Wunused-variable -Wimplicit-int -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wsuggest-attribute=noreturn -Wsuggest-attribute=format -O3 -std=c11 -march=native -fopenmp conftest.c -o conftest.i"
38
+ checked program was:
39
+ /* begin */
40
+ 1: #include "ruby.h"
41
+ 2:
42
+ 3: #include <openssl/sha.h>
43
+ /* end */
44
+
45
+ --------------------
46
+
47
+ have_library: checking for -lcrypto... -------------------- yes
48
+
49
+ "gcc -o conftest -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0/x86_64-linux -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0/ruby/backward -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0 -I. -I/home/nomeaning/.rbenv/versions/2.4.0/include -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wno-tautological-compare -Wno-parentheses-equality -Wno-constant-logical-operand -Wno-self-assign -Wunused-variable -Wimplicit-int -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wsuggest-attribute=noreturn -Wsuggest-attribute=format -O3 -std=c11 -march=native -fopenmp conftest.c -L. -L/home/nomeaning/.rbenv/versions/2.4.0/lib -Wl,-R/home/nomeaning/.rbenv/versions/2.4.0/lib -L. -L/home/nomeaning/.rbenv/versions/2.4.0/lib -fstack-protector -rdynamic -Wl,-export-dynamic -lstdc++ -Wl,-R/home/nomeaning/.rbenv/versions/2.4.0/lib -L/home/nomeaning/.rbenv/versions/2.4.0/lib -lruby-static -lcrypto -lstdc++ -lpthread -lgmp -ldl -lcrypt -lm -lc"
50
+ checked program was:
51
+ /* begin */
52
+ 1: #include "ruby.h"
53
+ 2:
54
+ 3: /*top*/
55
+ 4: extern int t(void);
56
+ 5: int main(int argc, char **argv)
57
+ 6: {
58
+ 7: if (argc > 1000000) {
59
+ 8: printf("%p", &t);
60
+ 9: }
61
+ 10:
62
+ 11: return 0;
63
+ 12: }
64
+ 13:
65
+ 14: int t(void) { ; return 0; }
66
+ /* end */
67
+
68
+ --------------------
69
+
70
+ have_library: checking for -lgomp... -------------------- yes
71
+
72
+ "gcc -o conftest -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0/x86_64-linux -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0/ruby/backward -I/home/nomeaning/.rbenv/versions/2.4.0/include/ruby-2.4.0 -I. -I/home/nomeaning/.rbenv/versions/2.4.0/include -O3 -fno-fast-math -ggdb3 -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wno-tautological-compare -Wno-parentheses-equality -Wno-constant-logical-operand -Wno-self-assign -Wunused-variable -Wimplicit-int -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat -Wsuggest-attribute=noreturn -Wsuggest-attribute=format -O3 -std=c11 -march=native -fopenmp conftest.c -L. -L/home/nomeaning/.rbenv/versions/2.4.0/lib -Wl,-R/home/nomeaning/.rbenv/versions/2.4.0/lib -L. -L/home/nomeaning/.rbenv/versions/2.4.0/lib -fstack-protector -rdynamic -Wl,-export-dynamic -lcrypto -lstdc++ -Wl,-R/home/nomeaning/.rbenv/versions/2.4.0/lib -L/home/nomeaning/.rbenv/versions/2.4.0/lib -lruby-static -lgomp -lcrypto -lstdc++ -lpthread -lgmp -ldl -lcrypt -lm -lc"
73
+ checked program was:
74
+ /* begin */
75
+ 1: #include "ruby.h"
76
+ 2:
77
+ 3: /*top*/
78
+ 4: extern int t(void);
79
+ 5: int main(int argc, char **argv)
80
+ 6: {
81
+ 7: if (argc > 1000000) {
82
+ 8: printf("%p", &t);
83
+ 9: }
84
+ 10:
85
+ 11: return 0;
86
+ 12: }
87
+ 13:
88
+ 14: int t(void) { ; return 0; }
89
+ /* end */
90
+
91
+ --------------------
92
+
@@ -0,0 +1,3 @@
1
+ module EasyPow
2
+ VERSION = "0.1.0"
3
+ end
data/lib/easy_pow.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'easy_pow/version'
2
+ require 'easy_pow/ext'
3
+ require 'securerandom'
4
+ require 'digest/sha2'
5
+
6
+ module EasyPow
7
+ @@hashes = [
8
+ ['sha1', 160],
9
+ ['md5', 128],
10
+ ['sha224', 224],
11
+ ['sha256', 256],
12
+ ['sha384', 384],
13
+ ['sha512', 512]
14
+ ]
15
+
16
+ def search_prefix(hash, bin, length, prefix, suffix = '', chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', parallel = true)
17
+ len = @@hashes.select{|a|a[0] == hash.to_s.downcase}[0]
18
+ raise ArgumentError.new('Unknown hash type: %s' % [hash.to_s.downcase]) unless len
19
+ len = len[1] / 8
20
+ raise ArgumentError.new('Too long prefix') if bin.size > len * 8
21
+ target = Array.new(len){0}
22
+ mask = Array.new(len){0}
23
+ bin.each_char.with_index do |b, i|
24
+ mask[i / 8] |= 1 << (7 - i % 8)
25
+ target[i / 8] |= b.to_i << (7 - i % 8)
26
+ end
27
+ send 'search_' + hash.to_s.downcase + '_ext', prefix, suffix, length, target.pack("C*"), mask.pack("C*"), chars, parallel
28
+ end
29
+
30
+ def search_suffix(hash, bin, length, prefix, suffix = '', chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', parallel = true)
31
+ len = @@hashes.select{|a|a[0] == hash.to_s.downcase}[0]
32
+ raise ArgumentError.new('Unknown hash type: %s' % [hash.to_s.downcase]) unless len
33
+ len = len[1] / 8
34
+ raise ArgumentError.new('Too long prefix') if bin.size > len * 8
35
+ target = Array.new(len){0}
36
+ mask = Array.new(len){0}
37
+ bin.each_char.with_index do |b, i|
38
+ mask[-(i / 8) - 1] |= 1 << (i % 8)
39
+ target[-(i / 8) - 1] |= b.to_i << (i % 8)
40
+ end
41
+ send 'search_' + hash.to_s.downcase + '_ext', prefix, suffix, length, target.pack("C*"), mask.pack("C*"), chars, parallel
42
+ end
43
+
44
+ def easy_pow(bits, socket = nil)
45
+ in_s = out_s = socket
46
+ if socket == nil
47
+ in_s = STDIN
48
+ out_s = STDOUT
49
+ end
50
+ prefix = SecureRandom.hex(8)
51
+ out_s.print "Send me proof-of-work: The first #{bits}-bits of sha256(\"#{prefix}\" + input.rstrip) is \"111...1\"\n"
52
+ out_s.flush
53
+ input = in_s.gets.strip.force_encoding('ASCII-8BIT')
54
+ digest = Digest::SHA256.digest(prefix + input.rstrip).unpack("C*")
55
+ bits.times do |i|
56
+ if (digest[i / 8] >> (7 - i % 8) & 1) == 0
57
+ return false
58
+ end
59
+ end
60
+ return true
61
+ end
62
+
63
+ def solve(input)
64
+ if /The first (\d+)-bits of sha256\("(.{16})"/ =~ input
65
+ search_prefix('sha256', '1' * $1.to_i, 12, $2)[16..-1]
66
+ else
67
+ raise ArgumentError.new("Invalid format")
68
+ end
69
+ end
70
+
71
+ module_function :search_prefix, :search_suffix
72
+ module_function :easy_pow, :solve
73
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_pow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nomeaning
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-25 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.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
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: 'Simple PoW for CTF '
42
+ email:
43
+ - nomeaning777@gmail.com
44
+ executables: []
45
+ extensions:
46
+ - ext/easy_pow/extconf.rb
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - easy_pow.gemspec
57
+ - ext/easy_pow/Makefile
58
+ - ext/easy_pow/ext.c
59
+ - ext/easy_pow/ext.o
60
+ - ext/easy_pow/ext.so
61
+ - ext/easy_pow/extconf.rb
62
+ - ext/easy_pow/mkmf.log
63
+ - lib/easy_pow.rb
64
+ - lib/easy_pow/version.rb
65
+ homepage: http://github.com/nomeaning777/easy_pow
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ - ext
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.6.8
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Simple PoW for CTF
90
+ test_files: []