Skip to content

Instantly share code, notes, and snippets.

@programaths
Created March 16, 2017 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save programaths/0b223e6f1531785cf15b3133d99d94bb to your computer and use it in GitHub Desktop.
Save programaths/0b223e6f1531785cf15b3133d99d94bb to your computer and use it in GitHub Desktop.
Generate deobfusacting golang
package main
import (
"fmt"
"math/rand"
)
func Generate(ch chan<- int) {
for i := 2; ; i++ {
ch <- i
}
}
func Filter(in <-chan int, out chan<- int, prime int) {
for {
i := <-in
if i%prime != 0 {
out <- i
}
}
}
func Send(c chan int, t int) (bool) {
defer func() {recover()}()
c <- t
return true
}
func Primes(ch chan int){
chn:=make(chan int)
go Generate(chn)
for i := 0; ; i++ {
prime := <-chn
if !Send(ch,prime) { break }
ch1 := make(chan int)
go Filter(chn, ch1, prime)
chn = ch1
}
}
func Factors(n int) (result map[int]int) {
result=make(map[int]int)
ch := make(chan int)
go Primes(ch)
for i := range ch{
pow:=0
for n%i == 0 {
n = n / i
pow++
}
result[i]=pow
if n == 1 {
for j := 0; j < 10; j++ {
result[<-ch] = 0
}
break
}
}
close(ch)
return result
}
func GetKey(s,e string) int {
ln:=len(s)
for i := 1; i < ln; i++ {
d:=Decode(s,i)
if d==e{
return i
}
}
panic("No key")
}
func Decode(a string,n int) string {
b:=""
ln:=len(a)
for i:=0;i<ln;i++ {
b = b + string(a[(i * n) % ln])
}
return b
}
func main() {
a:=`This generates a nice Go program which decodes an obfuscated string.
Admittingly, noone can do this while being interviewing.
Still, it can be some fun to experiment with it !
`
b:=""
ln:=len(a)
factors := Factors(ln)
fmt.Println(factors)
mult:=1
for k,v:=range factors{
if v!=0 {
continue
}
pow:=1+rand.Intn(2)
fmt.Println(k,"^",pow)
for pow>0 {
fmt.Println("mult =",mult)
mult=(mult*k)%ln
fmt.Println("mult times",k,"mod",ln,"=",mult)
pow--
}
}
fmt.Println(mult)
for i:=0;i<ln;i++{
b=b+string(a[(i*mult)%ln])
}
key:=GetKey(b,a)
fmt.Printf(`
package main
import (
"fmt"
)
func main() {
a := `+"`"+`%[1]s`+"`"+`
for i := 0; i < %[3]d; i++ {
fmt.Print(string(a[(i*%[2]d)%%%[3]d]))
}
}
`,b,key,ln)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment