Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Get-ConfigInfo {
- $cfg = 0 | select TempFolder
- $cfg.TempFolder = join-path $env:temp "Steganographer"
- mkdir $cfg.TempFolder -force -ErrorAction SilentlyContinue | Out-Null
- $cfg
- }
- Function Add-SteganographyType {
- $code =@"
- using System;
- using System.IO;
- using System.Drawing;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing.Imaging;
- namespace Neptune
- {
- class BitmapHelper
- {
- public static byte [] getBytes(Bitmap imgSource)
- {
- MemoryStream ms = new MemoryStream();
- imgSource.Save(ms, ImageFormat.Bmp);
- return ms.ToArray();
- }
- public static Bitmap getBitmap(byte [] arrData){
- MemoryStream ms = new MemoryStream(arrData);
- Bitmap bmp = new Bitmap(ms);
- return bmp;
- }
- }
- public class Steganographer
- {
- public static string ExtractText(Bitmap bmp)
- {
- byte[] data = BitmapHelper.getBytes(bmp);
- StringBuilder builder=new StringBuilder();
- int i = 54;
- int argb = 0;
- byte c;
- while (true)
- {
- c = 0;
- int j = 0;
- while(j<8)
- {
- if ((argb == 3))
- {
- // Skip the alpha byte
- argb = ((argb + 1) % 4);
- i++;
- }
- byte temp = data[i];
- temp &= 0x01;
- temp = (byte)(temp << j);
- c |= temp;
- argb = ((argb + 1) % 4);
- i++;
- j++;
- }
- // Character is completed
- if (c == 0)
- break;
- else
- builder.Append((char)c);
- }
- return builder.ToString();
- }
- public static Bitmap HideText(Bitmap imgSource, String txtMessage)
- {
- byte [] data=BitmapHelper.getBytes(imgSource);
- // Strip the least significant bit
- int i;
- for ( i = 54; i < data.Length; i++)
- {
- data[i] &= 0xfe;
- }
- i = 54;
- int argb = 0;
- foreach(char c in txtMessage.ToCharArray()){
- char ch = c;
- for (int b = 0; b < 8; b++)
- {
- if (argb == 3)
- {
- // Skip the Alpha byte
- i++;
- argb = ((argb + 1) % 4);
- }
- data[i++] |= (byte)(ch & 0x01);
- ch = (char)(ch >> 1);
- argb = ((argb + 1) % 4);
- }
- }
- return BitmapHelper.getBitmap(data);
- }
- }
- }
- "@
- try {
- [void][Neptune.Steganographer]
- } catch {
- add-type -TypeDefinition $code -ReferencedAssemblies "System.Drawing","mscorlib" | Out-Null
- }
- add-type -AssemblyName "System.Drawing" -ErrorAction SilentlyContinue| Out-Null
- }
- Add-SteganographyType
- Function Get-SteganographyMessage {
- [CmdletBinding(DefaultParameterSetName="URL")]
- param(
- [Parameter(Mandatory=$true,ParameterSetName="URL", ValueFromPipeline=$true)]
- [uri]$URL,
- [Parameter(Mandatory=$true,ParameterSetName="Path")]
- [string]$Path
- )
- $cfg = Get-ConfigInfo
- if($PSCmdlet.ParameterSetName -eq "URL"){
- $wc = New-Object System.Net.WebClient
- $imgFile = Join-Path $cfg.TempFolder $($URL.Segments | select -Last 1)
- $wc.DownloadFile($URL.AbsoluteUri,$imgFile)
- $bitmap = [System.Drawing.Bitmap]::FromFile($imgFile)
- [Neptune.Steganographer]::ExtractText($bitmap)
- $bitmap.Dispose()
- Remove-Item $imgFile
- } else {
- if(Test-Path $Path) {
- [io.fileinfo]$Path = Get-ChildItem $Path
- } else {
- throw "Can't find file $Path"
- }
- $bitmap = [System.Drawing.Bitmap]::FromFile($Path.FullName)
- [Neptune.Steganographer]::ExtractText($bitmap)
- }
- }
- Function Set-SteganographyMessage {
- [CmdletBinding(DefaultParameterSetName="URL")]
- param(
- [Parameter(Mandatory=$true,ParameterSetName="URL", ValueFromPipeline=$true)]
- [uri]$URL,
- [Parameter(Mandatory=$true,ParameterSetName="Path")]
- [string]$Path,
- [Parameter(Mandatory=$true)]
- [string]$Message,
- [Parameter(Mandatory=$true)]
- [string]$TargetPath,
- [switch]$Force
- )
- $cfg = Get-ConfigInfo
- if($PSCmdlet.ParameterSetName -eq "URL"){
- $wc = New-Object System.Net.WebClient
- $filename = $($URL.Segments | select -Last 1)
- [io.fileinfo]$Path = Join-Path $cfg.TempFolder $filename
- $wc.DownloadFile($URL.AbsoluteUri,$Path.FullName)
- } else {
- if(Test-Path $Path) {
- [io.fileinfo]$Path = Get-ChildItem $Path
- } else {
- throw "Can't find file $Path"
- }
- }
- if(Test-Path $TargetPath) {
- if($Force) {
- Remove-Item $TargetPath
- } else {
- throw "File $TargetPath already exist. Try overwriting it with -Force or specify another -TargetPath"
- }
- }
- $bitmap = [System.Drawing.Bitmap]::FromFile($Path.FullName)
- $outBitmap = [Neptune.Steganographer]::HideText($bitmap,$Message)
- $targetFolder = resolve-path (split-path (join-path ".\" $TargetPath))
- $targetFile = Split-Path -Leaf $TargetPath
- $TargetPath = Join-Path $targetFolder $targetFile
- $outBitmap.Save($TargetPath)
- $bitmap.Dispose()
- $outBitmap.Dispose()
- Get-ChildItem $TargetPath
- }
- Export-ModuleMember -Function "Get-SteganographyMessage","Set-SteganographyMessage"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement