๋ฐ˜์‘ํ˜•

ํŒจํ‚ท ์บก์ฒ˜๋ฅผ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ธ Pcap++์˜ ์ „๋ฐ˜์ ์ธ ์†Œ๊ฐœ๋Š” ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

[Wireshark] ๋„คํŠธ์›Œํฌ ํŒจํ‚ท ๋ชจ๋‹ˆํ„ฐ๋ง์€ PcapPlusPlus๋กœ ์‰ฝ๊ฒŒ!

 

[Wireshark] ๋„คํŠธ์›Œํฌ ํŒจํ‚ท ๋ชจ๋‹ˆํ„ฐ๋ง์€ PcapPlusPlus๋กœ ์‰ฝ๊ฒŒ!

Pcap++ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋„คํŠธ์›Œํฌ ํŒจํ‚ท ๊ฐ์ง€๋ฅผ ์œ„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ๋Š” Unix์˜ libpcap, Windows์˜ Npcap๊ณผ WinPcap์ด ๋Œ€ํ‘œ์ ์ž…๋‹ˆ๋‹ค. PcapPlusPlus(Pcap++)๋Š” ๋„คํŠธ์›Œํฌ ํŒจํ‚ท์„ ๊ฐ์ง€ํ•˜๊ณ  ํŒŒ์‹ฑํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ€ํ‹ฐ ํ”Œ๋žซํผ C++ ๋ผ

luckygg.tistory.com

์ค€๋น„ ๋‹จ๊ณ„

Pcap++๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ช‡ ๊ฐ€์ง€ ์ค€๋น„ ๋‹จ๊ณ„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์šด๋กœ๋“œ๊ฐ€ ํ•„์š”ํ•œ ํŒŒ์ผ์€ ๋งํฌ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์œผ๋‹ˆ ํ•ด๋‹น ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

1. Visual Studio ๋ฒ„์ „

Pcap++๋Š” 22.11 ๋ฒ„์ „ ๊ธฐ์ค€์œผ๋กœ Visual Studio 2015, 2017, 2019๋ฅผ ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. ์ด์— ๋งž๋Š” Visual Studio๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค.

2. WinPcap or Npcap SDK

WinPcap ๋˜๋Š” Npcap SDK๋ฅผ ์ค€๋น„ํ•ฉ๋‹ˆ๋‹ค. WinPcap์€ ์ง€์›์ด ๋๋‚ฌ์œผ๋ฏ€๋กœ, ๋ณธ๋ฌธ์—์„œ๋Š” Npcap SDK 1.13์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

3. Visual Studio 2010 ์žฌ๋ฐฐํฌ ํŒจํ‚ค์ง€

Microsoft Visual C++ 2010 ์žฌ๋ฐฐํฌ ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

4. Pcap++ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ ์˜ˆ์ œ ํ”„๋กœ์ ํŠธ

PcapPlusPlus Github์—์„œ Visual Studio ๋ฒ„์ „์— ๋งž๋Š” ํŒŒ์ผ์„ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. Release ํŽ˜์ด์ง€์—์„œ ์Šคํฌ๋กค์„ ์กฐ๊ธˆ ๋‚ด๋ฆฌ๋ฉด ๊ฐ ๊ฐœ๋ฐœํ™˜๊ฒฝ์— ๋งž๋Š” ํŒŒ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

PcapPlusPlus-Release-ํ™”๋ฉด
PcapPlusPlus Release ํ™”๋ฉด
๊ฐœ๋ฐœํ™˜๊ฒฝ-๋ณ„-๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ-ํŒŒ์ผ
๊ฐœ๋ฐœํ™˜๊ฒฝ ๋ณ„ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ํŒŒ์ผ

์˜ˆ์ œ ํ”„๋กœ์ ํŠธ ์‹คํ–‰ํ•˜๊ธฐ

๋ณธ๋ฌธ์˜ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์€ Windows์™€ Visual Studio 2019๋ฅผ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์œ„ Release ํŽ˜์ด์ง€์—์„œ pcapplusplus-22.11-windows-vs2019.zip์„ ๋‹ค์šด๋กœ๋“œํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์••์ถ•์„ ํ•ด์ œํ•œ ํ›„ pcapplusplus-22.11-windows-vs2019\ExampleProject ๊ฒฝ๋กœ๋ฅผ ๋ณด๋ฉด PcapPlusPlusPropertySheet.props ํŒŒ์ผ์ด ๋ณด์ž…๋‹ˆ๋‹ค.

์˜ˆ์ œ-ํ”„๋กœ์ ํŠธ-ํŒŒ์ผ-๊ตฌ์„ฑ
์˜ˆ์ œ ํ”„๋กœ์ ํŠธ ํŒŒ์ผ ๊ตฌ์„ฑ

