Advertisement
Guest User

Untitled

a guest
Jan 14th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.87 KB | None | 0 0
  1. local lastHostID = 0
  2. local lasttMessage = ""
  3. local tHosts = { }
  4.  
  5. --[[
  6.  
  7. lPort = destination - Port
  8. rPort = reply / sender - Port
  9.  
  10. ]]
  11.  
  12. --[[
  13.  
  14. Add check for double /sniff - hosts/
  15. do not close side if double!
  16.  
  17. ]]
  18.  
  19. local
  20. private = { }
  21.  
  22. --attack = {
  23. dos = {
  24. dns_client_error = nil,
  25. dns_server_error = nil,
  26. dns_flood = nil,
  27. rns_packet = nil
  28. }
  29. --}
  30.  
  31. --network = {
  32. --gather = {
  33. scann = {
  34. findRednet = nil,
  35. lookupHosts = nil
  36. }
  37. sniff = {
  38. addHost = nil,
  39. getHost = nil,
  40. getHostList = nil,
  41. parallel = nil,
  42. parseRednet = nil,
  43. reload = nil,
  44. reloadAll = nil,
  45. removeAllHosts = nil,
  46. removeHost = nil,
  47. run = nil,
  48. setHost = nil,
  49. stop = nil
  50. }
  51. --},
  52. spoof = {
  53. dnsReply = nil,
  54. dnsRequest = nil,
  55. forward = nil, -- soon
  56. host = nil,
  57. message = nil,
  58. request = nil -- IMPORTANT SOON
  59. }
  60. wired = {
  61. execute = nil,
  62. getMethods = nil,
  63. getNames = nil,
  64. getType = nil,
  65. isPresent = nil
  66. }
  67. --}
  68.  
  69. dos.dns_client_error = function(
  70. lPort,
  71. rPort) -- (opt)
  72.  
  73. end
  74.  
  75. dos.dns_server_error = function(
  76. lPort,
  77. rPort) -- (opt)
  78.  
  79. end
  80.  
  81. dos.dns_flood = function(
  82. mSide,
  83. lPort, -- (all)
  84. sProtocol, -- (all)
  85. nCount, -- (opt)
  86. nTimer)
  87. if type(sProtocol) ~= "string" and sProtocol ~= nil then
  88. error("Protocol as string expected", 0)
  89. end
  90.  
  91. if type(nCount) ~= "number" then
  92. nCount = 10
  93. end
  94.  
  95. local modem = peripheral.wrap( private.simpleSide(mSide) )
  96.  
  97. local nHost = sniff.addHost(
  98. 65535,
  99. nil,
  100. mSide)
  101.  
  102. local dosHosts = { }
  103. while true do
  104. os.startTimer(0.05)
  105. local sEvent, vData, nPort, nReplyPort, tMessage, nDistance
  106. = os.pullEvent()
  107.  
  108. for _, dosHost in pairs(dosHosts) do
  109. for i=1, nCount do
  110. local newRPort = math.random(1, 65535)
  111.  
  112. spoof.dnsReply(
  113. modem,
  114. dosHost.nReplyPort,
  115. newRPort,
  116. dosHost.sHostname == nil and tostring( math.random(1, 65535) )
  117. or dosHost.sHostname,
  118. dosHost.sProtocol)
  119. end
  120. end
  121.  
  122. if sEvent == "sniff_message" and vData == nHost then
  123. local message = sniff.parseRednet(tMessage)
  124.  
  125. if message
  126. and tMessage.sProtocol == "dns"
  127. and type(message.sType) == "string"
  128. and message.sType == "lookup"
  129. and (sProtocol == nil or
  130. sProtocol == tMessage.sProtocol)
  131. and lPort == nil or lPort == nReplyPort then
  132. dosHosts[os.startTimer(2)] =
  133. {
  134. nReplyPort = nReplyPort,
  135. sProtocol = message.sProtocol,
  136. sHostname = message.sHostname
  137. }
  138. end
  139. elseif sEvent == "timer" and type(dosHosts[vData]) == "table" then
  140. dosHosts[vData] = nil
  141. elseif sEvent == "timer" and vData == nTimer then
  142. break
  143. end
  144. end
  145.  
  146. sniff.removeHost(nHost)
  147. end
  148.  
  149. -- RE NEW SEND PACKET
  150. dos.rns_packet = function(
  151. lPort,
  152. rPort,
  153. sSide,
  154. sProtocol,
  155. nCount,
  156. nTimer)
  157.  
  158. end
  159.  
  160. private.tableContains = function(tTable, vmVar)
  161. for _, vsVar in pairs(tTable) do
  162. if vsVar == vmVar then
  163. return true
  164. end
  165. end
  166. return false
  167. end
  168.  
  169. private.fullPort = function(vPort)
  170. tPorts = vPort
  171.  
  172. if type(vPort) ~= "table" then
  173. tPorts = { private.simplePort(vPort) }
  174. end
  175.  
  176. for i, nPort in pairs(tPorts) do
  177. if not (nPort >= 0 and nPort <= 65535) then
  178. table.remove(tPorts, i)
  179. end
  180. end
  181.  
  182. if #tPorts == 0 then
  183. error("Selected ports are out of range")
  184. end
  185.  
  186. return tPorts
  187. end
  188.  
  189. private.simplePort = function(vPort)
  190. if type(vPort) == "string" then
  191. vPort = tonumber(vPort)
  192. elseif type(vPort) ~= "number" then
  193. error("Port as number or string expected", 0)
  194. end
  195.  
  196. if vPort >= 0 and vPort <= 65535 then
  197. return vPort
  198. end
  199.  
  200. error("Port is not in range", 0)
  201. end
  202.  
  203. private.updateSide = function(vSide)
  204. local tAviable = peripheral.getNames()
  205. local tSides = vSide
  206.  
  207. if #tAviable == 0 then
  208. error("No modems found", 0)
  209. end
  210.  
  211. if type(vSide) == "string" then
  212. tSides = { vSide }
  213. elseif type(vSide) ~= "table" then
  214. return tAviable
  215. end
  216.  
  217. for i, sSide in pairs(tSides) do
  218. local bLost = true
  219. for _, avSide in pairs(tAviable) do
  220. if sSide == avSide then
  221. bLost = false
  222. break
  223. end
  224. end
  225. if bLost then
  226. table.remove(tSides, i)
  227. end
  228. end
  229.  
  230. if #tSides == 0 then
  231. error("Selected modems not found", 0)
  232. end
  233.  
  234. return tSides
  235. end
  236.  
  237. private.fullSide = function(vSide)
  238. local tAviable = peripheral.getNames()
  239. local tSides = vSide
  240.  
  241. if #tAviable == 0 then
  242. error("No modems found", 0)
  243. end
  244.  
  245. if type(vSide) ~= "table" then
  246. tSides = { private.simpleSide(vSide) }
  247. end
  248.  
  249. for i, sSide in pairs(tSides) do
  250. local bLost = true
  251. for _, avSide in pairs(tAviable) do
  252. tSides[i] = private.simpleSide( sSide )
  253. if sSide == avSide then
  254. bLost = false
  255. break
  256. end
  257. end
  258. if bLost then
  259. table.remove(tSides, i)
  260. end
  261. end
  262.  
  263. if #tSides == 0 then
  264. error("Selected modems not found", 0)
  265. end
  266.  
  267. return tSides
  268. end
  269.  
  270. private.simpleSide = function(vSide)
  271. if type(vSide) ~= "string" then
  272. error("String as side expected", 0)
  273. end
  274.  
  275. if not private.tableContains({
  276. "top", "bottom",
  277. "left", "right",
  278. "front", "back"
  279. }, vSide) then
  280. error("Modem side not found", 0)
  281. end
  282.  
  283. return vSide
  284. end
  285.  
  286. scann.findRednet = function(
  287. mSide,
  288. nTimeout,
  289. sProtocol)
  290. local tRN = { }
  291.  
  292. if type(nTimeout) ~= "number" then
  293. error("Timout as number expected", 0)
  294. end
  295.  
  296. local nHost = sniff.addHost(
  297. 65533,
  298. nil,
  299. mSide)
  300.  
  301. local nTimer = os.startTimer(nTimeout)
  302. while true do
  303. local sEvent, vData, nPort, nReplyPort, tMessage, nDistance
  304. = os.pullEvent()
  305.  
  306. if sEvent == "siff_message" then
  307. local message = sniff.parseRednet(tMessage)
  308.  
  309. if message
  310. and not private.tableContains(tRN, message.nRecipient) then
  311. table.insert(tRN, message.nRecipient)
  312. end
  313. elseif sEvent == "timer" and nTimer == vData then
  314. break
  315. end
  316. end
  317.  
  318. sniff.removeHost(nHost)
  319.  
  320. return tRN
  321. end
  322.  
  323. scann.lookupHosts = function(
  324. mSide,
  325. sProtocol,
  326. rPort)
  327. local modem = peripheral.wrap( private.simpleSide( mSide ) )
  328. local tDnsHosts = { }
  329.  
  330. if type(sProtocol) ~= "string" then
  331. error("Protocol as string expected", 0)
  332. end
  333.  
  334. if type(rPort) ~= "number" then
  335. rPort = math.random(0, 65535)
  336. end
  337.  
  338. local nHost = sniff.addHost(
  339. rPort,
  340. nil,
  341. mSide)
  342.  
  343. spoof.dnsRequest(
  344. modem,
  345. rPort,
  346. sProtocol)
  347.  
  348. local nTimer = os.startTimer(2)
  349. while true do
  350. local sEvent, vData, nPort, nReplyPort, tMessage, nDistance
  351. = os.pullEvent()
  352.  
  353. if sEvent == "sniff_message" then
  354. local message = sniff.parseRednet(tMessage)
  355.  
  356. if message
  357. and nPort == rPort
  358. and tMessage.sProtocol == "dns"
  359. and type(message.sType) == "string"
  360. and message.sType == "lookup response"
  361. and type(message.sProtocol) == "string"
  362. and message.sProtocol == sProtocol then
  363. table.insert(tDnsHosts,{
  364. sHostname = message.sHostname,
  365. nRecipient = nReplyPort
  366. })
  367. end
  368. elseif sEvent == "timer" and nTimer == vData then
  369. break
  370. end
  371. end
  372.  
  373. sniff.removeHost(nHost)
  374.  
  375. return tDnsHosts
  376. end
  377.  
  378. sniff.addHost = function(
  379. lPort,
  380. rPort,
  381. vSide)
  382. lastHostID = lastHostID + 1
  383.  
  384. tHosts[ lastHostID ] =
  385. {
  386. ID = lastHostID,
  387. lPort = private.fullPort(lPort),
  388. rPort = (rPort == nil)
  389. and "true" or private.fullPort(rPort),
  390. sSide = private.fullSide(vSide)
  391. }
  392.  
  393. for _, mSide in pairs( tHosts[lastHostID].sSide ) do
  394. for _, mPort in pairs( tHosts[lastHostID].lPort ) do
  395. peripheral.call(mSide, "open", mPort)
  396. end
  397. end
  398.  
  399. return lastHostID
  400. end
  401.  
  402. sniff.getHost = function(ID)
  403. if type(tHosts[ID]) == "table" then
  404. return tHosts[ID]
  405. else
  406. error("Host not found", 0)
  407. end
  408. end
  409.  
  410. sniff.getHostList = function()
  411. local list = { }
  412. for i in pairs(tHosts) do
  413. table.insert(list, i)
  414. end
  415. return list
  416. end
  417.  
  418. sniff.parallel = function()
  419. if private.sniff_bRunning then
  420. error("Sniffer is already running", 0)
  421. end
  422. private.sniff_bRunning = true
  423.  
  424. while true do
  425. if not private.sniff_bRunning then
  426. sniff.stop()
  427. end
  428.  
  429. local sEvent, vData, nPort, nReplyPort, tMessage, nDistance
  430. = private.native_pullEventRaw()
  431. if sEvent == "modem_message"
  432. and textutils.serialize(tMessage) ~= lasttMessage then
  433. lasttMessage = textutils.serialize(tMessage)
  434. for _, tHost in pairs(tHosts) do
  435. if private.tableContains(tHost.sSide, vData)
  436. and private.tableContains(tHost.lPort, nPort) then
  437. if type(tHost.rPort) == "string"
  438. or private.tableContains(tHost.rPort, nReplyPort) then
  439. os.queueEvent(
  440. "sniff_message",
  441. tHost.ID,
  442. nPort,
  443. nReplyPort,
  444. tMessage,
  445. distance)
  446. end end
  447. end
  448. elseif sEvent == "peripheral_detach"
  449. and vData == "modem" then
  450. for ID in pairs(tHosts) do
  451. tHosts[ID].side = private.fullSide(side)
  452. end
  453. end
  454. end
  455. end
  456.  
  457. sniff.parseRednet = function(tMessage)
  458. if type(tMessage) == "table"
  459. and type(tMessage.nRecipient) == "number"
  460. and type(tMessage.nMessageID) == "number" then
  461. return tMessage.message
  462. end
  463.  
  464. return false
  465. end
  466.  
  467. sniff.reload = function(ID)
  468. sniff.setHost(ID)
  469. end
  470.  
  471. sniff.reloadAll = function()
  472. for _, tHost in pairs(tHosts) do
  473. sniff.reload(tHost.ID)
  474. end
  475. end
  476.  
  477. sniff.removeAllHosts = function()
  478. for _, tHost in pairs(tHosts) do
  479. sniff.removeHost(tHost.ID)
  480. end
  481. end
  482.  
  483. sniff.removeHost = function(ID)
  484. if type(tHosts[ID]) == "table" then
  485. for _, mSide in pairs( tHosts[ID].sSide ) do
  486. for _, mPort in pairs( tHosts[ID].lPort ) do
  487. local bFound = false
  488. for _, tHost in pairs(tHosts) do
  489. if private.tableContains(tHost.sSide, mSide)
  490. and private.tableContains(tHost.lPort, mPort) then
  491. bFound = true
  492. end
  493. end
  494.  
  495. if bFound then
  496. break
  497. end
  498.  
  499. peripheral.call(mSide, "close", mPort)
  500. end
  501. end
  502.  
  503. tHosts[ID] = nil
  504. else
  505. error("Host not found", 0)
  506. end
  507. end
  508.  
  509. private.native_pullEventRaw = os.pullEventRaw
  510. private.sniff_bRunning = false
  511. sniff.run = function()
  512. if private.sniff_bRunning then
  513. error("Sniffer is already running", 0)
  514. end
  515. private.sniff_bRunning = true
  516.  
  517. private.native_pullEventRaw = os.pullEventRaw
  518. os.pullEventRaw = function(...)
  519. if not private.sniff_bRunning then
  520. sniff.stop()
  521. end
  522.  
  523. local sEvent, vData, nPort, nReplyPort, tMessage, nDistance
  524. = private.native_pullEventRaw(...)
  525. if sEvent == "modem_message"
  526. and textutils.serialize(tMessage) ~= lasttMessage then
  527. lasttMessage = textutils.serialize(tMessage)
  528. for _, tHost in pairs(tHosts) do
  529. if private.tableContains(tHost.sSide, vData)
  530. and private.tableContains(tHost.lPort, nPort)
  531. and tHost.lPort == nPort then
  532. if type(tHost.rPort) == "string"
  533. or tHost.rPort == nReplyPort then
  534. os.queueEvent(
  535. "sniff_message",
  536. tHost.ID,
  537. nPort,
  538. nReplyPort,
  539. tMessage,
  540. distance)
  541. end end
  542. end
  543. elseif sEvent == "peripheral_detach"
  544. and vData == "modem" then
  545. for ID in pairs(tHosts) do
  546. tHosts[ID].side = private.fullSide(side)
  547. end
  548. end
  549.  
  550. return sEvent, vData, nPort, nReplyPort, tMessage, nDistance
  551. end
  552. end
  553.  
  554. sniff.setHost = function(
  555. ID,
  556. lPort,
  557. rPort,
  558. sSide)
  559. if type(tHosts[ID]) == "table" then
  560. local tlPort = tHosts[ID].lPort
  561. local trPort = tHosts[ID].rPort
  562. local tsSide = tHosts[ID].sSide
  563. local temp_lhID = lastHostID
  564.  
  565. if type(lPort) == "number"
  566. or type(lPort) == "table" then
  567. tlPort = private.fullPort(lPort)
  568. end
  569.  
  570. if type(rPort) == "number"
  571. or type(rPort) == "table" then
  572. tHosts[ID].rPort = private.fullPort(rPort)
  573. elseif type(rPort) == "string" then
  574. trPort = "true"
  575. end
  576.  
  577. if type(sSide) == "string"
  578. or type(sSide) == "table" then
  579. tsSide = private.fullSide(sSide)
  580. end
  581.  
  582. sniff.removeHost(ID)
  583. lastHostID = ID - 1
  584. sniff.addHost(tlPort, trPort, tsSide)
  585. lastHostID = temp_lhID
  586. else
  587. error("Host not found", 0)
  588. end
  589. end
  590.  
  591. sniff.stop = function()
  592. if private.sniff_bRunning then
  593. os.pullEventRaw = private.native_pullEventRaw
  594. private.sniff_bRunning = false
  595. else
  596. error("Sniffer is not running", 0)
  597. end
  598. end
  599.  
  600. spoof.dnsReply = function(
  601. modem,
  602. lPort,
  603. rPort,
  604. sHostname,
  605. sProtocol)
  606. if type(sHostname) ~= "string" then
  607. error("Hostname expected", 0)
  608. end
  609.  
  610. if type(sProtocol) ~= "string" then
  611. error("Protocol expected", 0)
  612. end
  613.  
  614. spoof.message(
  615. modem,
  616. lPort,
  617. rPort,
  618. rPort,
  619. {
  620. sType = "lookup response",
  621. sHostname = sHostname,
  622. sProtocol = sProtocol
  623. },
  624. "dns")
  625. end
  626.  
  627. spoof.dnsRequest = function(
  628. modem,
  629. rPort,
  630. sProtocol,
  631. sHostname)
  632. if type(sProtocol) ~= "string" then
  633. error("Protocol expected", 0)
  634. end
  635.  
  636. spoof.message(
  637. modem,
  638. 65535,
  639. rPort,
  640. rPort,
  641. {
  642. sType = "lookup",
  643. sProtocol = sProtocol,
  644. sHostname = sHostname
  645. },
  646. "dns")
  647. end
  648.  
  649. spoof.forward = function(
  650. mSide,
  651. rllport,
  652. rlrport,
  653. fklport,
  654. fkrport,
  655. isRednet,
  656. sProtocol,
  657. isRCroutine)
  658. local foo = function()
  659.  
  660. end
  661.  
  662. if isRCroutine then
  663. return coroutine.create(foo)
  664. else
  665. foo()
  666. end
  667. end
  668.  
  669. spoof.host = function(
  670. modem,
  671. vPort,
  672. sProtocol)
  673. -- Active spoofing (repeat) [dns]
  674. end
  675.  
  676. spoof.message = function(
  677. modem,
  678. destPort,
  679. replyPort,
  680. nRecipient,
  681. message,
  682. sProtocol)
  683. if not modem then
  684. error("No modem selected", 0)
  685. end
  686.  
  687. destPort = private.simplePort(destPort)
  688. replyPort = private.simplePort(replyPort)
  689. nRecipient = private.simplePort(nRecipient)
  690.  
  691. if type(sProtocol) ~= "string" then
  692. sProtocol = ""
  693. end
  694.  
  695. modem.transmit(destPort, replyPort,
  696. {
  697. nRecipient = nRecipient,
  698. message = message,
  699. nMessageID = math.random(1, 2147483647),
  700. sProtocol = sProtocol
  701. })
  702. end
  703.  
  704. spoof.request = function()
  705. -- Setup sniff
  706. -- Send message
  707. -- Wait for message
  708. -- Parse message
  709. -- Return
  710. end
  711.  
  712. wired.execute = function(
  713. modem,
  714. sName,
  715. sMethod,
  716. ...)
  717. if modem.isWireless() then
  718. error("Only for wired modems", 0)
  719. end
  720.  
  721. if type(sName) ~= "string" then
  722. error("Name expected", 0)
  723. end
  724.  
  725. return modem.callRemote(sName, sMethod, ...)
  726. end
  727.  
  728. wired.getMethods = function(modem, sName)
  729. if modem.isWireless() then
  730. error("Only for wired modems", 0)
  731. end
  732.  
  733. if type(sName) ~= "string" then
  734. error("Name expected", 0)
  735. end
  736.  
  737. return modem.getMethodsRemote(sName)
  738. end
  739.  
  740. wired.getNames = function(modem)
  741. if modem.isWireless() then
  742. error("Only for wired modems", 0)
  743. end
  744.  
  745. return modem.getNamesRemote()
  746. end
  747.  
  748. wired.getType = function(modem, sName)
  749. if modem.isWireless() then
  750. error("Only for wired modems", 0)
  751. end
  752.  
  753. if type(sName) ~= "string" then
  754. error("Name expected", 0)
  755. end
  756.  
  757. return modem.getTypeRemote(sName)
  758. end
  759.  
  760. wired.isPresent = function(modem, sName)
  761. if modem.isWireless() then
  762. error("Only for wired modems", 0)
  763. end
  764.  
  765. if type(sName) ~= "string" then
  766. error("Name expected", 0)
  767. end
  768.  
  769. return modem.isPresentRemote(sName)
  770. end
  771.  
  772. return {
  773. dos = dos,
  774. scann = scann,
  775. sniff = sniff,
  776. spoof = spoof,
  777. wired = wired
  778. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement