Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm currently learning about crypters and this is what I've learned so far.

A crypter consists of a builder and a stub.

The builders role is to encrypt a file and the stub wraps the file and makes it run in a buffer aka in the memory of the machine it is being decrypted on. (Please do correct if I am wrong)

I have created my File Encrypter (The builder) and to be honest I have no idea how to create a stub.. I've been looking around the entire day but all I can find are these really old Console applications not explaining anything really..

So my question is.. How do I wrap my current file encrypter with a stub.. or how do I create a stub. Not to sure how to form that question since I am new to stubs.

Here is my file encrypter.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows;
using Microsoft.Win32;
using System.Security.Cryptography;
using System.IO;

namespace Encrypter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string key;
        public MainWindow()
        {
            InitializeComponent();
            key = generateKey();
        }

        public string generateKey()
        {
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
        }

        private void EncryptBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.ShowDialog();

                inputencryptFileTextBox.Text = ofd.FileName;

                SaveFileDialog sfd = new SaveFileDialog();
                sfd.ShowDialog();

                outputencryptFileTextBox.Text = sfd.FileName;

                encrypt(inputencryptFileTextBox.Text, outputencryptFileTextBox.Text, key);
                MessageBox.Show("File has been encrypted.", "File");

            }
            catch(Exception encEx)
            {
                MessageBox.Show(encEx.ToString());
            }
            
        }

        private void encrypt(string input, string output, string strhash)
        {
            FileStream inFs, outFs;
            CryptoStream cs;
            TripleDESCryptoServiceProvider TDC = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] byteHash, byteTexto;

            inFs = new FileStream(input, FileMode.Open, FileAccess.Read);
            outFs = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write);

            byteHash = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strhash));
            byteTexto = File.ReadAllBytes(input);

            md5.Clear();

            TDC.Key = byteHash;
            TDC.Mode = CipherMode.ECB;

            cs = new CryptoStream(outFs, TDC.CreateEncryptor(), CryptoStreamMode.Write);

            int byteRead;
            long length, position = 0;
            length = inFs.Length;

            while (position < length)
            {
                byteRead = inFs.Read(byteTexto, 0, byteTexto.Length);
                position += byteRead;

                cs.Write(byteTexto, 0, byteRead);

            }

            inFs.Close();
            outFs.Close();

        }

        private void DecryptBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.ShowDialog();

                inputdecryptFileTextBox.Text = ofd.FileName;

                SaveFileDialog sfd = new SaveFileDialog();
                sfd.ShowDialog();

                outputdecryptFileTextBox.Text = sfd.FileName;

                decrypt(inputdecryptFileTextBox.Text, outputdecryptFileTextBox.Text, key);
                MessageBox.Show("File has been decrypted.", "File");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
   


        private void decrypt(string input, string output, string strhash)
        {
            FileStream inFs, outFs;
            CryptoStream cs;
            TripleDESCryptoServiceProvider TDC = new TripleDESCryptoServiceProvider();
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] byteHash, byteTexto;

            inFs = new FileStream(input, FileMode.Open, FileAccess.Read);
            outFs = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write);

            byteHash = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strhash));
            byteTexto = File.ReadAllBytes(input);

            md5.Clear();

            TDC.Key = byteHash;
            TDC.Mode = CipherMode.ECB;

            cs = new CryptoStream(outFs, TDC.CreateDecryptor(), CryptoStreamMode.Write);

            int byteRead;
            long length, position = 0;
            length = inFs.Length;

            while (position < length)
            {
                byteRead = inFs.Read(byteTexto, 0, byteTexto.Length);
                position += byteRead;

                cs.Write(byteTexto, 0, byteRead);

            }

            inFs.Close();
            outFs.Close();

        }
    }
}


What I have tried:

I've tried looking over some examples on Google and some forums but all I could find was these old Console Applications from 2009.
Posted
Updated 23-Sep-16 16:14pm

1 solution

Well, in the code you've posted, the 'builder' and 'stub' are basically combined into the encrypt and decrypt functions themselves - they are using 'filestreams' for example which is as close you'll get to 'memory' in your example - hey, if they work, thats fine

One way to get closer to a builder/stub solution as you put it, would be for the encrypt/decrypt functions to accept (for example) a byte array only .. so the work of the builder is to get a byte array - from a file for example, then present the byte array to the stub, with encrypts it/decrypts it

not sure I like the definition of 'builder' and 'stub' like that anyway - what I would do, is break down the processing even further, so that you could specify what type of encryption you wish to perform on a byte array - TripleDES as you're using here or 'AES' or such - so you might have a TripleDES encrypt class - really, it could accept a FileStream, or a ByteArray, or a MemoryStream for example

not sure if any of that waffle helps
 
Share this answer
 
Comments
Goober S. Johnsson 23-Sep-16 22:17pm    
It did help to be honest but this is what I've gotten ti down to

Stub - is a box that holds The encrypted payload > decrypts it and then uses RunPE
To shoot the decrypted payload into memory

RunPE - Makes sure to inject the payload into memory of the chosen process.

Encrypt the payload with the builder
cover that payload with a stub
the stub contains RunPE which will make sure that the payload
in the stub get shot into memory

Now see..
I have no idea how it should be built.. Any chance you could help me getting the base up for it?
Goober S. Johnsson 23-Sep-16 22:20pm    
Im assuming I will create it like this..

A builder that basically encrypts a file and adds a runPE to it
then it adds the stub to it
then saves the file?
Garth J Lancaster 23-Sep-16 23:47pm    
I would have been happy to give advice on things like 'self-decrypting files' - although my first advice would be, 'dont - the security world is heading away from such things, for a whole lot of reasons' .. that you've added 'RunPE' & 'inject the payload into memory of the chosen process' disturbs me - as someone who's worked in the EDI/finance/banking sectors it wouldnt be worth my name to help someone put such a process together, because the chance for abuse is mightly hi - so, sorry, no
Goober S. Johnsson 24-Sep-16 7:42am    
I totally understand I respect that decision!
I know it sounds weird and that I might use it for "bad things"
the the reality of it is that I am just trying to learn more about the subject and the best way for me to learn is to get my hand dirty in it.
And I haven't added a RunPE or antyhing, I was just explaining how I thought it was built :p

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900