How to load an assembly using a delimiter in c #

0

Hello, if I write this question it is because I have tried everything but it has not worked. The only way possible is to drop or put the file on disk but it seemed an inefficient program so I decided to add the assembly to the executable and then start it from a single file. I raise the question here because I know that there are very good programmers and maybe someone can explain to me a little more what is my mistake. I do not advocate anything, I simply saw a program that I saw on the network and it has not been possible to optimize it by a simple delimiter. The program code that you put together is:

using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
namespace Crypt
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            //No Arguments -> Exit
            if (args.Length < 2)
            {
                Console.WriteLine("Syntax: crypter.exe <Exe/Dll to get Encrypted> <Password> (Optional: output file name)");
                Environment.Exit(0);
            }

            String file = args[0];
            String pass = args[1];
            String outFile = "Crypted.exe";

            //If Output Name is specified -> Set it
            if (args.Length == 3)
            {
                outFile = args[2];
            }

            //File doesn't exist -> Exit
            if (!File.Exists(file))
            {
                Console.WriteLine("[!] The selected File doesn't exist!");
                Environment.Exit(0);
            }

            //Everything seems fine -> Reading bytes
            Console.WriteLine("[*] Reading Data...");
            byte[] plainBytes = File.ReadAllBytes(file);

            //Yep, got bytes -> Encoding
            Console.WriteLine("[*] Encoding Data...");
            byte[] encodedBytes = encodeBytes(plainBytes, pass);

            Console.WriteLine("[*] Save to Output File... ");

            //Leer el stub
            Console.WriteLine("[*] Reading Stub...");
            byte[] Stub = File.ReadAllBytes("Stub.exe");

            //byte separador
            string strseperate = "BLAUMOLAMUCHO";
            byte[] toBytes = Encoding.ASCII.GetBytes(strseperate);
            //byte[] toBytes = new byte[30];

            //write bytes
            //var stream
            //Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("skip.skip.exe");
            //Console.WriteLine(stream);
            var s = new MemoryStream();
            s.Write(Stub, 0, Stub.Length);
            s.Write(toBytes, 0, toBytes.Length);
            s.Write(encodedBytes, 0, encodedBytes.Length);
            var b3 = s.ToArray();
            Stream stream = new MemoryStream(b3);


            //Stream stream = new MemoryStream(encodedBytes);
            FileStream fileStream = new FileStream(@"out.exe", FileMode.Create, FileAccess.Write);
            for (int i = 0; i < stream.Length; i++)
                fileStream.WriteByte((byte)stream.ReadByte());

            Console.WriteLine("Done!");

            Console.WriteLine("\n[*] File successfully encoded!");
        }
        private static byte[] encodeBytes(byte[] bytes, String pass)
        {
            byte[] XorBytes = Encoding.Unicode.GetBytes(pass);

            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] ^= XorBytes[i % XorBytes.Length];
            }

            return bytes;
        }
    }
}

The code of the program that is responsible for starting the assembly:

using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Resources;
using System.Security.Cryptography;
using System.Reflection;
using Microsoft.Win32;

namespace skip
{
    static class Program
    {
        /// <summary>
        /// MAIN
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());

            //leemos el byte array
            byte[] file = File.ReadAllBytes(System.Reflection.Assembly.GetExecutingAssembly().Location);

            //ruta appdata
            string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            //obtenemos la string
            string str = System.Text.Encoding.ASCII.GetString(file);

            //char[] delimiterChars = { 'D', 'E', 'L', 'I', 'M' , 'I', 'T', 'A', 'D', 'O', 'R'};
            char[] delimiterChars = { 'B', 'L', 'A', 'U', 'M' , 'O', 'L', 'A', 'M', 'U', 'C', 'H', 'O'};
            Console.WriteLine(delimiterChars);
            string[] arr=str.Split(new string[] { "BLAUMOLAMUCHO" }, StringSplitOptions.None);
            string a = arr[0];
            string b = arr[1];
            /*Console.WriteLine(a);
            Console.WriteLine("---------------------------");
            Console.WriteLine(b);
            Console.ReadKey();*/

            byte[] encodedBytes = Encoding.ASCII.GetBytes(b);
            //Console.WriteLine(encodedBytes);
            //var stream
            //Stream stream = new MemoryStream(encodedBytes);
            Stream stream = new MemoryStream(encodedBytes);
            FileStream fileStream = new FileStream(@"tola.exe", FileMode.Create, FileAccess.Write);
            for (int i = 0; i < stream.Length; i++)
                fileStream.WriteByte((byte)stream.ReadByte());


         }

        private static void RunInternal(string exeName, String pass)
        {

            //Verify the Payload exists
            if (!File.Exists(exeName))
                return;

            //Read the raw bytes of the file
            byte[] resourcesBuffer = File.ReadAllBytes(exeName);

            //Decrypt bytes from payload
            byte[] decryptedBuffer = null;
            decryptedBuffer = decryptBytes(resourcesBuffer, pass);



            //If .NET executable -> Run
            if (System.Text.Encoding.ASCII.GetString(decryptedBuffer).Contains("</assembly>")) //Esto devuelve false
            {
                //Load the bytes as an assembly
                Assembly exeAssembly = Assembly.Load(decryptedBuffer);

                //Execute the assembly
                object[] parameters = new object[1];                //Don't know why but fixes TargetParameterCountException

                try{
                    exeAssembly.EntryPoint.Invoke(null, parameters);
                }catch (Exception ex){
                    Console.WriteLine(ex);
                    Console.ReadKey();
                }
            }
            else
            {
                Console.WriteLine(Encoding.ASCII.GetString(decryptedBuffer));
                Console.ReadKey();
            }

        }

        /// <summary>
        /// Decrypt the Loaded Assembly Bytes
        /// </summary>
        /// <param name="payload"></param>
        /// <returns>Decrypted Bytes</returns>
        private static byte[] decryptBytes(byte[] bytes, String pass)
        {
            byte[] XorBytes = Encoding.Unicode.GetBytes(pass);

            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] ^= XorBytes[i % XorBytes.Length];
            }

            return bytes;
        }
    }
}

I have tried in many ways but the only one that has worked for me is putting it as a resource. In this way, delimiting and separating by a delimiter I have not managed to make it work.

The error that returns me:

Binary with delimiter:

MZ? ♥   ♦   ??  ?       @                                   ?   ♫▼?♫ ?  ?!?☺L?!T
his program cannot be run in DOS mode.
$       PE  L☺♥ ?4‼Y        ? ☻☺♂☺♂  ♫       N,       @    @      ☻  ♦       ♦
      ?   ☻      ♥ @?  ►  ►    ►  ►      ►           ?+  S    @   ♣
      '  ♀   ?*  ∟                                                         H
       .text   T♀       ♫   ☻                 '.rsrc    ♣   @   ♠   ►
   @  @.reloc  ♀    '   ☻   ▬              @  B                0,      H   ☻ ♣ ?
!  $      ☺   ☺  ♠                                                ‼0☻ @   ☺  ◄ (
↕
 ▬(‼
 (¶
o§
(▬

▼→(↨
♂(↑
♠o↓
(→
 (←
&*←0♥ ?   ☻  ◄ ☻(∟
‼♣◄♣-♣8?   ☻(▬

¶♂♠♥(♥  ♠♂(↑
o↓
r☺  po↔
▬?☺‼♣◄♣-9 (▲
o▼    ☺
¶       o
& ?¶‼♦ ◄♦(!
 (←
& ?   +↓ (↑
o↓
(→
 (←
& *☺►    P ↕b ¶▼  ☺‼0♣ B   ♥  ◄ ("
♥o#

        -?☻♀+*  BSJB☺ ☺     ♀   v4.0.30319    ♣ l   ?☻  #~  H♥  (♦  #Strings
p  ∟   #US ?  ►   #GUID   ?  ?☺  #Blob       ☻  ☺G§☻        ?%3 ▬  ☺       ☻   ♥
   ♦   #   ☼   ♥   ☺   ☻
 ☺     ♠ 0 ) ♠ ? h ♠ ? ? ♠ ? ? ♠ ? ? ♠ ♠☺? ♠ ▼☺? ♠ 8☺? ♠ S☺? ♠ n☺? ♠ ?☺?☺♠ ?☺?☺♠
 ?☺? ♠ ?☺? ♠ ◄☻?☺? %☻  ♠ T☻4☻♠ t☻4☻♠ ?☻)
 ?☻?☻♠ ?☻? ♠ 0♥&♥♠ B♥) _ N♥  ♠ v♥j♥♠ ?♥) ♠ ?♥) ♠ ?♥) ♠ ?♥? ♠ ?♥? ♠ ♦♦) ♠ #♦)
 ☺     ☺ ☺ ?☺► ‼ ← ♣ ☺ ☺ P     ? 7
 ☺ ?     ? < ♫ ☺ L!    ? H ¶ ♥   ☺ U   ☻ ]   ☺ b   ☻ ] ◄ ? ∟ ↓ ? ∟ ! ? ∟ ) ? ∟ 1
 ? ∟ 9 ? ∟ A ? ∟ I ? ∟ Q ? ∟ Y ? ! a ? ∟ i ? ∟ q ? ∟ y ? & ? ? , ? ? 1 ? ? 1 ? ?
☻
 ? ?☻: ? ♦♥? ? ↓♥D ? 5♥H ? \♥N ? ⌂♥T ? ?♥Y ? ?♥_ ? ?♥d ? ?♥p ? ?♥u ? ?♥z ? ?♥? ?
 ?♥? ? ?♥? ? ♫♦T ? →♦?   ? 5 . ; ☺. ‼ ? . ← ☺☺. # ☺☺. + ☺☺. 3 ? . ♂ ? . C ☺☺. S
☺☺. [ ▼☺. k I☺. s V☺. { _☺. ? h☺i ? ? ♦?  ☺             ←   ♦           ☺
♦           ☺ ?☻         <Module> skip.exe Program skip mscorlib System Object M
ain RunInternal decryptBytes exeName pass bytes System.Runtime.Versioning Target
FrameworkAttribute .ctor System.Reflection AssemblyTitleAttribute AssemblyDescri
ptionAttribute AssemblyConfigurationAttribute AssemblyCompanyAttribute AssemblyP
roductAttribute AssemblyCopyrightAttribute AssemblyTrademarkAttribute AssemblyCu
ltureAttribute System.Runtime.InteropServices ComVisibleAttribute GuidAttribute
AssemblyVersionAttribute AssemblyFileVersionAttribute System.Diagnostics Debugga
bleAttribute DebuggingModes System.Runtime.CompilerServices CompilationRelaxatio
nsAttribute RuntimeCompatibilityAttribute STAThreadAttribute System.Windows.Form
s Application EnableVisualStyles SetCompatibleTextRenderingDefault Assembly GetE
xecutingAssembly get_Location System.IO File ReadAllBytes Environment SpecialFol
der GetFolderPath System.Text Encoding get_ASCII GetString Console WriteLine Con
soleKeyInfo ReadKey Exists String Contains Load MethodInfo get_EntryPoint Method
Base Invoke Exception get_Unicode GetBytes Byte  ↨< / a s s e m b l y >     ????
=??J?▲??R???z\V↓4??♥  ☺♣ ☻☺♫♫ ☻↔♣↔♣♫♦ ☺☺♫♦ ☺☺☻♣ ☺☺◄A♦ ☺♥  ☺♦☺   ♦ ☺☺☻♦  ↕U♥  ♫♣
♠↔♣↔♣↕U↔∟↕}☻♣ ☺↔♦↔♣☻G☺ →.NETFramework,Version=v4.0☺ T♫¶FrameworkDisplayName►.NET
 Framework 4    ☺ ♦Stub  ♣☺    ↨☺ ↕Copyright ??  2017  )☺ $0f86beb5-80bf-47f1-89
e1-df778adbda88  ♀☺ 1.0.0.0☺ ☺       ▲☺ ☺ T☻▬WrapNonExceptionThrows☺     ?4‼Y
 ☻   ∟☺  ?*  ?♀  RSDS§2??♦YL?{Q‼????☺   c:\Users\Androide\Desktop\CRYPTER SIN DR
OPEAR\Stub\obj\Debug\skip.pdb

                                                             ,          >,
                    0,                _CorExeMain mscoree.dll     ?%  @





                                  ☻ ►      ?↑   8  ?              ☺ ☺   P  ?
          ☺ ☺   h  ?              ☺     ?                 ☺     ?   ?@  ?☻
    0C  ?☺          ?☻4   V S _ V E R S I O N _ I N F O     ?♦??  ☺   ☺       ☺
    ?       ♦   ☺               D   ☺ V a r F i l e I n f o     $ ♦   T r a n s
l a t i o n       ?♦?☺  ☺ S t r i n g F i l e I n f o   ?☺  ☺ 0 0 0 0 0 4 b 0
4 ♣ ☺ F i l e D e s c r i p t i o n     S t u b      ☺ F i l e V e r s i o n
 1 . 0 . 0 . 0   4       ☺ I n t e r n a l N a m e   s k i p . e x e     H ↕ ☺ L
 e g a l C o p y r i g h t   C o p y r i g h t   ?     2 0 1 7   <       ☺ O r i
 g i n a l F i l e n a m e   s k i p . e x e     , ♣ ☺ P r o d u c t N a m e
 S t u b      ☺ P r o d u c t V e r s i o n   1 . 0 . 0 . 0   8 ☺ A s s e m b l
y   V e r s i o n   1 . 0 . 0 . 0   ???<?xml version="1.0" encoding="UTF-8" stan
dalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>


                                                                          ♀   P<






                      BLAUMOLAMUCHO|Z? 0 4 5 2 ??4 ? 2 3 4 q 2 3 4 1 2 3 4 1 2 3
94 1 2 3 4 1 2 ? 4 ?▼?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♂ ?☺$ p ?☺#
 } ?☺" b ?☺! k ?☺$ P ?☺& ] ?☺" J / + K ▬ . ☻, E ?☺4 ↔ ? ♣ ∟      ▼ ) o → ↕ W ↔ ▼
 C ∟   F ▼       A → r W ↔ ⌂ I ∟ ' F ▼ i A → R ? 7?4 0 2 ?↑¶W1 2 3 ' 1 6 3 4 1 2
 3 ◄ ; 2 3 4 1<⌂oWuXe☼ _sPoFlXb2C\nGo]e2WAi@e}i\e3WFiEe2DVbAgVaPlVA@tCiPuGe4C^md
i@iVlTAFtAiVuEe2A@sQmSlKTZtXeptFrZbAtT ss@eYb]yfrRdQmPrYAGtFiSuFe3TUrVeFFAaYeFo@
krt@rXbGtV usBe_b_ywo_f[gFrUtXo\AGtFiSuFe3AGsTmPlJDQsRr[pGi[nptFrZbAtT qo^p]lPt[
o]RQlPxStZoZsptFrZbAtT ss@eYb]ybr\dAcEAFtAiVuEe2A@sQmSlKC\pMrXgZtrt@rXbGtV usBe_
b_ywo\pSnJA@tCiPuGe4RDnFi^ewo\pStZb]lXtKAGtFiSuFe3C[nBo^e↔eLe1SKsGeY.cu\tZmQ.ge@
sZoZi_g2C\nGo] br\gFa\ ay@tQm1MSi] gyBtWm↔RQf]eQtZoZ ro\s\lQKTy{nUo4.Rt]r3SMsEe_
.wiUg_oAtZcG byAtVm→RDnFi^e→I_tWr\pgeCv[cVs4SHsFe^.fu_t[mV.wo\p[lVrgeCv[cVs4DTbG
gTiZg|oVe@ UrVs2OQjQcE 'eRd⌂eH 2 3↓| T ^ _ [ ◄ e \ F ] V ↕ 4Aa @ V G B ↕ R Z H ↕
 X Q H ↕ G [ ◄ Q \ Z E [ ] A T ↕ ↔ ¶ ▼ ↕ ↔ ¶ 1 2 ◄?Zi?n?O?}??H?A?3♦¶☺1 3☺1 0☺#◄7
 5☺?♦↕☺2☻0 0☺<♣3☺%A?zoV-4??7 2☺)♫9☺3 4 1▲3 2 '☻'W@aCN[ntxQeCt]o_TZr\wG☺9☺22 4 1♀
3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 W 3 4 1 2 ♂ < 0 t Z X T v V G R @ Z D E [ \
 ; 0 t Z X T d V F B [ \ Z 1 2 ☻ → ☺ ∟ ♣  ☻
 ↔ ♠ ♥ ☺ ♥   1 2 ♂ 8 0 { ] @ T @ ] U ] | R Y T 2 p [ _ A \ X T ∟ V L T 2 q ; 0 ~
 V S P ^ p [ A K A ] V Z G 4 r ] C M C [ T \ E ↕ ☺ ♦   ♣ 3 4 ← 3 2 x T U R X e @
 R P T _ R F Z A 3 4 1 2 s 8 0 } A ] V [ ] U ] t Z X T \ R Y T 2 p [ _ A \ X T ∟
 V L T 2 ♥ < 0 b A [ U G P @ ⌂ S ^ Q 1 2 p [ _ A \ X T 2 q ; 0 b A [ U G P @ g W
 A G X ] ] 4   ∟ ♥ →  ☺   ♀ ▼   ☺  ☺ ♠ 3 4 w = 2 u B A V Y S ^ J ¶ g W A G X ] ]
 4   ∟ ♥ →  ☺   ♀ ▼   ☺  ☺ ♠ 3 4 ?C2 ?☺4 1 2 3 4 ???<♀xYl◄vWr@i[n♀"♥.♥"¶e_c]dZnS
=‼UfF▲8▬ BtSnWaXo_e☼"JeG"♫>?
>
aBsWmQlM Im^n@=▬uCsPhQmPs▼mZcFoBoTt▲c[m♂aAm↔v♣"◄mSnZfQsEVWr@i[n♀"♥.♥"
8 ‼ ¶ ◄<@eBuQsEeVPAiBi]eUe@ Lm]nA=◄uFn♂sQhVmUs∟m[cAoGoWt▼c\m♫aBm∟v "
; ↕ ‼ ¶ ◄<@eBuQsEeVEKeWuEi]n⌂eBe] ^eEeX=‼aAI]v[kTr► FiucReAs♫"Ra]sW"∟>9
◄ ↕ ‼/CeCuVs@eUP@iEiXeVeA>>
><▲aAsVmVlH>2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 ?
4 ?72 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3
4 1 2 3 4 1 2 3 4 1 2 3 4

If you are looking for a bit, you will find the delimiter.

I do not do it for nothing, it just makes me curious and I consider that goal. But you can do so with an explanation would be worth my answer (since I look for it is to understand more than anything else).

    
asked by Sergio Ramos 10.05.2017 в 15:16
source

1 answer

1

Well, in the end I took a little time and I started to try it a bit and I've already gotten it to work. In principle, how @sstan told you in a comment. Probably your problem is to convert from byte[] to string[] , the logical thing is to work only with byte . Before showing you the code, I have neglected the whole issue of encrypting using password, decrypt using it, execute in memory etc. The code is basically encrypted by adding the separator and the decryption code and then decrypt to disk.

First, the project code Crypt (only the main[] )

static void Main(string[] args)
{
    //No Arguments -> Exit
    if (args.Length < 2)
    {
        Console.WriteLine("Syntax: crypter.exe <Exe/Dll to get Encrypted> <Password> (Optional: output file name)");
        Environment.Exit(0);
    }

    String file = args[0];
    String pass = args[1];
    String outFile = "Crypted.exe";

    //If Output Name is specified -> Set it
    if (args.Length == 3)
    {
        outFile = args[2];
    }

    //File doesn't exist -> Exit
    if (!File.Exists(file))
    {
        Console.WriteLine("[!] The selected File doesn't exist!");
        Environment.Exit(0);
    }

    //Everything seems fine -> Reading bytes
    Console.WriteLine("[*] Reading Data...");
    byte[] plainBytes = File.ReadAllBytes(file);

    //Yep, got bytes -> Encoding
    Console.WriteLine("[*] Encoding Data...");
    //byte[] encodedBytes = encodeBytes(plainBytes, pass);
    byte[] encodedBytes = plainBytes;

    Console.WriteLine("[*] Save to Output File... ");

    //Leer el stub
    Console.WriteLine("[*] Reading Stub...");
    byte[] Stub = File.ReadAllBytes("skip.exe");

    //byte separador
    string strseperate = "BLAUMOLAMUCHO";
    byte[] toBytes = Encoding.ASCII.GetBytes(strseperate);

    var s = new MemoryStream();
    s.Write(Stub, 0, Stub.Length);
    s.Write(toBytes, 0, toBytes.Length);
    s.Write(encodedBytes, 0, encodedBytes.Length);
    var b3 = s.ToArray();
    Stream stream = new MemoryStream(b3);

    FileStream fileStream = new FileStream(@"out.exe", FileMode.Create, FileAccess.Write);
    for (int i = 0; i < stream.Length; i++)
        fileStream.WriteByte((byte)stream.ReadByte());

    Console.WriteLine("Done!");

    Console.WriteLine("\n[*] File successfully encoded!");
}

Decryption project code skip :

static void Main()
{
    byte[] file = File.ReadAllBytes(@"D:\Workdir\Pruebas\Encripter\Crypt\bin\Debug\out.exe");

    var position = PatternAt(file, Encoding.ASCII.GetBytes("BLAUMOLAMUCHO"));

    int longitudSeparador = Encoding.ASCII.GetBytes("BLAUMOLAMUCHO").Length;

    byte[] encodedBytes = new byte[file.Length - position.First()-longitudSeparador];
    Array.Copy(file, position.First()+ longitudSeparador, encodedBytes, 0, file.Length - position.First()-longitudSeparador);

    FileStream fileStream = new FileStream(@"tola.exe", FileMode.Create, FileAccess.Write);
    for (int i = 0; i < encodedBytes.Length; i++)
        fileStream.WriteByte(encodedBytes[i]);
}

public static IEnumerable<int> PatternAt(byte[] source, byte[] pattern)
{
    for (int i = 0; i < source.Length; i++)
    {
        if (source.Skip(i).Take(pattern.Length).SequenceEqual(pattern))
        {
            yield return i;
        }
    }
}

With this in my tests when executing the "encrypted" file, the original is correctly generated and is perfectly executable. I hope you get a clue how to do it.

    
answered by 11.05.2017 / 17:45
source