[LLVMdev] Re: Hide visible string in variable (Chris Lattner)
Zhang Qiuyu
qiuyu at ucla.edu
Mon Oct 11 23:52:22 PDT 2004
Hi,
Thanks so much at first.
> Here are some observations:
>
> > for C level,
> >
> > char a[]="global string test";
> > for(i=0;i<strlen(a);i++){
> > a[i]= a[i]^RANDMON;
> > }
>
> If you compile this C code, "global string test" will occur in the program
> binary, so you have not obfuscated anything. You can construct exactly
> what you have above in LLVM (just write it as C, compile it to LLVM and
> you'll see what you need to generate), but I don't think this is what you
> want. What you really want is:
>
> char a[]="GLOBAL STRING TEST";
>
> and when the program starts up (perhaps in main), you want to insert this:
>
> for (i=0...)
> A[i] = tolower(a[i]);
>
> Note that you can use whatever function you wanted, obviously uppercasing
> the string isn't much obfuscation.
>
> To do this on LLVM, you have to do these things:
>
> 1. Read the string data as a constant (It's an instance of ConstantArray,
> which you get form the Globalvaraible with getInitializer() as you are
> doing.
Following your suggestion, I got some progress. Thanks again. But I am still stuck in some problems.
Constant *Cstr = GV->getInitializer();
After that, I tried to use
a.
for(unsigned i=0;i<Cstr->getNumOperands();++i){
Cstr->getOperand(i);
}
b. for(User::op_iterator I=Cstr->op_begin(),E=Cstr->op_end(); I!=E;++I){
std::cerr<<*I;
}
From either a or b, I could get each element of Global Variable. Supposedly, I will use my arithmetic like XOR etc to encode/hide the string. But I cannot use XOR, I mean I tried (*I)^0x33, it doesn't work. I tried op_xor, but I don't know how to use it. For C level, it is really staightforward. But here, I don't know how to do it. It should be easy way to do it. But I spent several hours on it. For simplifing prolem, I also tried to do the way like
a[i]=a[i]+1;
but I failed. Shy.
> 2. Construct a new ConstantArray with all of the elements of the original
> string, but modified according to the function you want (exclusive or
> is a reasonable start).
As you said, how could construct a new ConstantArray? Is it like
Constant *pC = new Constant(SBtype); // Constant(const Type *Ty)
for ConstantArray, replaceUsesOfWithOnConstant(Value *From, Value *To,
bool DisableChecking = false); this
API replaceUsesOfWithOnConstant seems to be able to do what I want, but how could create/construct a new Value with my owner value?
for 3,4,5, those should work well.
I would really appreciated if you can give me a very simple example to show me how to do it.
> 3. Change the initializer of the global variable to the new constant with
> setInitializer().
> 4. Clear the "constant" flag on the string, because the program will be
> dynamically hacking on the string: GV->setConstant(false);
> 5. Insert the for loop that translates the string when main runs.
>
> For #5, write the for loop you want, compile it with llvmgcc, then figure
> out how to generate it at compile time. Alternatively, you could put the
> 'decryption' routine in a library and just insert a call to the library.
>
> -Chris
>
More information about the llvm-dev
mailing list