So I set up an ssh honeypot on a ubuntu vm for fun, and wanted to sort the most frequently tried passwords and the most frequently tried user-pass combos.
I wrote something that while worked, took a fairly long time, even considering that the log file is 178.000+ lines.
So I decided to try multi-threading it. I tried using Parallel.ForEach.
This was messed up, didn't write the whole thing to the result files. I googled stuff and ended up finding something about concurrent stuff.
It kind of "works" now, but not really the way I want it to. It writes the data to the 2 files (most popular passwords.dat(mpp) and most popular combos.dat(mpc)), but they are neither in ascending nor descending order based on occurences in their lists.
(I know that the sorting works, because it was fine with just single-threaded foreach loops)
Here's the code I have so far (try not judge, I'm still in high school, I know it probably looks messy, and I will try to tidy it up a bit if I can get it to work)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections.Specialized;
using System.Threading;
using System.Collections.Concurrent;
namespace ConsoleApp1
{
public class m
{
public static List<string> passwords = new List<string>();
public static List<string> up = new List<string>();
public static void mpcF()
{
var mpc = new ConcurrentBag<string>();
var dictionary = up.GroupBy(str => str)
.ToDictionary(group => group.Key, group => group.Count());
var items = from pair in dictionary
orderby pair.Value descending
select pair;
Parallel.ForEach(items, item =>
{
mpc.Add((item.Key.PadRight(45) + " | " + item.Value/* + Environment.NewLine*/));
});
var result = string.Join(Environment.NewLine, mpc);
File.WriteAllText("mpc.dat", result);
Console.WriteLine("DUN-DUN-DUUNNN!!!!");
}
public static void mppF()
{
var mpp = new ConcurrentBag<string>();
var dictionary = passwords.GroupBy(str => str)
.ToDictionary(group => group.Key, group => group.Count());
var items = from pair in dictionary
orderby pair.Value descending
select pair;
Parallel.ForEach(items, item =>
{
mpp.Add((item.Key.PadRight(45) + " | " + item.Value/* + Environment.NewLine*/));
});
var result = string.Join(Environment.NewLine, mpp);
File.WriteAllText("mpp.dat", result);
Console.WriteLine("DUN-DUN-DUUNNN!!!! (2)");
}
}
class Program
{
static void read()
{
using (StreamReader sr = new StreamReader("ssh-honeypot.log"))
{
while (!sr.EndOfStream)
{
string[] t = sr.ReadLine().Split(']')[1].Split(' ');
if (t[2] != "Error")
{
m.passwords.Add(t[3]);
m.up.Add(t[2] + " - " + t[3]);
}
}
}
}
static void print()
{
m.mpcF();
m.mppF();
/*Thread t1 = new Thread(new ThreadStart(m.mpcF));
Thread t2 = new Thread(new ThreadStart(m.mppF));
t1.Start();
t2.Start();*/
}
static void Main(string[] args)
{
read();
print();
Console.ReadKey();
}
}
}