├── _config.yml ├── .gitignore ├── go.mod ├── Makefile ├── .github └── workflows │ └── co.yml ├── go.sum ├── README.md ├── LICENSE ├── co_test.go └── co.go /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-hacker -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *DS_Store 2 | de3* 3 | co3* 4 | co 5 | *.txt 6 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/i0Ek3/co 2 | 3 | go 1.15 4 | 5 | require ( 6 | github.com/sirupsen/logrus v1.7.0 7 | github.com/stretchr/testify v1.2.2 8 | ) 9 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: build test clean 2 | 3 | GO=go 4 | 5 | build: 6 | @$(GO) build . 7 | 8 | test: 9 | @$(GO) test -v . 10 | 11 | clean: 12 | @rm co 13 | -------------------------------------------------------------------------------- /.github/workflows/co.yml: -------------------------------------------------------------------------------- 1 | name: co building 2 | on: 3 | push: 4 | branches: 5 | - master 6 | jobs: 7 | build: 8 | runs-on: ubuntu-18.04 9 | steps: 10 | - name: Checkout 11 | uses: actions/checkout@v2 12 | 13 | - name: Build 14 | run: | 15 | ls ${{ github.workspace }} 16 | go build co.go 17 | go test -v 18 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 4 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 5 | github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= 6 | github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= 7 | github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= 8 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 9 | golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4= 10 | golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # co 2 | 3 | `co` is a Go tool to obfuscate and deobfuscate the code string or code file, there are three algorithms you can choose to obfuscate and deobfuscate them. But for now, co only support simple code obfuscation and deobfuscation, we'll add some complicated obfuscation and deobfuscation algorithms later. Also there are so many flaws, so, please be nice. 4 | 5 | - basic code string obfuscation/deobfuscation 6 | - file obfuscation/deobfuscation 7 | - multiple alternative obfuscation/deobfuscation algorithms 8 | - support enable and disable debug mode 9 | - no third-party libraries 10 | - customize the error message display 11 | 12 | ## Getting Started 13 | 14 | `go run co.go` or `go build . ; ./co` 15 | 16 | 17 | ## TODO 18 | 19 | - exported some functions 20 | - cmd support(later...) 21 | - need to refactor 22 | - write good comments 23 | - perfect the tests 24 | 25 | ## Contributing 26 | 27 | Pull requests and Issues are also welcome. 28 | 29 | ## License 30 | 31 | MIT. 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 co 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /co_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func TestObfuscate(t *testing.T) { 9 | c := &Confuse{cobit: 8, debug: "false"} 10 | check := func(t *testing.T, got, want string) { 11 | if got != want { 12 | t.Errorf("got: '%s' want: '%s'", got, want) 13 | } 14 | } 15 | 16 | t.Run("obfuscate", func(t *testing.T) { 17 | got := c.coalgo4("hello", c.debug) 18 | want := "-0_7-0_4-1_3-1_3-1_6" 19 | check(t, got, want) 20 | }) 21 | 22 | t.Run("obfuscate", func(t *testing.T) { 23 | got := c.coalgo4("", c.debug) 24 | want := "" 25 | check(t, got, want) 26 | }) 27 | } 28 | 29 | func BenchmarkObfuscate(b *testing.B) { 30 | c := &Confuse{"", true, 4, 8, "false"} 31 | for i := 0; i < b.N; i++ { 32 | c.Obfuscate("hello", c.debug, c.algoid) 33 | } 34 | } 35 | 36 | func TestDebfuscate(t *testing.T) { 37 | c := &Confuse{cobit: 8, debug: "false"} 38 | check := func(t *testing.T, got, want string) { 39 | if got != want { 40 | t.Errorf("got: '%s' want: '%s'", got, want) 41 | } 42 | } 43 | 44 | t.Run("deobfuscate", func(t *testing.T) { 45 | got := c.dealgo3("-0_7-0_4-1_3-1_3-1_6", c.debug) 46 | want := "hello" 47 | check(t, got, want) 48 | }) 49 | 50 | t.Run("deobfuscate", func(t *testing.T) { 51 | got := c.dealgo3("", c.debug) 52 | want := "" 53 | check(t, got, want) 54 | }) 55 | } 56 | 57 | func BenchmarkDeobfuscate(b *testing.B) { 58 | c := &Confuse{"", true, 3, 8, "false"} 59 | for i := 0; i < b.N; i++ { 60 | c.Deobfuscate("-1_0-1_0-1_0", c.debug, c.algoid) 61 | } 62 | } 63 | 64 | func ExampleObfuscate() { 65 | c := &Confuse{"", true, 3, 8, "false"} 66 | ret := c.coalgo4("h", c.debug) 67 | fmt.Println(ret) 68 | // Output: -0_7 69 | } 70 | -------------------------------------------------------------------------------- /co.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | "math/rand" 7 | "strings" 8 | "unicode" 9 | ) 10 | 11 | // code confuse operations 12 | const ( 13 | OB = "obfuscate" 14 | DE = "deobfuscate" 15 | NO = "" 16 | 17 | // alphabets number 18 | AN = 26 19 | ) 20 | 21 | // aliased 22 | type result []string 23 | 24 | // Confuse defines code confuse fileds 25 | type Confuse struct { 26 | status string 27 | algoed bool 28 | algoid int 29 | cobit int 30 | debug string 31 | } 32 | 33 | // Obfuscation defines code confuse interface 34 | type Obfuscation interface { 35 | checkStatus(status string) bool 36 | checkID(id int) bool 37 | caseTransform(code string, mode ...string) 38 | isCodeEmpty(code string) (ok bool) 39 | 40 | // for code obfuscate 41 | processOB(code, debug string) 42 | Obfuscate(code, debug string, id ...int) 43 | coalgo(id int, code, debug string) (newdata string) 44 | coalgo1(code, debug string) string 45 | coalgo2(code, debug string) string 46 | mapCode2Char(code string, len int) result 47 | coalgo3(code, debug string) string 48 | coalgo4(code, debug string) string 49 | parseEncodeIntoFile(code string, algoid int, debug string) bool 50 | processFileOB(filename string, algoid int, debug string) string 51 | 52 | // for code deobfuscate 53 | processDE(code, debug string) 54 | Deobfuscate(code, debug string, id ...int) 55 | dealgo(id int, code, debug string) (newdata string) 56 | dealgo1(code, debug string) string 57 | dealgo2(code, debug string) string 58 | dealgo3(code, debug string) string 59 | parseDecodeIntoFile(code string, algoid int, debug string) bool 60 | processFileDE(filename string, algoid int, debug string) string 61 | } 62 | 63 | // New creates a pointer type Confuse 64 | func NewConfuse(defa bool, id, bit int, debug string) *Confuse { 65 | co := &Confuse{ 66 | algoed: defa, 67 | algoid: id, 68 | cobit: bit, 69 | debug: debug, 70 | } 71 | return co 72 | } 73 | 74 | func (c *Confuse) checkStatus(status string) bool { 75 | if status == OB || status == DE { 76 | return true 77 | } 78 | return false 79 | } 80 | 81 | func (c *Confuse) checkID(id int) bool { 82 | if c.algoid >= 1 && c.algoid <= 3 { 83 | return true 84 | } 85 | return false 86 | } 87 | 88 | func (c *Confuse) caseTransform(code string, mode ...string) { 89 | alphabetu := make(map[int]rune, AN) 90 | alphabetl := make(map[int]rune, AN) 91 | 92 | upper := 0 93 | lower := 0 94 | if len(mode) == 0 { 95 | for i, _ := range code { 96 | if unicode.IsUpper((rune)(code[i])) { 97 | upper++ 98 | } else if unicode.IsLower((rune)(code[i])) { 99 | lower++ 100 | } 101 | } 102 | 103 | if upper > lower { 104 | strings.ToUpper(code) 105 | for i := 0; i < AN; i++ { 106 | for j := 'A'; j <= 'Z'; j++ { 107 | alphabetu[i] = j 108 | } 109 | } 110 | } else { 111 | strings.ToLower(code) 112 | for i := 0; i < AN; i++ { 113 | for j := 'a'; j <= 'z'; j++ { 114 | alphabetl[i] = j 115 | } 116 | } 117 | } 118 | } else { 119 | if mode[0] == "lower" { 120 | strings.ToLower(code) 121 | for i := 0; i < AN; i++ { 122 | for j := 'a'; j <= 'z'; j++ { 123 | alphabetl[i] = j 124 | } 125 | } 126 | } else if mode[0] == "upper" { 127 | strings.ToUpper(code) 128 | for i := 0; i < AN; i++ { 129 | for j := 'A'; j <= 'Z'; j++ { 130 | alphabetu[i] = j 131 | } 132 | } 133 | } 134 | } 135 | } 136 | 137 | func (c *Confuse) isCodeEmpty(code string) bool { 138 | if code == "" { 139 | return false 140 | } 141 | return true 142 | } 143 | 144 | func (c *Confuse) processOB(code, debug string) (ok bool) { 145 | if c.isCodeEmpty(code) { 146 | ok = true 147 | if c.algoed && c.checkStatus(c.status) { 148 | switch c.status { 149 | case OB: 150 | if c.checkID(c.algoid) { 151 | c.Obfuscate(code, debug, c.algoid) 152 | } else { 153 | showMsg("wrong encoding number.") 154 | } 155 | } 156 | } else { 157 | switch c.status { 158 | case OB: 159 | c.Obfuscate(code, debug) 160 | } 161 | } 162 | } 163 | return ok == true 164 | } 165 | 166 | // Obfuscate obfuscates the code 167 | func (c *Confuse) Obfuscate(code, debug string, id ...int) { 168 | if len(id) > 0 { 169 | c.algoid = id[0] 170 | if c.checkID(c.algoid) { 171 | // TODO: add channel to transport data 172 | go func(i int) { 173 | //c.coalgo(c.algoid, code, debug) 174 | c.coalgo(i, code, debug) 175 | }(c.algoid) 176 | } 177 | } 178 | } 179 | 180 | // coalgo defines code obfuscation algorithms 181 | func (c *Confuse) coalgo(id int, code, debug string) (newdata string) { 182 | switch id { 183 | case 1: 184 | newdata = c.coalgo1(code, debug) 185 | case 2: 186 | newdata = c.coalgo2(code, debug) 187 | case 3: 188 | newdata = c.coalgo3(code, debug) 189 | case 4: 190 | newdata = c.coalgo4(code, debug) 191 | } 192 | return 193 | } 194 | 195 | // coalgo1 encoding the code string with ordinary offset 196 | // transform, which means map alphabet to next n alphabet 197 | // with all lower case. 198 | func (c *Confuse) coalgo1(code, debug string) string { 199 | var encode string 200 | alphabet := make(map[int]rune) 201 | 202 | encode = strings.ToLower(code) 203 | 204 | for i := 0; i < AN; i++ { 205 | for j := 'a'; j <= 'z'; j++ { 206 | alphabet[i] = j 207 | } 208 | } 209 | 210 | for i, _ := range code { 211 | num := (int)(code[i]) - 48 - 97 + c.cobit - AN 212 | if num > 0 { 213 | encode = string(alphabet[num]) 214 | } 215 | encode = string(alphabet[num+AN]) 216 | } 217 | 218 | return encode 219 | } 220 | 221 | // coalgo2 encoding the code string with ordinary offset 222 | // transform, which means map alphabet to next n alphabet 223 | // with all upper case. 224 | func (c *Confuse) coalgo2(code, debug string) string { 225 | var encode string 226 | alphabet := make(map[int]rune) 227 | 228 | encode = strings.ToUpper(code) 229 | 230 | for i := 0; i < AN; i++ { 231 | for j := 'A'; j <= 'Z'; j++ { 232 | alphabet[i] = j 233 | } 234 | } 235 | 236 | for i, _ := range code { 237 | num := (int)(code[i]) - 48 - 65 + c.cobit - AN 238 | if num > 0 { 239 | encode = string(alphabet[num]) 240 | } 241 | encode = string(alphabet[num+AN]) 242 | } 243 | 244 | return encode 245 | } 246 | 247 | func (c *Confuse) mapCode2Char(code string, len int) result { 248 | mode := "lower" 249 | c.caseTransform(code, mode) 250 | 251 | specChar := result{"_", "-"} 252 | newer := make(result, len) 253 | 254 | for i := 0; i < AN; i++ { 255 | newer[i] = specChar[1] + fmt.Sprint(i/c.cobit) + specChar[0] + fmt.Sprint(i%c.cobit) 256 | } 257 | return newer 258 | } 259 | 260 | // coalgo3 encoding the code string with ordinary offset 261 | // transform, but mapped with special characters like _. 262 | func (c *Confuse) coalgo3(code, debug string) string { 263 | ret := "" 264 | newer := c.mapCode2Char(code, len(code)) 265 | 266 | for i := range code { 267 | idx := int(code[i]) 268 | if debug == "true" { 269 | fmt.Printf("idx ----> %v\n", idx) 270 | } 271 | 272 | // TODO: parse other special characters rather than letter only 273 | if 97 <= idx && idx <= 122 { 274 | n := idx - 97 275 | ret += newer[n] 276 | if debug == "true" { 277 | fmt.Printf("ret ----> %v\n", ret) 278 | } 279 | } 280 | } 281 | return ret 282 | } 283 | 284 | func (c *Confuse) coalgo4(code, debug string) string { 285 | ret := "" 286 | newer := c.mapCode2Char(code, AN) 287 | 288 | for i := range code { 289 | idx := int(code[i]) 290 | if debug == "true" { 291 | fmt.Printf("idx ----> %v\n", idx) 292 | } 293 | n := idx - 97 294 | ret += newer[n] 295 | if debug == "true" { 296 | fmt.Printf("ret ----> %v\n", ret) 297 | } 298 | } 299 | return ret 300 | } 301 | 302 | func (c *Confuse) parseEncodeIntoFile(code string, algoid int, debug string) bool { 303 | var newdata string 304 | rand.Seed(1000) 305 | name := "co" + fmt.Sprint(algoid) + "_" + fmt.Sprint(rand.Intn(100000)) + ".txt" 306 | 307 | // TODO 308 | //newdata = c.coalgo(algoid, code, debug) 309 | newdata = c.coalgo4(code, debug) 310 | 311 | if err := ioutil.WriteFile(name, []byte(newdata), 0644); err != nil { 312 | showMsg("file write failed.") 313 | } 314 | fmt.Println(newdata) 315 | return true 316 | } 317 | 318 | func (c *Confuse) processFileOB(filename string, algoid int, debug string) string { 319 | var newdata string 320 | 321 | data, err := ioutil.ReadFile(filename) 322 | if err != nil { 323 | showMsg("file read failed.") 324 | } 325 | 326 | name := "co" + fmt.Sprint(algoid) + "_" + strings.TrimRight(filename, "go") + "txt" 327 | 328 | newdata = c.coalgo(algoid, string(data), debug) 329 | if err := ioutil.WriteFile(name, []byte(newdata), 0644); err != nil { 330 | showMsg("file write failed.") 331 | } 332 | return newdata 333 | } 334 | 335 | func (c *Confuse) processDE(code, debug string) (ok bool) { 336 | if c.isCodeEmpty(code) { 337 | ok = true 338 | if c.algoed && c.checkStatus(c.status) { 339 | switch c.status { 340 | case DE: 341 | if c.checkID(c.algoid) { 342 | c.Deobfuscate(code, debug, c.algoid) 343 | } else { 344 | showMsg("wrong decoding number.") 345 | } 346 | } 347 | } else { 348 | switch c.status { 349 | case DE: 350 | c.Deobfuscate(code, debug) 351 | } 352 | } 353 | } 354 | return ok == true 355 | } 356 | 357 | // Deobfuscate deobfuscates the code 358 | func (c *Confuse) Deobfuscate(code, debug string, id ...int) { 359 | if len(id) > 0 { 360 | c.algoid = id[0] 361 | if c.checkID(c.algoid) { 362 | // TODO 363 | go func(i int) { 364 | //c.dealgo(c.algoid, code, debug) 365 | c.dealgo(i, code, debug) 366 | }(c.algoid) 367 | } 368 | } 369 | } 370 | 371 | // dealgo defines code deobfuscation algorithms 372 | func (c *Confuse) dealgo(id int, code, debug string) (newdata string) { 373 | switch id { 374 | case 1: 375 | newdata = c.dealgo1(code, debug) 376 | case 2: 377 | newdata = c.dealgo2(code, debug) 378 | case 3: 379 | newdata = c.dealgo3(code, debug) 380 | } 381 | return 382 | } 383 | 384 | func (c *Confuse) dealgo1(code, debug string) string { 385 | var decode string 386 | alphabet := make(map[int]rune) 387 | 388 | for i := 0; i < AN; i++ { 389 | for j := 'i'; j <= 'r'; j++ { 390 | alphabet[i] = j 391 | } 392 | for k := 'a'; k <= 'h'; k++ { 393 | alphabet[i] = k 394 | } 395 | } 396 | 397 | for i, _ := range code { 398 | num := (int)(code[i]) + 48 + 97 - c.cobit + AN 399 | if num > AN { 400 | decode = string(alphabet[num-AN]) 401 | } 402 | decode = string(alphabet[num]) 403 | } 404 | 405 | return decode 406 | } 407 | 408 | func (c *Confuse) dealgo2(code, debug string) string { 409 | var decode string 410 | alphabet := make(map[int]rune) 411 | 412 | for i := 0; i < AN; i++ { 413 | for j := 'I'; j <= 'R'; j++ { 414 | alphabet[i] = j 415 | } 416 | for k := 'A'; k <= 'H'; k++ { 417 | alphabet[i] = k 418 | } 419 | } 420 | 421 | for i, _ := range code { 422 | num := (int)(code[i]) + 48 + 65 - c.cobit + AN 423 | if num > AN { 424 | decode = string(alphabet[num-AN]) 425 | } 426 | decode = string(alphabet[num]) 427 | } 428 | 429 | return decode 430 | } 431 | 432 | func (c *Confuse) dealgo3(code, debug string) string { 433 | m := map[rune]string{ 434 | 'a': "-0_0", 435 | 'b': "-0_1", 436 | 'c': "-0_2", 437 | 'd': "-0_3", 438 | 'e': "-0_4", 439 | 'f': "-0_5", 440 | 'g': "-0_6", 441 | 'h': "-0_7", 442 | 'i': "-1_0", 443 | 'j': "-1_1", 444 | 'k': "-1_2", 445 | 'l': "-1_3", 446 | 'm': "-1_4", 447 | 'n': "-1_5", 448 | 'o': "-1_6", 449 | 'p': "-1_7", 450 | 'q': "-2_0", 451 | 'r': "-2_1", 452 | 's': "-2_2", 453 | 't': "-2_3", 454 | 'u': "-2_4", 455 | 'v': "-2_5", 456 | 'w': "-2_6", 457 | 'x': "-2_7", 458 | 'y': "-3_0", 459 | 'z': "-3_1", 460 | } 461 | 462 | ret := "" 463 | res := "" 464 | sign := 0 465 | 466 | for i := 0; i < len(code); i++ { 467 | if (i+1)%4 == 0 { 468 | for j := sign; j < i+1; j++ { 469 | ret += string(code[j]) 470 | } 471 | 472 | if debug == "true" { 473 | fmt.Printf("ret ----> %v\n", ret) 474 | } 475 | 476 | for idx, v := range m { 477 | if ret == v { 478 | res += string(idx) 479 | if debug == "true" { 480 | fmt.Printf("idx ----> %v\n", idx) 481 | } 482 | } 483 | } 484 | if debug == "true" { 485 | fmt.Printf("res ----> %v\n", res) 486 | } 487 | sign += 4 488 | ret = "" 489 | } 490 | } 491 | if debug == "true" { 492 | fmt.Printf("result ----> %v\n", res) 493 | } 494 | return res 495 | } 496 | 497 | func (c *Confuse) parseDecodeIntoFile(code string, algoid int, debug string) bool { 498 | var newdata string 499 | rand.Seed(1000) 500 | name := "de" + fmt.Sprint(algoid) + "_" + fmt.Sprint(rand.Intn(100000)) + ".txt" 501 | 502 | newdata = c.dealgo(algoid, code, debug) 503 | if err := ioutil.WriteFile(name, []byte(newdata), 0644); err != nil { 504 | showMsg("file write failed.") 505 | } 506 | fmt.Println(newdata) 507 | return true 508 | } 509 | 510 | func (c *Confuse) processFileDE(filename string, algoid int, debug string) string { 511 | data, err := ioutil.ReadFile(filename) 512 | if err != nil { 513 | showMsg("file read failed.") 514 | } 515 | 516 | name := "de" + fmt.Sprint(algoid) + "_" + strings.TrimRight(filename, "txt") + "go" 517 | 518 | newdata := c.dealgo(algoid, string(data), debug) 519 | if err := ioutil.WriteFile(name, []byte(newdata), 0644); err != nil { 520 | showMsg("file write failed.") 521 | } 522 | return newdata 523 | } 524 | 525 | func showMsg(msg string) { 526 | fmt.Printf("\tInfo -----> %v\n", msg) 527 | } 528 | 529 | func doLoop(input, res1, res2, msg string) bool { 530 | if input == res1 || input == res2 { 531 | return false 532 | } else { 533 | showMsg(msg) 534 | return true 535 | } 536 | } 537 | 538 | func runIO(input *string, output string) { 539 | fmt.Printf(output) 540 | fmt.Scan(input) 541 | } 542 | 543 | func main() { 544 | var ( 545 | input string 546 | inputO string 547 | filename string 548 | code string 549 | debug string 550 | ) 551 | 552 | const ( 553 | OBSTR = "OB" 554 | DESTR = "DE" 555 | 556 | OUTPUT = "Which option do you want to operate? [OB or DE]: " 557 | OBMSG = "None of them, please input again!" 558 | 559 | CODE = "code" 560 | FILE = "file" 561 | 562 | T = "true" 563 | F = "false" 564 | 565 | OBSRC = "Which option do you want to obfuscate? [code or file]: " 566 | DESRC = "Which option do you want to deobfuscate? [code or file]: " 567 | 568 | CODEIN = "Please input the code string: " 569 | DEBUG = "Would you like to enable the debug mode? [true or false]: " 570 | FNSTR = "Please input the filename: " 571 | ) 572 | 573 | // default algorithm is coalgo3() and dealgo3(), 574 | // and debug mode disable 575 | co := NewConfuse(true, 3, 8, F) 576 | 577 | A1: 578 | runIO(&inputO, OUTPUT) 579 | if doLoop(inputO, OBSTR, DESTR, OBMSG) { 580 | goto A1 581 | } 582 | 583 | switch inputO { 584 | case OBSTR: 585 | A2: 586 | runIO(&input, OBSRC) 587 | if doLoop(input, CODE, FILE, OBMSG) { 588 | goto A2 589 | } 590 | 591 | switch input { 592 | case CODE: 593 | runIO(&code, CODEIN) 594 | A3: 595 | runIO(&debug, DEBUG) 596 | if doLoop(debug, T, F, OBMSG) { 597 | goto A3 598 | } 599 | 600 | if co.processOB(code, co.debug) { 601 | co.parseEncodeIntoFile(code, co.algoid, debug) 602 | showMsg("code obfuscated to the file!") 603 | } else { 604 | showMsg("code cannot obfuscated!") 605 | } 606 | case FILE: 607 | runIO(&filename, FNSTR) 608 | A4: 609 | runIO(&debug, DEBUG) 610 | if doLoop(debug, T, F, OBMSG) { 611 | goto A4 612 | } 613 | 614 | if filename != "" { 615 | co.processFileOB(filename, co.algoid, debug) 616 | showMsg("file obfuscated.") 617 | } else { 618 | showMsg("invalid filename!") 619 | } 620 | } 621 | case DESTR: 622 | A5: 623 | runIO(&input, DESRC) 624 | if doLoop(input, CODE, FILE, OBMSG) { 625 | goto A5 626 | } 627 | 628 | switch input { 629 | case CODE: 630 | runIO(&code, CODEIN) 631 | A6: 632 | runIO(&debug, DEBUG) 633 | if doLoop(debug, T, F, OBMSG) { 634 | goto A6 635 | } 636 | 637 | if co.processDE(code, co.debug) { 638 | co.parseDecodeIntoFile(code, co.algoid, debug) 639 | showMsg("code deobfuscated to the file!") 640 | } else { 641 | showMsg("code cannot deobfuscated!") 642 | } 643 | case FILE: 644 | runIO(&filename, FNSTR) 645 | A7: 646 | runIO(&debug, DEBUG) 647 | if doLoop(debug, T, F, OBMSG) { 648 | goto A7 649 | } 650 | 651 | if filename != "" { 652 | co.processFileDE(filename, co.algoid, debug) 653 | showMsg("file deobfuscated.") 654 | } else { 655 | showMsg("invalid filename!") 656 | } 657 | } 658 | default: 659 | } 660 | } 661 | --------------------------------------------------------------------------------