Skip to content

Commit

Permalink
Do some js parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
imustafin committed Oct 31, 2024
1 parent c9de166 commit be98afd
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
57 changes: 56 additions & 1 deletion app/lib/vk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,64 @@ def self.retry_handle_429(response, desired_uri)
end

def self.parse_salt(s)
if data = /'(.*)'/.match(s)
if data = /^'(.*)'$/.match(s)
return data.captures[0]
end

return unless s.starts_with?('(function() {') && s.ends_with?(';})();')
s = s.delete_prefix('(function() {')
s = s.delete_suffix(';})();')

codes = s.delete_prefix('var codes = [[').split(']];').first.split('],[')
codes = codes.map do |carr|
carr
.split(/\(function\(e?\) ?{/)
.reject(&:blank?)
.map { |s| s.delete_suffix(',').delete_suffix(';})') }
.reverse
end


nums = codes.map do |cod|
cod
.reduce(0) { |code, f| js_func(f, code) }
.chr
end

nums.join
end

def self.js_func(s, arg = nil)
if s.start_with?('return')
s = s.delete_prefix('return ')
if s.match?(/^-?\d+$/)
return s.to_i
end
matches = /.* (.) (.*)/.match(s)
r = matches[2].to_i
op = matches[1]
if op == '-'
return arg.to_i - r
elsif op == '+'
return arg.to_i + r
elsif op == '^'
if arg
return arg.to_i ^ r
else
r
end
end
elsif s.start_with?('var map = {')
s = s.delete_prefix('var map = {')
m = s.split('};return').first.split(',')
m.each do |s|
k, v = s.split(':')
if k == "\"#{arg}\""
return v.to_i
end
end
return nil
end
end

def self.response_requires_429?(response)
Expand Down
26 changes: 26 additions & 0 deletions spec/vk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,30 @@
})
end
end

describe 'js' do
describe '.js_func' do
it 'handles simple returns' do
expect(described_class.js_func('return -13')).to eq(-13)
end

it 'handles subtraction' do
expect(described_class.js_func('return e - 1', 10)).to eq(9)
end

it 'handles xor' do
expect(described_class.js_func('return e ^ 2', 10)).to eq(8)
end

it 'handles nil xor' do
expect(described_class.js_func('return e ^ 3', nil)).to eq(3)
end

it 'handles map access' do
expect(
described_class.js_func('var map = {"-1":1,"-2":2};return map[e]', -2))
.to eq(2)
end
end
end
end

0 comments on commit be98afd

Please sign in to comment.