Advertisement
Guest User

Powershell Steganography Module

a guest
Apr 10th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Get-ConfigInfo {
  2.     $cfg = 0 | select TempFolder
  3.     $cfg.TempFolder = join-path $env:temp "Steganographer"
  4.     mkdir $cfg.TempFolder -force -ErrorAction SilentlyContinue | Out-Null
  5.     $cfg
  6. }
  7.  
  8. Function Add-SteganographyType {
  9. $code =@"
  10. using System;
  11. using System.IO;
  12. using System.Drawing;
  13. using System.Collections.Generic;
  14. using System.Text;
  15. using System.Drawing.Imaging;
  16.  
  17. namespace Neptune
  18. {
  19.    class BitmapHelper
  20.    {
  21.        
  22.        public static  byte [] getBytes(Bitmap imgSource)
  23.        {
  24.  
  25.            MemoryStream ms = new MemoryStream();
  26.            imgSource.Save(ms, ImageFormat.Bmp);
  27.            return ms.ToArray();
  28.        }
  29.        public static Bitmap getBitmap(byte [] arrData){
  30.            MemoryStream ms = new MemoryStream(arrData);
  31.  
  32.            Bitmap bmp = new Bitmap(ms);
  33.          
  34.            return bmp;
  35.        }
  36.  
  37.    }
  38.    public class Steganographer
  39.    {
  40.        public static string ExtractText(Bitmap bmp)
  41.        {
  42.            byte[] data = BitmapHelper.getBytes(bmp);
  43.            StringBuilder builder=new StringBuilder();
  44.            int i = 54;
  45.            int argb = 0;
  46.            byte c;
  47.            while (true)
  48.            {
  49.                c = 0;
  50.                int j = 0;
  51.                while(j<8)
  52.                {
  53.                    if ((argb == 3))
  54.                    {
  55.                        // Skip the alpha byte
  56.                        argb = ((argb + 1) % 4);
  57.                        i++;
  58.                    }
  59.                    
  60.                        byte temp = data[i];
  61.                        temp &= 0x01;
  62.                        temp = (byte)(temp << j);
  63.                        c |= temp;
  64.                        argb = ((argb + 1) % 4);
  65.                        i++;
  66.                        j++;
  67.  
  68.                    
  69.                }
  70.                // Character is completed
  71.                if (c == 0)
  72.                    break;
  73.                else
  74.                    builder.Append((char)c);
  75.  
  76.            }
  77.            return builder.ToString();
  78.        }
  79.  
  80.        public static Bitmap HideText(Bitmap imgSource, String txtMessage)
  81.        {
  82.            byte [] data=BitmapHelper.getBytes(imgSource);
  83.            // Strip the least significant bit
  84.            int i;
  85.            for ( i = 54; i < data.Length; i++)
  86.            {
  87.  
  88.                data[i] &= 0xfe;
  89.            }
  90.  
  91.             i = 54;
  92.             int argb = 0;
  93.            foreach(char c in txtMessage.ToCharArray()){
  94.              
  95.                char ch = c;
  96.                
  97.                for (int b = 0; b < 8; b++)
  98.                {
  99.                    if (argb == 3)
  100.                    {
  101.                        // Skip the Alpha byte
  102.                        i++;
  103.                        argb = ((argb + 1) % 4);
  104.                    }
  105.                    data[i++] |= (byte)(ch & 0x01);
  106.                    ch = (char)(ch >> 1);
  107.                    argb = ((argb + 1) % 4);
  108.                    
  109.                }
  110.  
  111.            }
  112.          
  113.            return BitmapHelper.getBitmap(data);
  114.              
  115.  
  116.        }
  117.    }
  118. }
  119.  
  120. "@
  121.     try {
  122.         [void][Neptune.Steganographer]
  123.     } catch {
  124.         add-type -TypeDefinition $code -ReferencedAssemblies "System.Drawing","mscorlib" | Out-Null
  125.     }
  126.     add-type -AssemblyName "System.Drawing" -ErrorAction SilentlyContinue| Out-Null
  127. }
  128. Add-SteganographyType
  129. Function  Get-SteganographyMessage {
  130.     [CmdletBinding(DefaultParameterSetName="URL")]
  131.     param(
  132.         [Parameter(Mandatory=$true,ParameterSetName="URL", ValueFromPipeline=$true)]
  133.         [uri]$URL,
  134.         [Parameter(Mandatory=$true,ParameterSetName="Path")]
  135.         [string]$Path
  136.     )
  137.     $cfg = Get-ConfigInfo
  138.     if($PSCmdlet.ParameterSetName -eq "URL"){
  139.         $wc = New-Object System.Net.WebClient
  140.         $imgFile = Join-Path $cfg.TempFolder $($URL.Segments | select -Last 1)
  141.         $wc.DownloadFile($URL.AbsoluteUri,$imgFile)
  142.         $bitmap = [System.Drawing.Bitmap]::FromFile($imgFile)
  143.         [Neptune.Steganographer]::ExtractText($bitmap)
  144.         $bitmap.Dispose()
  145.         Remove-Item $imgFile
  146.     } else {
  147.         if(Test-Path $Path) {
  148.             [io.fileinfo]$Path = Get-ChildItem $Path
  149.         } else {
  150.             throw "Can't find file $Path"
  151.         }    
  152.         $bitmap = [System.Drawing.Bitmap]::FromFile($Path.FullName)
  153.         [Neptune.Steganographer]::ExtractText($bitmap)
  154.     }
  155. }
  156.  
  157. Function  Set-SteganographyMessage {
  158.     [CmdletBinding(DefaultParameterSetName="URL")]
  159.     param(
  160.         [Parameter(Mandatory=$true,ParameterSetName="URL", ValueFromPipeline=$true)]
  161.         [uri]$URL,
  162.         [Parameter(Mandatory=$true,ParameterSetName="Path")]
  163.         [string]$Path,
  164.         [Parameter(Mandatory=$true)]
  165.         [string]$Message,
  166.         [Parameter(Mandatory=$true)]
  167.         [string]$TargetPath,
  168.         [switch]$Force
  169.     )    
  170.     $cfg = Get-ConfigInfo
  171.     if($PSCmdlet.ParameterSetName -eq "URL"){
  172.         $wc = New-Object System.Net.WebClient
  173.         $filename = $($URL.Segments | select -Last 1)
  174.         [io.fileinfo]$Path = Join-Path $cfg.TempFolder $filename
  175.         $wc.DownloadFile($URL.AbsoluteUri,$Path.FullName)
  176.     } else {
  177.         if(Test-Path $Path) {
  178.             [io.fileinfo]$Path = Get-ChildItem $Path
  179.         } else {
  180.             throw "Can't find file $Path"
  181.         }    
  182.     }
  183.     if(Test-Path $TargetPath) {
  184.         if($Force) {
  185.             Remove-Item $TargetPath
  186.         } else {
  187.             throw "File $TargetPath already exist. Try overwriting it with -Force or specify another -TargetPath"
  188.         }
  189.     }
  190.     $bitmap = [System.Drawing.Bitmap]::FromFile($Path.FullName)
  191.     $outBitmap = [Neptune.Steganographer]::HideText($bitmap,$Message)
  192.     $targetFolder = resolve-path (split-path (join-path ".\" $TargetPath))
  193.     $targetFile = Split-Path -Leaf $TargetPath
  194.     $TargetPath = Join-Path $targetFolder $targetFile
  195.     $outBitmap.Save($TargetPath)
  196.     $bitmap.Dispose()
  197.     $outBitmap.Dispose()
  198.     Get-ChildItem $TargetPath
  199. }
  200.  
  201. Export-ModuleMember -Function "Get-SteganographyMessage","Set-SteganographyMessage"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement