多线程资源访问问题

godzeye 2006-09-06 12:29:15
我程序中有两个arraylist,一个用于贮存用户名,一个用于贮存密码。
(一个用于暴力猜解ftp帐号的简单程序)

现在我要做的工作是把用户名和密码的组合都尝试完毕。
问题是多线程的时候怎么从那两个arraylist中取数据,不能重复,多线程又不会发生资源争用。
...全文
214 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
GXY2005 2006-09-07
  • 打赏
  • 举报
回复
晕楼上真牛人..
----------------------
廣告滿天飛,這樣可以掙錢是吧!
godzeye 2006-09-07
  • 打赏
  • 举报
回复
this._action._loader="results|"+host+"|ftp ("+plugin._port.ToString()+"/tcp)|NOTE|;神眼网络安全扫描系统成功检测到一个ftp登陆帐户----用户名:"+login+" 密码:"+pass+";;Risk factor : Low;NESSUS_ID : 100882";
this.action_from_WorkThread(this,this._action);
//
if(plugin._isLimitCrack)
{
goto exit;
}
(*_intCount)++;
(* _intPasswd)++;

break;
case 6:
this._action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
this._action._loader="FtpCrack一线程遇到强制退出破解代码的情况,这可能上由于远端运行的并非ftp服务,或者服务关闭等,线程正在退出....\n";
this.action_from_WorkThread(this,this._action);

goto exit;
default:
this._action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
this._action._loader="FtpCrack一线程遇到未知退出返回值,相关线程正在退出....\n";
this.action_from_WorkThread(this,this._action);

goto exit;
}
//关闭socket
GodzEyeLib.CommonFunction.hydra_disconnect(socket);
}
exit:
// *_current_thread_count=(*_current_thread_count)-1;
//
this._action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
this._action._loader=host+":"+Thread.CurrentThread.Name+"线程执行FtpCrack插件完毕,正在退出....\n";
this.action_from_WorkThread(this,this._action);
}


private string GetNextLogin
{
get
{
// lock(plugin._UserName)
// {
if(*_intCount%plugin._PassWd.Count==0)
return (string)plugin._UserName[((int)(*_intCount/plugin._PassWd.Count))-1];
return (string)plugin._UserName[((int)(*_intCount/ plugin._PassWd.Count))];
// }
}
}
private string GetNextPassWd
{
get
{
// lock(plugin._PassWd)
// {
if(*_intCount==plugin._UserName.Count*plugin._PassWd.Count)
return null;
if(*_intPasswd==plugin._PassWd.Count)
*_intPasswd=1;
return (string)plugin._PassWd[(*_intPasswd)-1];
// }
}
}

[DllImport("FTP.dll", CharSet=CharSet.Ansi)]
public static extern int start_ftp(int socket,string login,string pass,string options);
}
}
godzeye 2006-09-07
  • 打赏
  • 举报
回复

// //
// for(int i=0;i<work_threads.Length;i++)
// {
// try
// {
// WorkThread work=new WorkThread();
// work.host=addr.ToString();
// work.plugin=this;
// work._intCount=&intCount;
// work._intPasswd=&Passwd;
// // work._current_thread_count=¤t_thread_count;
//
// work.action_from_WorkThread+=new FtpCrack.WorkThread.ActionedFromWorkThread(work_action_from_WorkThread);
// //
// work_threads[i]=new Thread(new ThreadStart(work.service_ftp));
// work_threads[i].Name=addr.ToString()+":线程"+i.ToString();
// work_threads[i].IsBackground=true;
// work_threads[i].Start();
// // current_thread_count++;
// }
// catch{}
// //告知启动一个新的扫描线程。
// this._action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
// this._action._loader="正在启动新的扫描"+work_threads[i].Name+"检测ftp若口令....\n";
// this.action_from_plugin(this,this._action);
// Thread.Sleep(100);
// }

// int count=0;
// while(true)
// {
// count=0;
// Thread.Sleep(500);
// for(int k=0;k<work_threads.Length;k++)
// {
// if(work_threads[k]==null)
// {
// count++;
// continue;
// }
// try
// {
//// switch(work_threads[k].ThreadState)
//// {
//// case System.Threading.ThreadState.Aborted:
//// break;
//// default:
//// break;
//// }
//
// if(work_threads[k].ThreadState!=System.Threading.ThreadState.Running)
// {
// work_threads[k].Abort();
// work_threads[k].Join();
// work_threads[k]=null;
// }
// }
// catch(Exception e)
// {
// GodzEyeLib.Log.WriteLog(GodzEyeLib.LogType.Error,"{0}---,{1}---,{2}",e.Message,e.Source,e.StackTrace);
// }
// }
// if(count==work_threads.Length)
// break;
// }

