用ASP.NET实现网上“追捕”
用ASP.NET实现网上“追捕”
我们可以在ASP.Net中通过使用Sockets Class来对Internet上的主机进行远程的控制和探测。
下面的例子,可以对指定的主机进行探测,其功能类似追捕。
<%@ Import Namespace="System.Net" %>
<% @Import Namespace="System.Net.Sockets" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat=server>
protected void Page_Load(Object Src, EventArgs E){
TCPClient tcpc = new TCPClient();
String host="host";
showmsg.Text=TcpConnect(tcpc,host,25); //SMTP端口
/*
showmsg.Text=TcpConnect(tcpc,host,80); //WWW端口
showmsg.Text=TcpConnect(tcpc,host,21); //FTP端口
showmsg.Text=TcpConnect(tcpc,host,110); //POP端口
showmsg.Text=TcpConnect(tcpc,host,1080); //Socket5 端口,代理服务器使用的端口
showmsg.Text=TcpConnect(tcpc,host,53); //DNS端口
*/
}
String TcpConnect(TCPClient tcpc,String host,int port){
StreamReader sr ;
String strRet="123";
if(0 == tcpc.Connect(host,port)){
//连接服务器成功
sr = new StreamReader(tcpc.GetStream(), Encoding.Default);
strRet=sr.ReadLine();
}
return strRet;
}
</script>
<html>
<head>
<title>网络追捕</title>
<link rel="stylesheet" type="text/css" href="/doufu.css">
</head>
<body>
<asp:Label id=showmsg runat=server />
</body>
</html>
前面的程序,其实是很简单的。这个程序现在还是有些小问题,比如在处理通过IP地址查找主机域名的时候,如果输入的不是合法的IP地址,则程序会返回一个错误,大家如果有兴趣,可以自己修改修改这个程序的。
<%@ Import NameSpace="System.Net" %>
<% @Import Namespace="System.Net.Sockets" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat=server>
String strRet;
protected void doClick(Object Src, EventArgs E){
if(dropdown1.SelectedItem.Value.ToInt16()==2){
IPHostEntry hostInfo = DNS.GetHostByName(txtSearch.Text);
strRet= hostInfo.AddressList[0].ToString() + "<br>"; //域名转换成IP地址
}
else{
IPHostEntry hostInfo = DNS.GetHostByAddr(txtSearch.Text);
strRet= hostInfo.Hostname + "<br>"; //IP地址转换成域名
}
if(CheckBox1.Checked){
String host=txtSearch.Text;
TCPClient tcpc = new TCPClient();
strRet=strRet + "SMTP Server:" + TcpConnect(tcpc,host,25) +
"<br>"; //SMTP端口
tcpc = new TCPClient();
strRet=strRet + "WWW Server:" + TcpConnect(tcpc,host,80) + "<br>";
//WWW端口
tcpc = new TCPClient();
strRet=strRet + "FTP Server: " + TcpConnect(tcpc,host,21) +
"<br>"; //FTP端口
tcpc = new TCPClient();
strRet=strRet + "Pop3 Server:" + TcpConnect(tcpc,host,110) +
"<br>"; //POP端口
tcpc = new TCPClient();
strRet=strRet + "代理 Server:" + TcpConnect(tcpc,host,1080) +
"<br>"; //Socket5端口,代理服务器使用的端口
tcpc = new TCPClient();
strRet=strRet + "DNS Server:" + TcpConnect(tcpc,host,53) + "<br>";
//DNS端口
}
showmsg.Text=strRet;
}
String TcpConnect(TCPClient tcpc,String host,int port){
//这个函数的功能是检查指定的主机的port是否在用
String strRet="服务没有找到";
if(0 == tcpc.Connect(host,port)){
//连接服务器成功
strRet="正在运行";
}
return strRet;
}
</script>
<html>
<head>
<title>WEB追捕</title>
<SCRIPT LANGUAGE="JScript">
//这两个函数是功能是对剪贴板进行存取
function doufucopy() {
textRange = txtSearch.createTextRange();
textRange.execCommand("Copy");
}
function doufupaste() {
textRange = txtSearch.createTextRange();
textRange.execCommand("Paste");
}
-->
</SCRIPT>
</head>
<body>
<form id=testForm runat=server>
<asp:DropDownList id="dropdown1" runat="server">
<asp:ListItem ID=ListItem1 Value=1>查找域名</asp:ListItem>
<asp:ListItem ID=ListItem2 Value=2>查找IP</asp:ListItem>
</asp:DropDownList>
<asp:TextBox runat=server id=txtSearch />
<br>
<asp:CheckBox id="CheckBox1" runat="server" Text="检查对方的机器"
/>
<br>
<asp:Button runat=server id=do Text="查找" onClick=doClick
/>
</form>
<asp:Label id=showmsg runat=server />
<input type=button value="复制" onclick="doufucopy();">
<input type=button value="粘贴" onclick="doufucopy();">
</body>
</html>
(豆腐)