Module: Obscure

Defined in:
lib/codeobscure/obscure.rb

Constant Summary collapse

@@TABLENAME =
"symbols"
@@SYMBOL_DB_FILE =
"symbols"
@@STRING_SYMBOL_FILE =
"func.list"

Class Method Summary collapse

Class Method Details

.createTableObject

维护数据库方便日后作排重



17
18
19
20
21
# File 'lib/codeobscure/obscure.rb', line 17

def self.createTable  
  if !File.exist? @@SYMBOL_DB_FILE
    `echo "create table #{@@TABLENAME}(src text, des text,PRIMARY KEY (src));" | sqlite3 #{@@SYMBOL_DB_FILE}` 
  end 
end

.insertValue(line, ramdom) ⇒ Object



23
24
25
# File 'lib/codeobscure/obscure.rb', line 23

def self.insertValue(line,ramdom)  
  `echo "insert or ignore into #{@@TABLENAME} values('#{line}' ,'#{ramdom}');" | sqlite3 #{@@SYMBOL_DB_FILE}`
end

.query(line) ⇒ Object



27
28
29
# File 'lib/codeobscure/obscure.rb', line 27

def self.query(line) 
  `echo "select * from #{@@TABLENAME} where src='#{line}';" | sqlite3 #{@@SYMBOL_DB_FILE}`
end

.ramdomStringObject



31
32
33
# File 'lib/codeobscure/obscure.rb', line 31

def self.ramdomString  
  `openssl rand -base64 64 | tr -cd 'a-zA-Z' |head -c 16`
end

.run(root_dir) ⇒ Object

有define重复的问题等待解决



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
# File 'lib/codeobscure/obscure.rb', line 36

def self.run(root_dir)

  @@HEAD_FILE="#{root_dir}/codeObfuscation.h"  
  @@SYMBOL_DB_FILE = "#{root_dir}/#{@@SYMBOL_DB_FILE}" 
  @@STRING_SYMBOL_FILE = "#{root_dir}/#{@@STRING_SYMBOL_FILE}"
  
  if File.exist? @@SYMBOL_DB_FILE
    `rm -f #{@@SYMBOL_DB_FILE}`
  end 
  if File.exists? @@HEAD_FILE 
    `rm -f #{@@HEAD_FILE}` 
  end 
  createTable  
    
  date = `date`
  file = File.new @@HEAD_FILE , 'w'
  file.puts "#ifndef co_codeObfuscation_h" 
  file.puts "#define co_codeObfuscation_h" 
  file.puts "//confuse string at #{date.to_s}"

  symbol_file = File.open(@@STRING_SYMBOL_FILE).read
  symbol_file.each_line do |line|
    line_type = line.rstrip.split(":").first
    line_content = line.rstrip.split(":").last
    result = FiltSymbols.query(line_content) 
    if result.nil? || result.empty? 
      ramdom = ramdomString
      insertValue line_content  , ramdom  
      if line_type == "p"
        result = FiltSymbols.query("set#{line_content.upcase_first_letter}") 
        if result.nil? || result.empty? 
          file.puts "#define #{line_content} #{ramdom}"
          file.puts "#define _#{line_content} _#{ramdom}"
          file.puts "#define set#{line_content.upcase_first_letter} set#{ramdom.upcase_first_letter}"
        end
      else 
        file.puts "#define #{line_content} #{ramdom}"
      end
    end 
  end

  file.puts "#endif" 
  file.close

  `sqlite3 #{@@SYMBOL_DB_FILE} .dump`  
  if File.exist? @@SYMBOL_DB_FILE
    `rm -f #{@@SYMBOL_DB_FILE}`
  end
  if File.exist? @@STRING_SYMBOL_FILE
    `rm -f #{@@STRING_SYMBOL_FILE}`
  end
  @@HEAD_FILE
end