초기 커밋.

This commit is contained in:
chainsecure
2026-05-28 14:48:48 +09:00
parent 377498fae8
commit 5d6ee943f6
31 changed files with 3182 additions and 0 deletions

116
ControlAgent/Form1.cs Normal file
View File

@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
namespace ControlAgent
{
public partial class Form1 : Form
{
private bool _isAgentRunning = true;
public Form1()
{
InitializeComponent();
// 백그라운드 태스크 형태로 서버 감지 및 수신 스레드 기동 (UI 먹통 현상 방지)
Task.Run(() => StartAgentTask());
}
private void StartAgentTask()
{
while (_isAgentRunning)
{
try
{
using (TcpClient client = new TcpClient())
{
var result = client.BeginConnect("192.168.219.47", 9999, null, null);
bool success = result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(5));
if (success)
{
client.EndConnect(result);
using (NetworkStream stream = client.GetStream())
using (System.IO.StreamReader reader = new System.IO.StreamReader(stream, Encoding.UTF8))
{
// 1. 연결되자마자 MAC 주소 송신
string myMac = GetMacAddress() + "\n";
byte[] macBytes = Encoding.UTF8.GetBytes(myMac);
stream.Write(macBytes, 0, macBytes.Length);
stream.Flush();
// 2. 서버로부터 명령/데이터 대기 (ReadLine 사용으로 안정성 확보)
while (client.Connected)
{
string command = reader.ReadLine(); // 데이터가 들어올 때까지 여기서 대기
if (string.IsNullOrEmpty(command)) break; // 연결이 끊기면 루프 탈출
// 명령 처리
// 에이전트 코드의 command == "STOP" 내부를 수정
if (command == "STOP")
{
// /f 옵션(강제 종료)과 /t 0(즉시)을 추가하고 절대 경로로 실행
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.Arguments = "/c shutdown /s /f /t 0"; // /f가 강제 종료 옵션입니다.
psi.CreateNoWindow = true;
psi.UseShellExecute = false;
System.Diagnostics.Process.Start(psi);
_isAgentRunning = false;
break;
}
else
{
// 데이터 전송 테스트 수신 시 처리
MessageBox.Show("서버 메시지 수신: " + command);
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("에이전트 루프 오류: " + ex.Message);
}
if (_isAgentRunning) Thread.Sleep(5000); // 5초 대기 후 재연결
}
}
// MAC 물리 주소 자동 추출 내부 함수
public string GetMacAddress()
{
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
// 가상 장치 필터링 및 오프라인 장치 필터링 수행
if (ni.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel &&
ni.OperationalStatus == OperationalStatus.Up)
{
string mac = ni.GetPhysicalAddress().ToString();
if (!string.IsNullOrEmpty(mac))
{
return mac; // 수집 완료
}
}
}
return "MAC Not Found";
}
public string GetMyInfo()
{
string pcName = Environment.MachineName;
string mac = GetMacAddress();
return $"{pcName}|{mac}";
}
}
}