์ด ํŒŒ์ผ์„ ํ…์ŠคํŠธ ์—๋””ํ„ฐ๋กœ ๋ถˆ๋Ÿฌ์˜จ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

์†์„ฑ-์‹œํŠธ-๋ณ€๊ฒฝํ•˜๊ธฐ
์†์„ฑ ์‹œํŠธ ๋ณ€๊ฒฝํ•˜๊ธฐ

ํ•ญ๋ชฉ ์„ค๋ช…
PcapPlusPlusHome ์˜ˆ์ œ ์ฝ”๋“œ๊ฐ€ ์œ„์น˜ํ•œ ๊ฒฝ๋กœ
PcapSdkHome ์ค€๋น„๋‹จ๊ณ„ 2์—์„œ ๋‹ค์šด๋กœ๋“œํ•œ WinPcap ๋˜๋Š” Npcap SDK๊ฐ€ ์œ„์น˜ํ•œ ๊ฒฝ๋กœ

์ด๋ ‡๊ฒŒ ์ €์žฅํ•œ ํ›„ ExampleProject.sln ํŒŒ์ผ์„ ์‹คํ–‰ํ•˜๋ฉด ํ”„๋กœ์ ํŠธ๊ฐ€ ์ •์ƒ์ ์œผ๋กœ ๋กœ๋“œ๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ, ๊ฒฝ๋กœ๊ฐ€ ๋‹ค๋ฅด๋ฉด ๋นŒ๋“œ ์ค‘ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š”๋ฐ, ์œ„์™€ ๊ฐ™์ด ์†์„ฑ ์‹œํŠธ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ์ง์ ‘ include, lib ๊ฒฝ๋กœ๋ฅผ ์ˆ˜์ •ํ•ด๋„ ์ƒ๊ด€์—†์Šต๋‹ˆ๋‹ค.

main.cpp์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

#include <iostream>
#include <IPv4Layer.h>
#include <Packet.h>
#include <PcapFileDevice.h>

int main(int argc, char* argv[])
{
	// open a pcap file for reading
	pcpp::PcapFileReaderDevice reader("1_packet.pcap");
	if (!reader.open())
	{
		std::cerr << "Error opening the pcap file" << std::endl;
		return 1;
	}

	// read the first (and only) packet from the file
	pcpp::RawPacket rawPacket;
	if (!reader.getNextPacket(rawPacket))
	{
		std::cerr << "Couldn't read the first packet in the file" << std::endl;
		return 1;
	}

	// parse the raw packet into a parsed packet
	pcpp::Packet parsedPacket(&rawPacket);

	// verify the packet is IPv4
	if (parsedPacket.isPacketOfType(pcpp::IPv4))
	{
		// extract source and dest IPs
		pcpp::IPv4Address srcIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getSrcIPv4Address();
		pcpp::IPv4Address destIP = parsedPacket.getLayerOfType<pcpp::IPv4Layer>()->getDstIPv4Address();

		// print source and dest IPs
		std::cout
			<< "Source IP is '" << srcIP << "'; "
			<< "Dest IP is '" << destIP << "'"
			<< std::endl;
	}

	// close the file
	reader.close();

	return 0;
}

์ฝ”๋“œ๋งŒ ๋ด๋„ ๋™์ž‘์ด ์•„์ฃผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. 1_packet.pcap ํŒŒ์ผ์„ ๋ถˆ๋Ÿฌ์˜จ ํ›„ packet์„ ์ฝ์–ด๋‚ด๊ณ , IPv4 ํƒ€์ž…์ด๋ผ๋ฉด Source IP์™€ Destination IP๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด ๋‹ค์ž…๋‹ˆ๋‹ค. ํ•œ๋ฒˆ ์‹คํ–‰ํ•ด ๋ณผ๊นŒ์š”?

๋™์ž‘-๊ฒฐ๊ณผ
๋™์ž‘ ๊ฒฐ๊ณผ

Source IP is '10.0.0.138'; Dest IP is '10.0.0.1'์ด๋ผ๋Š” ๋ฌธ์ž์—ด์ด ์ถœ๋ ฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์™€์ด์–ด์ƒคํฌ๋กœ 1_packet.pcap ํŒŒ์ผ์„ ์—ด์–ด๋ณด๋ฉด, ์‹ค์ œ๋กœ Source IP์™€ Destination IP๊ฐ€ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, ์ž˜ ๋™์ž‘ํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์™€์ด์–ด์ƒคํฌ๋กœ-pcap-ํŒŒ์ผ์„-์—ด์–ด๋ณธ-๋ชจ์Šต
์™€์ด์–ด์ƒคํฌ๋กœ pcap ํŒŒ์ผ์„ ์—ด์–ด๋ณธ ๋ชจ์Šต

 

๋ฐ˜์‘ํ˜•