ac1d_buRn (07-11-2012),Flengo Jr. (07-11-2012),luccss (07-11-2012),OBrozz (07-11-2012),pDevice (08-22-2012),PikaMucha_Itu (07-11-2012),Reflex- (07-11-2012)
K guys I want to explain the shift position encryption cipher method, I see alot of C++ code here using shift method incorrectly below is a modified caesar cipher, First modification is that we don't use Alphabet A..Z which is 26 characters, Instead we use all readable characters and special characters, the second modification is that the Alphabet we use is mixed up making harder to decrypt as it not in its common Alphabet order. The reason this will work is because we use modulus to cycle through the Alphabet, This means when we get to the last Character of the Alphabet is will start from the begining of the alphabet, This prevents it from making the cipher non readable if the shift is more than the alphabet, The C++ I have seen have this potential bug if used incorrectly. Anyway here is a simple Left/Right Modified Caesar Cipher.
Modified Caesar Cipher:
Now the next Function is similar to a Caesar cipher(shift method) but would be a lot harder to crack without knowing the "key" its called the vigenere cipher. This is my implementation of the vigenere cipher and this code also got published on delphi.abou*****mCode:Function CaesarLeft(sString: String; iAmount: Integer):String; var i, iPos, iAlphaBet: Integer; sAlphabet: String; begin sAlphabet:= 'aBcDeFgHiJkLmNoPqRsTuVwXyZAbCdEfGhIjKlMnOpQrStUvWxYz1029384756'; iAlphaBet:= Length(sAlphabet); i:= 1; while i <= Length(sString) do Begin if sString[i] = ' ' then Result:= Result + ' ' else begin iPos:= pred(pos(sString[i],sAlphabet)); if iPos < 1 then iPos:= 1; Result:= Result + sAlphabet[(((iPos + iAlphaBet) - iAmount) mod iAlphaBet) + 1]; end; inc(i); end; end; Function CaesarRight(sString: String; iAmount: Integer):String; var i, iPos, iAlphaBet: Integer; sAlphabet: String; begin sAlphabet:= 'aBcDeFgHiJkLmNoPqRsTuVwXyZAbCdEfGhIjKlMnOpQrStUvWxYz1029384756'; iAlphaBet:= Length(sAlphabet); i:= 1; while i <= Length(sString) do Begin if sString[i] = ' ' then Result:= Result + ' ' else begin iPos:= pred(pos(sString[i],sAlphabet)); Result:= Result + sAlphabet[((iPos + iAmount) mod iAlphaBet) + 1]; end; inc(i); end; end;
Vigenere Cipher:
Hope this helps anyone wanting to understand the Shift method of encryption, The vigenere is ofcause alot harder to crack than caesar and Xor methods... I also coded this in C# so if people have trouble reading Delphi syntax and fine it easier to read in C# then I can post that source code also.Code:Function VigenereExEncrypt(sSource, sKey: String; bDecrypt: Boolean = False; iTableSize: Integer = 94): String; var i, iPosText, iPosKey: Integer; sTable: string; begin //Create our Cipher Table i:= 32; While i <= (iTableSize + 32) do Begin sTable:= ConCat(sTable, Chr(i)); inc(i); end; //Make the key the same size or greater than the Source while Length(sSource) >= Length(sKey) do sKey:= ConCat(sKey,sKey); //Remove Line Feed & Carrage Returns from Cipher i:=0; while i<=Length(sSource) do if (sSource[i]=Chr(10)) or (sSource[i]=Chr(13)) then Delete(sSource, i, 1) else Inc(i); {Uncomment if you need to remove spaces //Remove Spaces from Cipher i:=0; while i<=Length(sSource) do if sSource[i]=' ' then Delete(sSource, i, 1) else Inc(i); } //Vegenere Encryption/Decryption routine i:= 1; while i <= Length(sSource) do Begin iPosText := pred(pos(sSource[i],sTable)); iPosKey := pred(pos(sKey[i],sTable)); //Encrypt or Decrypt(Default is Encrypt) Case bDecrypt of False: Result := Result + sTable[((iPosText + iPosKey) mod iTableSize) + 1]; True : Result := Result + sTable[(((iPosText + iTableSize) - iPosKey) mod iTableSize) + 1]; end; inc(i); end; end;
//Edit
Decided to add my C# source because I know not many people code in Delphi and it will be easier for them to read C#, Mind you C# is not my first choice of programming languages and if you are a full C# coder you might see a better way to code this, If you do find a better way then please post your implementation
C# Caesar/Rot13/Vigenere:
Code:using System; /* Class : StringCrypt.cs Aurthor: Departure Url : im-integrations.com Info: Encrypt Strings with Vigenere Cipher and ROT Cipher(Caesar Cipher, Rot13 ect..) Based on information from wiki */ internal class StringCrypt { public static string Vigenere(string sSource, string sKey, int iTableSize = 94, bool bDecrypt = false) { //Variables int i = 32; int iPosText; int iPosKey; string sTable = ""; string sResult = ""; //Create Table while (i < (iTableSize + 32)) { sTable += ((char)i); i++; } //Make Key same size as Cipher while (sSource.Length >= sKey.Length) { sKey = string.Concat(sKey, sKey); } //Vigenere Routine i = 0; while (i <= (sSource.Length - 1)) { if (sTable.IndexOf(sSource[i]) == -1) sResult += sSource[i]; else { iPosText = sTable.IndexOf(sSource[i]); iPosKey = sTable.IndexOf(sKey[i]); if (bDecrypt) sResult += sTable[(((iPosText + iTableSize) - iPosKey) % iTableSize)]; else sResult += sTable[((iPosText + iPosKey) % iTableSize)]; } i++; } return sResult; } public static string Caesar(string sSource, int iKey, bool bDecrypt = false) { //Variables string sResult = ""; string sTable = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int iPosText; int i; //Convert to Uppercase sSource = sSource.ToUpper(); //Caesar Routine i = 0; while (i <= (sSource.Length - 1)) { if (sTable.IndexOf(sSource[i]) == -1) sResult += sSource[i]; else { iPosText = sTable.IndexOf(sSource[i]); if (bDecrypt) sResult += sTable[(((iPosText + sTable.Length) - iKey) % sTable.Length)]; else sResult += sTable[((iPosText + iKey) % sTable.Length)]; } i++; } return sResult; } public static string ROT13(string sSource) { return Caesar(sSource, 13, false); } }
Last edited by Departure; 07-11-2012 at 02:05 AM. Reason: Added C# source
ac1d_buRn (07-11-2012),Flengo Jr. (07-11-2012),luccss (07-11-2012),OBrozz (07-11-2012),pDevice (08-22-2012),PikaMucha_Itu (07-11-2012),Reflex- (07-11-2012)
Nice one. I took the liberty of writing a C# version for my own benefit, here are the fruits of my labour:
DISCLAIMER: Only tested with a few basic ones. Also, doesn't handle special characters (i.e éêþ and whatever else)Code:public static class VigenereCipher { private const int LOWER_ASCII = 32; // start at ' ' (space) private const int UPPER_ASCII = 129; // end past DEL. Note, I will be using 127/128/129 to mask CR/LF/TAB respectively. private const int RANGE_ASCII = UPPER_ASCII - LOWER_ASCII; public static string CipherText(string key, string data) { if (!string.IsNullOrEmpty(data)) { data = data.Replace('\r', (char)127).Replace('\n', (char)128).Replace('\t', (char)129); //normalize the string first. string cipherKey = CreateKey(key, data.Length); //need an acceptable key. // a touch of LINQ magic brightens everyone's day :3 return data.Aggregate(new StringBuilder(data.Length), (builder, c) => builder.Append( (char)((((c + cipherKey[builder.Length]) - (LOWER_ASCII << 1)) % RANGE_ASCII) + LOWER_ASCII) // mindfuck alert. ) ).ToString(); } return data; } public static string DecipherText(string key, string data) { if (!string.IsNullOrEmpty(data)) { string decipherKey = CreateKey(key, data.Length); data = data.Aggregate(new StringBuilder(data.Length), (builder, c) => builder.Append( (char)((((c + RANGE_ASCII) - decipherKey[builder.Length]) % RANGE_ASCII) + LOWER_ASCII) // MOAR MINDFUX ) ).ToString(); return data.Replace((char)127, '\r').Replace((char)128, '\n').Replace((char)129, '\t'); //reverse the special char replacement. } return data; } // simple helper method to create an acceptable Vigenére cipher key. private static string CreateKey(string baseKey, int len) { if (baseKey.Length == len) return baseKey; else if (baseKey.Length > len) return baseKey.Substring(0, len); else return Enumerable.Range(0, len - baseKey.Length).Aggregate(new StringBuilder(baseKey, len), (builder, index) => builder.Append(builder[index])).ToString(); } }
Last edited by Jason; 07-11-2012 at 07:51 AM.
You can win the rat race,Originally Posted by Jeremy S. Anderson
But you're still nothing but a fucking RAT.
++Latest Projects++
[Open Source] Injection Library
Simple PE Cipher
FilthyHooker - Simple Hooking Class
CLR Injector - Inject .NET dlls with ease
Simple Injection - An in-depth look
MPGH's .NET SDK
eJect - Simple Injector
Basic PE Explorer (BETA)
Departure (07-11-2012)
good job.
This takes away the lag!
Nice work Jason, judging by your code, you are long time C# programmer... I am also going to try an assembly version that way it can be used as inline assembly with most programming languages without changing the syntax too much. I don't normally code in assembly because of the time it takes, but in this case I think this could be done easier in assembly because its really just moving bytes around and using basic addition, subtraction and modulus
Great job!
Contact: lucasco_10@hotmail.com
Nice man great job
i guess its gonna take a bit longer than a single 1 hour C++ to understand that
Assembly isn't *that* portable really. Sure, the assembler code itself is, but running it within various different languages is a hassle. Plus, writing in the languages own syntax can have better results (i.e JIT will architecturally optimize IL code depending on the current environment, which would won't get with static assembly code)
And yeah, been using C# for just over a year now I think, it's brilliant.
Last edited by Jason; 07-12-2012 at 08:46 AM.
You can win the rat race,Originally Posted by Jeremy S. Anderson
But you're still nothing but a fucking RAT.
++Latest Projects++
[Open Source] Injection Library
Simple PE Cipher
FilthyHooker - Simple Hooking Class
CLR Injector - Inject .NET dlls with ease
Simple Injection - An in-depth look
MPGH's .NET SDK
eJect - Simple Injector
Basic PE Explorer (BETA)
Sorry but when I said "use assembly for portability" I was not thinking of .net, I was thinking more C++ native win32 sort of thing, anyway with .net it needs the .net frame work which is hardly portable. But now that I am thinking about .net, how does .net execute shell code? can it execute shell code? I know it can call win32 library's without to much problems so I was thinking there might be a way to execute shell code with parameters in C#?
You can run shellcode in a roundabout manner. You need some WinAPI PInvoke to do it though (VirtualAlloc is about the only one).
i.e:
You can also add craps to the delegate declaration to specify calling convention and other shit:Code:private delegate uint delShellcode(); // shellcode stub, takes no params (you can add some if you need to) and returns a 32-bit value (whatever eax happens to be holding) public static uint RunShellcode(byte[] bShellcode) { IntPtr alloc = VirtualAlloc(IntPtr.Zero, bShellcode.Length, 0x1000 | 0x2000, 0x40); //MEM_COMMIT | MEM_RESERVE & PAGE_EXECUTE_READWRITE if (alloc != IntPtr.Zero) { Marshal.Copy(bShellcode, 0, alloc, bShellcode.Length); //write the shellcode to executable memory. var stub = (delShellcode)Marshal.GetDelegateForFunctionPointer(alloc, typeof(delShellcode)); return stub(); } throw new OutOfMemoryException("Unable to allocate any memory"); }
And then to call it:Code:[UnmanagedFunctionPointer(CallingConvention.StdCall)] private delegate uint delShellcode(); // shellcode stub, takes no params and returns a 32-bit value (whatever eax happens to be holding)
As for .NET, immaterial. JIT is an aspect of interpreted languages, not necessarily .NET framework. The fact remains that a lot of the common languages don't have raw asm support. With the extra time it takes to write such a cipher in ASM, you may as well just use online converters and just convert it to whatever language you need it for, then fix the mistakes :3Code:byte[] shellcode = { 0x31, 0xC0, //xor eax, eax 0x83, 0xC0, 0x01, //add eax, 1 0xC3 //ret }; uint result = RunShellcode(shellcode); Console.WriteLine(result); //should be 1
Last edited by Jason; 07-12-2012 at 12:20 PM.
You can win the rat race,Originally Posted by Jeremy S. Anderson
But you're still nothing but a fucking RAT.
++Latest Projects++
[Open Source] Injection Library
Simple PE Cipher
FilthyHooker - Simple Hooking Class
CLR Injector - Inject .NET dlls with ease
Simple Injection - An in-depth look
MPGH's .NET SDK
eJect - Simple Injector
Basic PE Explorer (BETA)
Departure (07-13-2012)
OK can you explain were to set this thing on?..... act like you are explaining it to someone who don't know anything bout this stuff....in fact don't act..: LOL