buildDate.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. package main
  2. import (
  3. "bufio"
  4. "crypto/rand"
  5. "encoding/base64"
  6. "encoding/json"
  7. "flag"
  8. "fmt"
  9. "io"
  10. "log"
  11. mathrand "math/rand"
  12. "os"
  13. "qianxin-inc.cn/common/utils/encode"
  14. "strings"
  15. v2 "sinohorizon.com/scan-probe/pkg/common/types/v2"
  16. "sinohorizon.com/traffic-analysis-server/pkg/common/types/v1/utils/mixer"
  17. probetypes "sinohorizon.com/traffic-analysis-server/pkg/common/types/v1/types"
  18. "sinohorizon.com/traffic-analysis-server/pkg/common/types/v1/utils/decode"
  19. )
  20. var clients []string
  21. var mixStr string
  22. var auth string
  23. var getPortsV2Map map[string]string
  24. func init() {
  25. var err error
  26. var basic string
  27. var res []byte
  28. mixStr, err = mixer.GetDeMixStr(mixer.Code1)
  29. basic, err = decode.DecodeCBC(probetypes.BASIC, mixStr)
  30. if err != nil {
  31. fmt.Fprintf(os.Stdout, "decode error")
  32. panic(fmt.Errorf("failed to get auth token"))
  33. }
  34. basic = strings.Split(basic, " ")[1]
  35. //fmt.Println(basic)
  36. res, err = base64.StdEncoding.DecodeString(basic)
  37. if err != nil {
  38. fmt.Fprintf(os.Stdout, "base64 decode error")
  39. panic(fmt.Errorf("failed to get auth token"))
  40. }
  41. auth = string(res)
  42. //fmt.Println(auth)
  43. }
  44. func main(){
  45. var portsCount int
  46. var probesCount int
  47. var matchCount int
  48. var heartbeatCount int
  49. flag.IntVar(&portsCount, "ports-count", 0, "")
  50. flag.IntVar(&probesCount, "probes-count", 0, "")
  51. flag.IntVar(&matchCount, "match-count", 0, "")
  52. flag.IntVar(&heartbeatCount, "heartbeat-count", 0, "")
  53. flag.Parse()
  54. BuildGetPortsV2Mesg(portsCount)
  55. BuildGetProbesMesg(probesCount)
  56. BuildMatchMesg(matchCount)
  57. BuildHreatBeatMesg(heartbeatCount)
  58. //DataForPre()
  59. //BuildBusinessUrl(pppoeCount)
  60. //BuildClientUrl(clientCount)
  61. //BuildPocUrl(pocCount)
  62. }
  63. func BuildHreatBeatMesg(count int){
  64. filePath := "./data/heartBeat.txt"
  65. _, err := os.Create(filePath)
  66. if err != nil {
  67. log.Fatal("Create poc file error: ",err.Error())
  68. return
  69. }
  70. file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
  71. if err != nil {
  72. log.Fatal("Open poc file error:", err.Error())
  73. return
  74. }
  75. defer func() {
  76. err = file.Close()
  77. if err != nil{
  78. log.Fatal("Close poc file error:", err.Error())
  79. }
  80. }()
  81. writer := bufio.NewWriter(file)
  82. product_class:="ZNHE600-REALTEK+RTL9600"
  83. domain_ip:="114.217.26.173"
  84. cloud_tasktime:="4.416 ms"
  85. e8ctmp_log:="3.0-0.0.12-1#1624935018#1624935018#57324#0#1#1#10800#10800#1624935018#3720#R4036"
  86. for i:=1;i<=count;i++{
  87. //mac:=fmt.Sprintf("%012d",i)
  88. pppoe:=fmt.Sprintf("test%d",i)
  89. gateway_mac :=fmt.Sprintf("2%011d",i)
  90. loid:= fmt.Sprintf("%016d",i)
  91. data:=fmt.Sprintf("%s|%s|%s|%s|%s|%s|%s|3.0.01|0.0.01",pppoe,gateway_mac,loid,product_class,domain_ip,cloud_tasktime,e8ctmp_log)
  92. sendDataPre := fmt.Sprintf("%s|||qianxinBasicAuth:qianxinBasicAuth#@!ZXC|||HeartBeat|||00000000000000000001|||%d|||%s", pppoe, len(data), data)
  93. es, _ := encode.Encrypt(sendDataPre, mixStr)
  94. //getPortsV2Map[mac]=es
  95. _, err = writer.WriteString(fmt.Sprintf("%s:%s\n",gateway_mac,es))
  96. if err != nil {
  97. continue
  98. }
  99. }
  100. err = writer.Flush()
  101. if err != nil {
  102. log.Fatal("Write poc file failed: ",err.Error())
  103. }
  104. }
  105. func BuildMatchMesg(count int){
  106. filePath := "./data/match.txt"
  107. _, err := os.Create(filePath)
  108. if err != nil {
  109. log.Fatal("Create poc file error: ",err.Error())
  110. return
  111. }
  112. file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
  113. if err != nil {
  114. log.Fatal("Open poc file error:", err.Error())
  115. return
  116. }
  117. defer func() {
  118. err = file.Close()
  119. if err != nil{
  120. log.Fatal("Close poc file error:", err.Error())
  121. }
  122. }()
  123. writer := bufio.NewWriter(file)
  124. req := &v2.Request{}
  125. target := v2.Target{
  126. IP: "192.168.16.6",
  127. Port:23,
  128. Protocol:"tcp",
  129. }
  130. config := v2.Config{
  131. Rarity:9,
  132. SendTimeout:1000000000,
  133. ReadTimeout:1000000000,
  134. }
  135. probe := v2.ProbeBase{
  136. Name:"tn3270",
  137. Data:"Ly9zWS8vb1lBRWxDVFMwek1qYzVMVFF0UmYvdy8vc1ovLzBaLy9zQS8vMEE=",
  138. Protocol:"tcp",
  139. }
  140. req.Config=config
  141. req.Target=target
  142. req.Probe=probe
  143. req.DeviceAllScanCount=1
  144. req.DevicePortScanIndex=1
  145. req.DevicePortAllScanCount=1
  146. req.DeviceScanIndex=1
  147. req.ScanID="0012222222222"
  148. req.Count=1
  149. req.Index=1
  150. for i:=1;i<=count;i++{
  151. mac:=fmt.Sprintf("%012d",i)
  152. pppoe:=fmt.Sprintf("test%d",i)
  153. gateway_mac :=fmt.Sprintf("2%011d",i)
  154. loid:= fmt.Sprintf("%016d",i)
  155. req.Mac = mac
  156. req.DevName = "miuyedeMBP"
  157. req.PPPOEName=pppoe
  158. req.GatewayMac=gateway_mac
  159. req.LoID=loid
  160. data,_:=json.Marshal(req)
  161. sendDataPre := fmt.Sprintf("%s|||qianxinBasicAuth:qianxinBasicAuth#@!ZXC|||Match|||00000000000000000001|||%d|||%s", pppoe, len(data), data)
  162. es, _ := encode.Encrypt(sendDataPre, mixStr)
  163. //getPortsV2Map[mac]=es
  164. _, err = writer.WriteString(fmt.Sprintf("%s:%s\n",gateway_mac,es))
  165. if err != nil {
  166. continue
  167. }
  168. }
  169. err = writer.Flush()
  170. if err != nil {
  171. log.Fatal("Write poc file failed: ",err.Error())
  172. }
  173. }
  174. func BuildGetPortsV2Mesg(count int){
  175. filePath := "./data/getPortsV2.txt"
  176. _, err := os.Create(filePath)
  177. if err != nil {
  178. log.Fatal("Create poc file error: ",err.Error())
  179. return
  180. }
  181. file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
  182. if err != nil {
  183. log.Fatal("Open poc file error:", err.Error())
  184. return
  185. }
  186. defer func() {
  187. err = file.Close()
  188. if err != nil{
  189. log.Fatal("Close poc file error:", err.Error())
  190. }
  191. }()
  192. writer := bufio.NewWriter(file)
  193. req := &v2.Request{}
  194. for i:=1;i<=count;i++{
  195. mac:=fmt.Sprintf("%012d",i)
  196. pppoe:=fmt.Sprintf("test%d",i)
  197. gateway_mac :=fmt.Sprintf("2%011d",i)
  198. req.Mac=mac
  199. req.ScanType="completescan"
  200. data,_:=json.Marshal(req)
  201. sendDataPre := fmt.Sprintf("%s|||qianxinBasicAuth:qianxinBasicAuth#@!ZXC|||GetPortsV2|||00000000000000000001|||%d|||%s",pppoe , len(data), data)
  202. es, _ := encode.Encrypt(sendDataPre, mixStr)
  203. //getPortsV2Map[mac]=es
  204. _, err = writer.WriteString(fmt.Sprintf("%s:%s\n",gateway_mac,es))
  205. if err != nil {
  206. continue
  207. }
  208. }
  209. err = writer.Flush()
  210. if err != nil {
  211. log.Fatal("Write poc file failed: ",err.Error())
  212. }
  213. }
  214. func BuildGetProbesMesg(count int){
  215. filePath := "./data/getProbes.txt"
  216. _, err := os.Create(filePath)
  217. if err != nil {
  218. log.Fatal("Create poc file error: ",err.Error())
  219. return
  220. }
  221. file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
  222. if err != nil {
  223. log.Fatal("Open poc file error:", err.Error())
  224. return
  225. }
  226. defer func() {
  227. err = file.Close()
  228. if err != nil{
  229. log.Fatal("Close poc file error:", err.Error())
  230. }
  231. }()
  232. writer := bufio.NewWriter(file)
  233. req := &v2.Request{}
  234. target := v2.Target{
  235. IP: "192.168.15.1",
  236. Port:9090,
  237. Protocol:"udp",
  238. }
  239. config := v2.Config{
  240. Rarity:9,
  241. SendTimeout:1000000000,
  242. ReadTimeout:1000000000,
  243. }
  244. req.Config=config
  245. req.Target=target
  246. req.DeviceAllScanCount=1
  247. req.DevicePortScanIndex=1
  248. req.DevicePortAllScanCount=1
  249. req.DeviceScanIndex=1
  250. req.ScanID="0012222222222"
  251. for i:=1;i<=count;i++{
  252. mac:=fmt.Sprintf("%012d",i)
  253. pppoe:=fmt.Sprintf("test%d",i)
  254. gateway_mac :=fmt.Sprintf("2%011d",i)
  255. loid:= fmt.Sprintf("%016d",i)
  256. req.Mac = mac
  257. req.DevName = "miuyedeMBP"
  258. req.PPPOEName=pppoe
  259. req.GatewayMac=gateway_mac
  260. req.LoID=loid
  261. data,_:=json.Marshal(req)
  262. sendDataPre := fmt.Sprintf("%s|||qianxinBasicAuth:qianxinBasicAuth#@!ZXC|||GetProbes|||00000000000000000001|||%d|||%s", pppoe, len(data), data)
  263. es, _ := encode.Encrypt(sendDataPre, mixStr)
  264. //getPortsV2Map[mac]=es
  265. _, err = writer.WriteString(fmt.Sprintf("%s:%s\n",gateway_mac,es))
  266. if err != nil {
  267. continue
  268. }
  269. }
  270. err = writer.Flush()
  271. if err != nil {
  272. log.Fatal("Write poc file failed: ",err.Error())
  273. }
  274. }
  275. func BuildDeviceRecognise(){
  276. filePath := "./data/recognise.txt"
  277. _, err := os.Create(filePath)
  278. if err != nil {
  279. log.Fatal("Create poc file error: ",err.Error())
  280. return
  281. }
  282. file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
  283. if err != nil {
  284. log.Fatal("Open poc file error:", err.Error())
  285. return
  286. }
  287. defer func() {
  288. err = file.Close()
  289. if err != nil{
  290. log.Fatal("Close poc file error:", err.Error())
  291. }
  292. }()
  293. }
  294. func BuildPocUrl(pocCount int){
  295. filePath := "./data/poc.txt"
  296. _, err := os.Create(filePath)
  297. if err != nil {
  298. log.Fatal("Create poc file error: ",err.Error())
  299. return
  300. }
  301. file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0666)
  302. if err != nil {
  303. log.Fatal("Open poc file error:", err.Error())
  304. return
  305. }
  306. defer func() {
  307. err = file.Close()
  308. if err != nil{
  309. log.Fatal("Close poc file error:", err.Error())
  310. }
  311. }()
  312. writer := bufio.NewWriter(file)
  313. buf := make([]byte, 6)
  314. clientLen := len(clients)
  315. i := 0
  316. count := 0
  317. clientNum := 0
  318. var mac string
  319. var url string
  320. for i < pocCount {
  321. count ++
  322. if count > pocCount*2 {
  323. log.Fatal("Build poc url failed")
  324. }
  325. clientNum = mathrand.Intn(clientLen)
  326. _, err = rand.Read(buf)
  327. if err != nil {
  328. fmt.Println("Get mac buf failed: ", err.Error())
  329. continue
  330. }
  331. buf[0] |= 2
  332. mac=fmt.Sprintf("%02x%02x%02x%02x%02x%02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5])
  333. url = fmt.Sprintf("https://security-iot.189smarthome.com:9092/api/v2/poc?vendor=%s&version=0.0.01&mac=%s\n",clients[clientNum],strings.ToUpper(mac))
  334. _, err = writer.WriteString(url)
  335. if err != nil {
  336. continue
  337. }
  338. i++
  339. }
  340. err = writer.Flush()
  341. if err != nil {
  342. log.Fatal("Write poc file failed: ",err.Error())
  343. }
  344. }
  345. func BuildBusinessUrl(pppoeCount int){
  346. var err error
  347. exists := Exists("./data")
  348. if exists {
  349. err = os.RemoveAll("./data")
  350. if err != nil {
  351. log.Fatal(" Remove data failed: ", err.Error())
  352. }
  353. }
  354. err = os.Mkdir("./data", os.ModePerm)
  355. if err != nil {
  356. log.Fatal(" Mkdir data failed: ", err.Error())
  357. }
  358. businessFile := "./data/business.txt"
  359. _, err = os.Create(businessFile)
  360. if err != nil {
  361. log.Fatal("Create business file error: ",err.Error())
  362. return
  363. }
  364. file, err := os.OpenFile(businessFile, os.O_WRONLY|os.O_CREATE, 0666)
  365. if err != nil {
  366. log.Fatal("Open business file error:", err.Error())
  367. return
  368. }
  369. defer func() {
  370. err = file.Close()
  371. if err != nil{
  372. log.Fatal("Close business file error:", err.Error())
  373. }
  374. }()
  375. pppoe := 0
  376. i :=0
  377. count :=0
  378. var businessUrl string
  379. writer := bufio.NewWriter(file)
  380. for i<pppoeCount{
  381. count ++
  382. if count > pppoeCount*2 {
  383. log.Fatal("Build business url failed")
  384. }
  385. pppoe = mathrand.Intn(99999999)
  386. businessUrl = fmt.Sprintf("https://security-iot.189smarthome.com:9092/api/v2/check_pppoe_business?pppoe=0250%08d\n",pppoe)
  387. _, err = writer.WriteString(businessUrl)
  388. if err != nil {
  389. continue
  390. }
  391. i++
  392. }
  393. err = writer.Flush()
  394. if err != nil {
  395. log.Fatal("Write business file failed: ",err.Error())
  396. }
  397. }
  398. func BuildClientUrl(clientCount int){
  399. clientFilePath := "./data/client.txt"
  400. _, err := os.Create(clientFilePath)
  401. if err != nil {
  402. log.Fatal("Create client file error: ",err.Error())
  403. return
  404. }
  405. clientFile, err := os.OpenFile(clientFilePath, os.O_WRONLY|os.O_CREATE, 0666)
  406. if err != nil {
  407. log.Fatal("Open client file error:", err.Error())
  408. return
  409. }
  410. defer func() {
  411. err = clientFile.Close()
  412. if err != nil{
  413. log.Fatal("Close client file error:", err.Error())
  414. }
  415. }()
  416. clientWriter := bufio.NewWriter(clientFile)
  417. buf := make([]byte, 6)
  418. clientLen := len(clients)
  419. i := 0
  420. count := 0
  421. clientNum := 0
  422. var mac string
  423. var clientUrl string
  424. for i < clientCount {
  425. count ++
  426. if count > clientCount*2 {
  427. log.Fatal("Build client url failed")
  428. }
  429. clientNum = mathrand.Intn(clientLen)
  430. _, err = rand.Read(buf)
  431. if err != nil {
  432. fmt.Println("Get mac buf failed: ", err.Error())
  433. continue
  434. }
  435. buf[0] |= 2
  436. mac=fmt.Sprintf("%02x%02x%02x%02x%02x%02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5])
  437. clientUrl = fmt.Sprintf("https://security-iot.189smarthome.com:9092/api/v2/probe-client?vendor=%s&version=3.0.01&mac=%s\n",clients[clientNum],strings.ToUpper(mac))
  438. _, err = clientWriter.WriteString(clientUrl)
  439. if err != nil {
  440. continue
  441. }
  442. i++
  443. }
  444. err = clientWriter.Flush()
  445. if err != nil {
  446. log.Fatal("Write business file failed: ",err.Error())
  447. }
  448. }
  449. func DataForPre(){
  450. var err error
  451. exists := Exists("./data")
  452. if exists {
  453. err = os.RemoveAll("./data")
  454. if err != nil {
  455. log.Fatal(" Remove data failed: ", err.Error())
  456. }
  457. }
  458. err = os.Mkdir("./data", os.ModePerm)
  459. if err != nil {
  460. log.Fatal(" Mkdir data failed: ", err.Error())
  461. }
  462. preClientFilePath := "./predata/client.txt"
  463. preClientFile, err := os.Open(preClientFilePath)
  464. if err != nil {
  465. log.Fatal("Open pre client file error:", err.Error())
  466. return
  467. }
  468. defer func() {
  469. err = preClientFile.Close()
  470. if err != nil{
  471. log.Fatal("Close pre client file error:", err.Error())
  472. }
  473. }()
  474. reader := bufio.NewReader(preClientFile)
  475. var line []byte
  476. for{
  477. line,_,err = reader.ReadLine()
  478. if err == io.EOF{
  479. break
  480. }
  481. clients = append(clients,string(line))
  482. }
  483. }
  484. func Exists(name string) bool {
  485. if _, err := os.Stat(name); err != nil {
  486. if os.IsNotExist(err) {
  487. return false
  488. }
  489. }
  490. return true
  491. }