-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathheXDump.rb
100 lines (85 loc) · 1.67 KB
/
heXDump.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env ruby
# encoding: ascii-8bit
# frozen_string_literal: true
require 'English'
require 'fileutils'
require 'securerandom'
FLAG_PATH = File.join(ENV['HOME'], 'flag')
DEFAULT_MODE = "sha1sum %s | awk '{ print $1 }'"
def setup
STDOUT.sync = 0
STDIN.sync = 0
@mode = DEFAULT_MODE
@file = '/tmp/' + SecureRandom.hex
FileUtils.touch(@file)
@key = output("sha256sum #{FLAG_PATH} | awk '{ print $1 }'").strip
raise if @key.size != 32 * 2
end
def menu
<<~MENU
1) write
2) read
3) change output mode
0) quit
MENU
end
def output(cmd)
IO.popen(cmd, &:gets)
end
def write
puts 'Data? (In hex format)'
data = gets
return false unless data && !data.empty? && data.size < 0x1000
IO.popen("xxd -r -ps - #{@file}", 'r+') do |f|
f.puts data
f.close_write
end
return false unless $CHILD_STATUS.success?
true
end
def read
unless File.exist?(@file)
puts 'Write something first plz.'
return true
end
puts output(format(@mode, @file))
true
end
def mode_menu
<<~MODE
Which mode?
- SHA1
- MD5
- AES
MODE
end
def change_mode
puts mode_menu
@mode = case gets.strip.downcase
when 'sha1' then "sha1sum %s | awk '{ print $1 }'"
when 'md5' then "md5sum %s | awk '{ print $1 }'"
when 'aes' then "openssl enc -aes-256-ecb -in %s -K #{@key} | xxd -ps"
else DEFAULT_MODE
end
end
def secret
FileUtils.cp(FLAG_PATH, @file)
true
end
def main_loop
puts menu
case gets.to_i
when 1 then write
when 2 then read
when 3 then change_mode
when 1337 then secret
else false
end
end
setup
begin
loop while main_loop
puts 'See ya!'
ensure
FileUtils.rm_f(@file)
end