/*==========单线程处理=========*/
WorkThread work=new WorkThread();
work.host=addr.ToString();
work.plugin=this;
work._intCount=&intCount;
work._intPasswd=&Passwd;

//work._current_thread_count=¤t_thread_count;

work.action_from_WorkThread+=new FtpCrack.WorkThread.ActionedFromWorkThread(work_action_from_WorkThread);
//// Thread t=new Thread(new ThreadStart(work.service_ftp));
//// t.Start();
work.service_ftp();
/*==========单线程处理=========*/
}

#endregion


private void work_action_from_WorkThread(object sender, Action action)
{
this.action_from_plugin(this,action);

}
}


/// <summary>
///
/// </summary>
public unsafe class WorkThread
{
public int * _intCount;
public int * _intPasswd;

//public int * _current_thread_count;
public string host;
// 自身插件的实例。
public FtpCrack.PluginClass plugin;
//声明委托。
public delegate void ActionedFromWorkThread(object sender,Action action);
public event ActionedFromWorkThread action_from_WorkThread;
//消息。
private GodzEyeLib.Action _action=new Action();

/*返回数字含义:
* 1.可以继续实施破击
* 2.连接断开,需要重新连接。
* 3.协议不需要密码验证
* 4.协议发生错误
* 5.破解一密码成功
* 6.强制退出密码破解过程
* */
public void service_ftp()
{

while(true)
{
int socket=GodzEyeLib.CommonFunction.hydra_connect_tcp(host,plugin._port,2000);
if(socket==-1)
{
this._action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
this._action._loader=host+":"+plugin._port.ToString()+"端口无法连接,正在退出检测过程....\n";
this.action_from_WorkThread(this,this._action);
goto exit;
}

//判断密码是否使用完了。
if(this.GetNextPassWd==null)
{
this._action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
this._action._loader=host+Thread.CurrentThread.Name+"在ftp若口令猜测上,由于密码字典使用完,线程正在退出....\n";
this.action_from_WorkThread(this,this._action);

goto exit;
}

//保存当前的用户名和密码
string login=this.GetNextLogin;
string pass=this.GetNextPassWd;
//是否详细输出提示信息。
if(plugin._isVerbose)
{
this._action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
this._action._loader=host+" "+Thread.CurrentThread.Name+" :"+plugin._port.ToString()+"端口,正在尝试-----"+login+":"+pass+"密码组合....\n";
this.action_from_WorkThread(this,this._action);
}

//破解执行。
int rtn=WorkThread.start_ftp(socket,login,pass,null);
switch(rtn)
{
case 1:
(*_intCount)++;
(* _intPasswd)++;
break;
case 2:
break;
case 3:
//提示.
this._action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
this._action._loader=host+": FTP服务器不需要密码验证!\n";
this.action_from_WorkThread(this,this._action);
//漏洞记录.
this._action._actionType=GodzEyeLib.ActionType.Action_Vul_Write;
this._action._loader="results|"+host+"|ftp ("+plugin._port.ToString()+"/tcp)|NOTE|;该ftp服务器不需要密码验证。;;Risk factor : Low;NESSUS_ID : 100882";
this.action_from_WorkThread(this,this._action);

goto exit;
case 4:
this._action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
this._action._loader="FtpCrack检测到"+host+" "+plugin._port.ToString()+"端口上运行的并非ftp服务或者远程正在积极阻止做出安全相应,破解无法继续,线程正在退出....\n";
this.action_from_WorkThread(this,this._action);

goto exit;
case 5:
//提示.
this._action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
this._action._loader=host+": 神眼网络安全扫描系统成功检测到一个ftp登陆帐户----用户名:"+login+" 密码:"+pass+"\n";
this.action_from_WorkThread(this,this._action);
//漏洞记录.
this._action._actionType=GodzEyeLib.ActionType.Action_Vul_Write;
godzeye 2006-09-07
  • 打赏
  • 举报
回复
/// <summary>
/// (C)2005-2006 godzeye 保留所有权利
///
/// 文件名称:PluginClass.cs
/// 文件ID:
/// 文件说明:
/// FTP 协议密码破解插件。
///
/// 测试:
///
/// 当前版本: 1.0
///
/// 作者: godzeye
/// 创建日期: 2006-9-1
/// 最后修改日期: 2006-9-1
///
/// 历史修改记录:
///
/// </summary>
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using GodzEyeLib;


namespace FtpCrack
{
/// <summary>
/// FTP 协议密码破解插件。
/// </summary>
public class PluginClass:GodzEyeLib.IPlugin
{
public PluginClass()
{
}

public event GodzEyeLib.ActionedFromPlugins action_from_plugin;

#region 变量
//用户自定义端口
public int _port=21;
//破解线程数量
public int _threadCount;
//是否显示详细破解信息
public bool _isVerbose;
//是否为无限制破解
public bool _isLimitCrack;
//连接失败尝试次数
public int _faileTryCount;
//用户名字典所在路径
private string _userNameFilePath;
//密码字典路径
private string _passwdFilePath;
//以下参数为用户名、密码所设
public ArrayList _UserName;
public ArrayList _PassWd;
//用于报告插件消息。
public GodzEyeLib.Action _action=new Action();

#endregion

#region IPlugin接口实现
public string PluginVersion
{
get
{
return "1.0.0";
}
}

public string PluginAuthor
{
get
{
return "godzeye";
}
}

public string PluginDescription
{
get
{
return "该插件用于检测ftp的弱口令,它包括一系列稀有的特性,可以更有效地对ftp服务器进行密码破解探测";
}
}

public GodzEyeLib.PluginType PluginScope
{
get
{
return GodzEyeLib.PluginType.PassWord_Crack;
}
}
public string PluginName
{
get
{
return "FtpCrack";
}
}

public void InitPlugin()
{
this._UserName=new ArrayList();
this._PassWd=new ArrayList();
//
if(!File.Exists(@"data\FtpCrack.xml"))
{
throw new GodzEyeException("FtpCrack插件配置文件不存在!");
}
else
{
_action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
_action._loader="FtpCrack插件正在读取插件配置.......\n";
this.action_from_plugin(this,_action);
//
GodzEyeLib.ConfigReader cr=new GodzEyeLib.ConfigReader(@"./data/FtpCrack.xml");
this._port=int.Parse(cr.GetValue("port"));
this._threadCount=int.Parse(cr.GetValue("threadCount"));

if(cr.GetValue("isVerbose")=="false")
{
this._isVerbose=false;
}
else
{
this._isVerbose=true;
}
if(cr.GetValue("isLimitCrack")=="true")
{
this._isLimitCrack=true;
}
else
{
this._isLimitCrack=false;
}
this._faileTryCount=int.Parse(cr.GetValue("faileTryCount"));
this._userNameFilePath=cr.GetValue("userNameFilePath");
this._passwdFilePath=cr.GetValue("passwdFilePath");

//
_action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
_action._loader="FtpCrack插件正在把用户名和密码字典载入内存......\n";
this.action_from_plugin(this,_action);

if(File.Exists(this._userNameFilePath)&&File.Exists(this._passwdFilePath))
{
using (StreamReader sr = new StreamReader(this._userNameFilePath))
{
while (sr.Peek() >= 0)
{
string strTmp=sr.ReadLine();
this._UserName.Add(strTmp);
}
}

using (StreamReader sr = new StreamReader(this._passwdFilePath))
{
while (sr.Peek() >= 0)
{
string strTmp=sr.ReadLine();
this._PassWd.Add(strTmp);
}
}
}

}
}

public void ExitPlugin()
{
_action._actionType=GodzEyeLib.ActionType.Action_NoteTip;
_action._loader="FtpCrack插件正在退出=====>>>.....\n";
this.action_from_plugin(this,_action);
//
this._UserName.Clear();
//this._UserName=null;
this._PassWd.Clear();
//this._PassWd=null;
}

public void ShowUseDiag(VulInfo vulItem)
{
}

public void ShowConfigDiag()
{
FtpCrack.misc.Form_FtpConfig ftp_config=new FtpCrack.misc.Form_FtpConfig();
ftp_config.ShowDialog();
}

#endregion
#region startAttack
public unsafe void StartAttack(IPAddress addr)
{
//协商socket
GodzEyeLib.CommonFunction.init_socket();
int intCount=1;
int Passwd=1;

//当前线程。
// // int current_thread_count=0;
//
// //线程数组
// Thread[] work_threads=new Thread[this._threadCount];
//
yjf_1982 2006-09-06
  • 打赏
  • 举报
回复
同步
just up
godzeye 2006-09-06
  • 打赏
  • 举报
回复
加分喽
www_123du_com 2006-09-06
  • 打赏
  • 举报
回复
godzeye() ( ) 信誉:100 Blog 2006-09-06 17:16:00 得分: 0


类似的方法都尝试过,好象还没有单线程效率高啊
__________________________________________________

有可能是你的程序的问题,有可能。

可以把关键代码贴出来看看。
antoniusguo 2006-09-06
  • 打赏
  • 举报
回复
如果你单线程CPU使用率都是满的就不要多线程
godzeye 2006-09-06
  • 打赏
  • 举报
回复
类似的方法都尝试过,好象还没有单线程效率高啊
www_123du_com 2006-09-06
  • 打赏
  • 举报
回复
分段取呗。

比如10个线程,那么分成10段。

线程一试第1段,线程二试第2段......

如果只是取数据,不需要同步。

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